Skip to content

Commit

Permalink
Correctly read commands from cmd file when file path is absolute
Browse files Browse the repository at this point in the history
  • Loading branch information
DamjanBecirovic committed Oct 24, 2024
1 parent f5d68c7 commit ed30309
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
27 changes: 25 additions & 2 deletions pkg/commands/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
)

type File struct {
Expand All @@ -15,8 +16,11 @@ type File struct {
}

func (f *File) Extract() error {
// Resolve FilePath relative to YamlPath
absoluteFilePath := filepath.Join(filepath.Dir(f.YamlPath), f.FilePath)
// Get the full path to the commands file in the reposiotry
absoluteFilePath, err := f.getAbsoluteFilePath()
if err != nil {
return fmt.Errorf("failed to resolved the file path for file %s, error: %w", absoluteFilePath, err)
}

// Open the file
file, err := os.Open(filepath.Clean((absoluteFilePath)))
Expand All @@ -43,4 +47,23 @@ func (f *File) Extract() error {
}

return nil
}

func (f *File) getAbsoluteFilePath() (string, error) {
// Get the path to git repository root on filesystem
workingDir, err := os.Getwd()
if err != nil {
return "", err
}

// If file path starts with '/' it is an absolute path from the root of git repository
if strings.HasPrefix(f.FilePath, "/") {
// Join the git repository root with the file path
return filepath.Join(workingDir, f.FilePath), nil
} else {
// Else, join the git repository root with the directory path for YML file
ymlDirPath := filepath.Join(workingDir, filepath.Dir(f.YamlPath))
// and then File path is relative to that YML directory path
return filepath.Join(ymlDirPath, f.FilePath), nil
}
}
14 changes: 11 additions & 3 deletions pkg/commands/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func Test__Extract(t *testing.T) {

assert.Error(t, err)

expectedErrorMessage := "failed to open the commands_file at ../../test/fixtures/non_existing_file.txt"
expectedErrorMessage := "failed to open the commands_file at"
assert.Contains(t, err.Error(), expectedErrorMessage)

// If commands file is empty, it retruns the error
Expand All @@ -27,15 +27,23 @@ func Test__Extract(t *testing.T) {

assert.Error(t, err)

expectedErrorMessage = "the commands_file at location ../../test/fixtures/empty_file.txt is empty"
expectedErrorMessage = "empty_file.txt is empty"
assert.Contains(t, err.Error(), expectedErrorMessage)

// Commands are read successfully from the valid file.
// Commands are read successfully from the valid file with relative path.
file.FilePath = "valid_commands_file.txt"
err = file.Extract()

assert.Nil(t, err)

expectedCommands := []string{"echo 1", "echo 12", "echo 123"}
assert.Equal(t, file.Commands, expectedCommands)

// Commands are read successfully from the valid file with absolute path.
file.FilePath = "/../../test/fixtures/valid_commands_file.txt"
file.Commands = []string{}
err = file.Extract()

assert.Nil(t, err)
assert.Equal(t, file.Commands, expectedCommands)
}
4 changes: 2 additions & 2 deletions test/e2e/cmd_files_all_possible_locations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
os_image: "ubuntu2004"
global_job_config:
prologue:
commands_file: "valid_commands_file.txt"
commands_file: "/.semaphore/valid_commands_file.txt"
epilogue:
always:
commands_file: "valid_commands_file.txt"
Expand All @@ -36,7 +36,7 @@
on_pass:
commands_file: "valid_commands_file.txt"
on_fail:
commands_file: "valid_commands_file.txt"
commands_file: "/.semaphore/valid_commands_file.txt"
jobs:
- name: Run tests
commands_file: "valid_commands_file.txt"
Expand Down

0 comments on commit ed30309

Please sign in to comment.