Skip to content

Commit

Permalink
don't change pwd
Browse files Browse the repository at this point in the history
Signed-off-by: Gerd Oberlechner <[email protected]>
  • Loading branch information
geoberle committed Nov 25, 2024
1 parent d0f3624 commit caf2cc7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 50 deletions.
24 changes: 10 additions & 14 deletions tooling/templatize/pkg/ev2/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
21 changes: 2 additions & 19 deletions tooling/templatize/pkg/pipeline/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package pipeline

import (
"fmt"
"os"
"path/filepath"

"gopkg.in/yaml.v3"
Expand All @@ -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))
}
51 changes: 34 additions & 17 deletions tooling/templatize/pkg/pipeline/common_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package pipeline

import (
"os"
"path/filepath"
"testing"

Expand Down Expand Up @@ -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 {
Expand All @@ -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")

}

0 comments on commit caf2cc7

Please sign in to comment.