diff --git a/tooling/templatize/pkg/ev2/pipeline.go b/tooling/templatize/pkg/ev2/pipeline.go index d3fe0a807..b1dcc0179 100644 --- a/tooling/templatize/pkg/ev2/pipeline.go +++ b/tooling/templatize/pkg/ev2/pipeline.go @@ -50,13 +50,12 @@ func PrecompilePipelineForEV2(pipelineFilePath string, vars config.Variables) (* } // store the processed files to disk relative to the pipeline directory - _, restoreDir, err := processedPipeline.EnterPipelineDir() - if err != nil { - return nil, fmt.Errorf("failed to enter pipeline directory: %w", err) - } - defer restoreDir() for filePath, content := range processedFiles { - err := os.WriteFile(filePath, content, 0644) + absFilePath, err := processedPipeline.AbsoluteFilePath(filePath) + if err != nil { + return nil, fmt.Errorf("failed to get absolute file path for %q: %w", filePath, err) + } + err = os.WriteFile(absFilePath, content, 0644) if err != nil { return nil, fmt.Errorf("failed to write precompiled file %q: %w", filePath, err) } @@ -66,18 +65,15 @@ func PrecompilePipelineForEV2(pipelineFilePath string, vars config.Variables) (* } func readReferencedPipelineFiles(p *pipeline.Pipeline) (map[string][]byte, error) { - // switch to pipeline directory to ensure relative paths are resolvable - _, restoreDir, err := p.EnterPipelineDir() - if err != nil { - return nil, fmt.Errorf("failed to enter pipeline directory: %w", err) - } - defer restoreDir() - referencedFiles := make(map[string][]byte) for _, rg := range p.ResourceGroups { for _, step := range rg.Steps { if step.Parameters != "" { - paramFileContent, err := os.ReadFile(step.Parameters) + absFilePath, err := p.AbsoluteFilePath(step.Parameters) + if err != nil { + return nil, fmt.Errorf("failed to get absolute file path for %q: %w", step.Parameters, err) + } + paramFileContent, err := os.ReadFile(absFilePath) if err != nil { return nil, fmt.Errorf("failed to read parameter file %q: %w", step.Parameters, err) } diff --git a/tooling/templatize/pkg/pipeline/common.go b/tooling/templatize/pkg/pipeline/common.go index e8c2fb1d3..2a2584000 100644 --- a/tooling/templatize/pkg/pipeline/common.go +++ b/tooling/templatize/pkg/pipeline/common.go @@ -2,7 +2,6 @@ package pipeline import ( "fmt" - "os" "path/filepath" "gopkg.in/yaml.v3" @@ -26,22 +25,6 @@ func (p *Pipeline) PipelineFilePath() string { return p.pipelineFilePath } -func (p *Pipeline) EnterPipelineDir() (string, func(), error) { - currentDir, err := os.Getwd() - if err != nil { - return "", nil, err - } - - pipelineDir, err := filepath.Abs(filepath.Dir(p.pipelineFilePath)) - if err != nil { - return "", nil, err - } - err = os.Chdir(pipelineDir) - if err != nil { - return "", nil, err - } - - return pipelineDir, func() { - _ = os.Chdir(currentDir) - }, nil +func (p *Pipeline) AbsoluteFilePath(filePath string) (string, error) { + return filepath.Abs(filepath.Join(filepath.Dir(p.pipelineFilePath), filePath)) } diff --git a/tooling/templatize/pkg/pipeline/common_test.go b/tooling/templatize/pkg/pipeline/common_test.go index aa1f42f95..6a2ad5c8c 100644 --- a/tooling/templatize/pkg/pipeline/common_test.go +++ b/tooling/templatize/pkg/pipeline/common_test.go @@ -1,7 +1,6 @@ package pipeline import ( - "os" "path/filepath" "testing" @@ -37,7 +36,7 @@ func TestDeepCopy(t *testing.T) { } } -func TestEnterPipelineDir(t *testing.T) { +func TestAbsoluteFilePath(t *testing.T) { configProvider := config.NewConfigProvider("../../testdata/config.yaml") vars, err := configProvider.GetVariables("public", "int", "", config.NewConfigReplacements("r", "sr", "s")) if err != nil { @@ -48,21 +47,39 @@ func TestEnterPipelineDir(t *testing.T) { t.Errorf("failed to read new pipeline: %v", err) } - originalDir, _ := os.Getwd() - - pipelineDir, cleanup, err := pipeline.EnterPipelineDir() - if err != nil { - t.Errorf("failed to enter pipeline dir: %v", err) + abspath := func (path string) string { + abs, _ := filepath.Abs(path) + return abs + } + testCases := []struct { + name string + relativeFile string + absoluteFile string + }{ + { + name: "basic", + relativeFile: "test.bicepparam", + absoluteFile: abspath("../../testdata/test.bicepparam"), + }, + { + name: "go one lower", + relativeFile: "../test.bicepparam", + absoluteFile: abspath("../../test.bicepparam"), + }, + { + name: "subdir", + relativeFile: "subdir/test.bicepparam", + absoluteFile: abspath("../../testdata/subdir/test.bicepparam"), + }, + } + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + abs, err := pipeline.AbsoluteFilePath(tc.relativeFile) + if err != nil { + t.Errorf("failed to get absolute file path: %v", err) + } + assert.Equal(t, abs, tc.absoluteFile, "expected absolute file path to be correct") + }) } - defer cleanup() - - currentDir, _ := os.Getwd() - pipelineAbsDir, _ := filepath.Abs(pipelineDir) - assert.Equal(t, pipelineDir, pipelineAbsDir, "expected absolute pipeline dir to be announced") - assert.Equal(t, currentDir, pipelineAbsDir, "expected to be in pipeline dir") - - cleanup() - restoredDir, _ := os.Getwd() - assert.Equal(t, restoredDir, originalDir, "expected to return to original dir") }