From ed303095c89cc9b8cf9a815b45e1d227f036ca7f Mon Sep 17 00:00:00 2001 From: Damjan Becirovic Date: Thu, 24 Oct 2024 13:20:27 +0200 Subject: [PATCH] Correctly read commands from cmd file when file path is absolute --- pkg/commands/file.go | 27 ++++++++++++++++++-- pkg/commands/file_test.go | 14 +++++++--- test/e2e/cmd_files_all_possible_locations.rb | 4 +-- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/pkg/commands/file.go b/pkg/commands/file.go index 6ca97cc..b68c2a9 100644 --- a/pkg/commands/file.go +++ b/pkg/commands/file.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "path/filepath" + "strings" ) type File struct { @@ -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))) @@ -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 + } } \ No newline at end of file diff --git a/pkg/commands/file_test.go b/pkg/commands/file_test.go index 34d5609..dce0b67 100644 --- a/pkg/commands/file_test.go +++ b/pkg/commands/file_test.go @@ -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 @@ -27,10 +27,10 @@ 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() @@ -38,4 +38,12 @@ func Test__Extract(t *testing.T) { 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) } \ No newline at end of file diff --git a/test/e2e/cmd_files_all_possible_locations.rb b/test/e2e/cmd_files_all_possible_locations.rb index 959e16d..4dca798 100644 --- a/test/e2e/cmd_files_all_possible_locations.rb +++ b/test/e2e/cmd_files_all_possible_locations.rb @@ -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" @@ -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"