-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[VAULT-28010] fix(funcs): fix relative paths in
file()
(#145)
The recently added `file()` function supports relative paths but they’ll always be relative to the working dir that exec’s enos. This is usually fine unless enos is invoked with `--chdir`, in which case we need funcs that support relative paths to be relative to the `--chdir` working dir. * Make `file()` support the `--chdir` base path when given a base path * Bump the version * Add tests for funcs that support relative paths * Remove test data that wasn't being used Signed-off-by: Ryan Cragun <me@ryan.ec>
- Loading branch information
1 parent
57167cd
commit 3ab1184
Showing
6 changed files
with
167 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package funcs | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/zclconf/go-cty/cty" | ||
) | ||
|
||
func TestAbsPathFunc(t *testing.T) { | ||
tmpDir := t.TempDir() | ||
t.Parallel() | ||
|
||
for name, test := range map[string]struct { | ||
basePath func() string | ||
path string | ||
absPath func() string | ||
}{ | ||
"absolute_with_irrelevant_base": { | ||
basePath: func() string { return "/my/current/working/dir" }, | ||
path: "/some/absolute/path/file.txt", | ||
absPath: func() string { return "/some/absolute/path/file.txt" }, | ||
}, | ||
"absolute_no_base": { | ||
basePath: func() string { return "" }, | ||
path: "/some/absolute/path/file.txt", | ||
absPath: func() string { return "/some/absolute/path/file.txt" }, | ||
}, | ||
"relative_no_base": { | ||
basePath: func() string { | ||
wd, err := os.Getwd() | ||
require.NoError(t, err) | ||
|
||
return wd | ||
}, | ||
path: "./some/relative", | ||
absPath: func() string { | ||
wd, err := os.Getwd() | ||
require.NoError(t, err) | ||
abs, err := filepath.Abs(filepath.Join(wd, "./some/relative")) | ||
require.NoError(t, err) | ||
|
||
return abs | ||
}, | ||
}, | ||
"relative_base": { | ||
basePath: func() string { return tmpDir }, | ||
path: "./some/relative", | ||
absPath: func() string { | ||
abs, err := filepath.Abs(filepath.Join(tmpDir, "./some/relative")) | ||
require.NoError(t, err) | ||
|
||
return abs | ||
}, | ||
}, | ||
} { | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
basePath := test.basePath() | ||
absPath, err := AbsPathFunc(basePath).Call([]cty.Value{cty.StringVal(test.path)}) | ||
require.NoError(t, err) | ||
require.Equal(t, test.absPath(), absPath.AsString()) | ||
}) | ||
} | ||
} | ||
|
||
func TestFileFunc(t *testing.T) { | ||
tmpDir := t.TempDir() | ||
t.Parallel() | ||
|
||
for name, test := range map[string]struct { | ||
basePath func() string | ||
path func() string | ||
contents string | ||
}{ | ||
"absolute_with_irrelevant_base": { | ||
basePath: func() string { return "/my/current/working/dir" }, | ||
path: func() string { | ||
p, err := filepath.Abs("./testdata/test_file_func.txt") | ||
require.NoError(t, err) | ||
|
||
return p | ||
}, | ||
contents: "static\n", | ||
}, | ||
"absolute_no_base": { | ||
basePath: func() string { return "" }, | ||
path: func() string { | ||
p, err := filepath.Abs("./testdata/test_file_func.txt") | ||
require.NoError(t, err) | ||
|
||
return p | ||
}, | ||
contents: "static\n", | ||
}, | ||
"relative": { | ||
basePath: func() string { | ||
require.NoError(t, os.WriteFile(filepath.Join(tmpDir, "test_file_func.txt"), []byte("dynamic"), 0o755)) | ||
return tmpDir | ||
}, | ||
path: func() string { return "./test_file_func.txt" }, | ||
contents: "dynamic", | ||
}, | ||
"relative_no_base": { | ||
basePath: func() string { return "" }, | ||
path: func() string { return "./testdata/test_file_func.txt" }, | ||
contents: "static\n", | ||
}, | ||
} { | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
basePath := test.basePath() | ||
contents, err := FileFunc(basePath).Call([]cty.Value{cty.StringVal(test.path())}) | ||
require.NoError(t, err) | ||
require.Equal(t, test.contents, contents.AsString()) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
static |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters