Skip to content

Commit 1b06da1

Browse files
feat(templater): add absPath template function (#2788)
Signed-off-by: Mateen Anjum <mateenali66@gmail.com> Co-authored-by: Valentin Maerten <maerten.valentin@gmail.com>
1 parent 6e37e3d commit 1b06da1

4 files changed

Lines changed: 29 additions & 0 deletions

File tree

internal/templater/funcs.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func init() {
3434
"IsSH": IsSH, // Deprecated
3535
"joinPath": filepath.Join,
3636
"relPath": filepath.Rel,
37+
"absPath": filepath.Abs,
3738
"merge": merge,
3839
"spew": spew.Sdump,
3940
"fromYaml": fromYaml,

task_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2601,6 +2601,27 @@ func TestSplitArgs(t *testing.T) {
26012601
assert.Equal(t, "3\n", buff.String())
26022602
}
26032603

2604+
func TestAbsPath(t *testing.T) {
2605+
t.Parallel()
2606+
2607+
var buff bytes.Buffer
2608+
e := task.NewExecutor(
2609+
task.WithDir("testdata/abs_path"),
2610+
task.WithStdout(&buff),
2611+
task.WithStderr(&buff),
2612+
task.WithSilent(true),
2613+
)
2614+
require.NoError(t, e.Setup())
2615+
2616+
err := e.Run(t.Context(), &task.Call{Task: "default"})
2617+
require.NoError(t, err)
2618+
2619+
cwd, err := os.Getwd()
2620+
require.NoError(t, err)
2621+
expected := filepath.Join(cwd, "bar") + "\n"
2622+
assert.Equal(t, expected, buff.String())
2623+
}
2624+
26042625
func TestSingleCmdDep(t *testing.T) {
26052626
t.Parallel()
26062627

testdata/abs_path/Taskfile.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: '3'
2+
3+
tasks:
4+
default:
5+
cmds:
6+
- cmd: echo '{{absPath "foo/../bar"}}'

website/src/docs/reference/templating.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,7 @@ tasks:
617617
- echo "{{.WIN_PATH | fromSlash}}" # Convert to OS-specific slashes
618618
- echo "{{joinPath .OUTPUT_DIR .BINARY_NAME}}" # Join path elements
619619
- echo "Relative {{relPath .ROOT_DIR .TASKFILE_DIR}}" # Get relative path
620+
- echo '{{absPath "../sibling"}}' # Resolve to an absolute path
620621
```
621622

622623
### Data Structure Functions

0 commit comments

Comments
 (0)