Skip to content

Commit

Permalink
feat: variable inheritance tests (#2038)
Browse files Browse the repository at this point in the history
  • Loading branch information
pd93 authored Feb 5, 2025
1 parent 3aee0a0 commit b5b1524
Show file tree
Hide file tree
Showing 49 changed files with 519 additions and 2 deletions.
108 changes: 106 additions & 2 deletions task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package task_test

import (
"bytes"
"cmp"
"context"
"fmt"
"io"
Expand Down Expand Up @@ -134,8 +135,7 @@ func TestEnv(t *testing.T) {
},
}
tt.Run(t)
t.Setenv("TASK_X_ENV_PRECEDENCE", "1")
experiments.EnvPrecedence = experiments.New("ENV_PRECEDENCE")
enableExperimentForTest(t, &experiments.EnvPrecedence, "1")
ttt := fileContentTest{
Dir: "testdata/env",
Target: "overridden",
Expand Down Expand Up @@ -3207,6 +3207,110 @@ func TestReference(t *testing.T) {
}
}

func TestVarInheritance(t *testing.T) {
enableExperimentForTest(t, &experiments.EnvPrecedence, "1")
tests := []struct {
name string
want string
call string
}{
{
name: "shell",
want: "shell\nshell\n",
},
{
name: "entrypoint-global-dotenv",
want: "entrypoint-global-dotenv\nentrypoint-global-dotenv\n",
},
{
name: "entrypoint-global-vars",
want: "entrypoint-global-vars\nentrypoint-global-vars\n",
},
{
// We can't send env vars to a called task, so the env var is not overridden
name: "entrypoint-task-call-vars",
want: "entrypoint-task-call-vars\nentrypoint-global-vars\n",
},
{
// Dotenv doesn't set variables
name: "entrypoint-task-call-dotenv",
want: "entrypoint-task-call-vars\nentrypoint-task-call-dotenv\n",
},
{
name: "entrypoint-task-call-task-vars",
want: "entrypoint-task-call-task-vars\nentrypoint-task-call-task-vars\n",
},
{
// Dotenv doesn't set variables
name: "entrypoint-task-dotenv",
want: "entrypoint-global-vars\nentrypoint-task-dotenv\n",
},
{
name: "entrypoint-task-vars",
want: "entrypoint-task-vars\nentrypoint-task-vars\n",
},
// {
// // Dotenv not currently allowed in included taskfiles
// name: "included-global-dotenv",
// want: "included-global-dotenv\nincluded-global-dotenv\n",
// },
{
name: "included-global-vars",
want: "included-global-vars\nincluded-global-vars\n",
call: "included",
},
{
// We can't send env vars to a called task, so the env var is not overridden
name: "included-task-call-vars",
want: "included-task-call-vars\nincluded-global-vars\n",
call: "included",
},
{
// Dotenv doesn't set variables
// Dotenv not currently allowed in included taskfiles (but doesn't error in a task)
name: "included-task-call-dotenv",
want: "included-task-call-vars\nincluded-global-vars\n",
call: "included",
},
{
name: "included-task-call-task-vars",
want: "included-task-call-task-vars\nincluded-task-call-task-vars\n",
call: "included",
},
{
// Dotenv doesn't set variables
// Somehow dotenv is working here!
name: "included-task-dotenv",
want: "included-global-vars\nincluded-task-dotenv\n",
call: "included",
},
{
name: "included-task-vars",
want: "included-task-vars\nincluded-task-vars\n",
call: "included",
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var buff bytes.Buffer
t.Setenv("VAR", "shell")
t.Setenv("ENV", "shell")
e := task.Executor{
Dir: fmt.Sprintf("testdata/var_inheritance/v3/%s", test.name),
Stdout: &buff,
Stderr: &buff,
Silent: true,
Force: true,
}
call := cmp.Or(test.call, "default")
require.NoError(t, e.Setup())
require.NoError(t, e.Run(context.Background(), &ast.Call{Task: call}))
assert.Equal(t, test.want, buff.String())
})
}
}

// enableExperimentForTest enables the experiment behind pointer e for the duration of test t and sub-tests,
// with the experiment being restored to its previous state when tests complete.
//
Expand Down
11 changes: 11 additions & 0 deletions testdata/var_inheritance/v3/entrypoint-global-dotenv/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: '3'

silent: true
dotenv:
- 'global.env'

tasks:
default:
cmds:
- 'echo "{{.VAR}}"'
- 'echo "$ENV"'
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
VAR=entrypoint-global-dotenv
ENV=entrypoint-global-dotenv
15 changes: 15 additions & 0 deletions testdata/var_inheritance/v3/entrypoint-global-vars/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: '3'

silent: true
dotenv:
- 'global.env'
vars:
VAR: entrypoint-global-vars
env:
ENV: entrypoint-global-vars

tasks:
default:
cmds:
- 'echo "{{.VAR}}"'
- 'echo "$ENV"'
2 changes: 2 additions & 0 deletions testdata/var_inheritance/v3/entrypoint-global-vars/global.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
VAR=entrypoint-global-dotenv
ENV=entrypoint-global-dotenv
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version: '3'

silent: true
dotenv:
- 'global.env'
vars:
VAR: entrypoint-global-vars
env:
ENV: entrypoint-global-vars

tasks:
default:
dotenv:
- 'task.env'
cmds:
- task: called-task
vars:
VAR: entrypoint-task-call-vars

called-task:
dotenv:
- 'called-task.env'
cmds:
- 'echo "{{.VAR}}"'
- 'echo "$ENV"'
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
VAR=entrypoint-task-call-dotenv
ENV=entrypoint-task-call-dotenv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
VAR=entrypoint-global-dotenv
ENV=entrypoint-global-dotenv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
VAR=entrypoint-task-dotenv
ENV=entrypoint-task-dotenv
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
version: '3'

silent: true
dotenv:
- 'global.env'
vars:
VAR: entrypoint-global-vars
env:
ENV: entrypoint-global-vars

tasks:
default:
dotenv:
- 'task.env'
cmds:
- task: called-task
vars:
VAR: entrypoint-task-call-vars

called-task:
vars:
VAR: entrypoint-task-call-task-vars
env:
ENV: entrypoint-task-call-task-vars
cmds:
- 'echo "{{.VAR}}"'
- 'echo "$ENV"'
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
VAR=entrypoint-global-dotenv
ENV=entrypoint-global-dotenv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
VAR=entrypoint-task-dotenv
ENV=entrypoint-task-dotenv
23 changes: 23 additions & 0 deletions testdata/var_inheritance/v3/entrypoint-task-call-vars/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
version: '3'

silent: true
dotenv:
- 'global.env'
vars:
VAR: entrypoint-global-vars
env:
ENV: entrypoint-global-vars

tasks:
default:
dotenv:
- 'task.env'
cmds:
- task: called-task
vars:
VAR: entrypoint-task-call-vars

called-task:
cmds:
- 'echo "{{.VAR}}"'
- 'echo "$ENV"'
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
VAR=entrypoint-global-dotenv
ENV=entrypoint-global-dotenv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
VAR=entrypoint-task-dotenv
ENV=entrypoint-task-dotenv
17 changes: 17 additions & 0 deletions testdata/var_inheritance/v3/entrypoint-task-dotenv/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
version: '3'

silent: true
dotenv:
- 'global.env'
vars:
VAR: entrypoint-global-vars
env:
ENV: entrypoint-global-vars

tasks:
default:
dotenv:
- 'task.env'
cmds:
- 'echo "{{.VAR}}"'
- 'echo "$ENV"'
2 changes: 2 additions & 0 deletions testdata/var_inheritance/v3/entrypoint-task-dotenv/global.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
VAR=entrypoint-global-dotenv
ENV=entrypoint-global-dotenv
2 changes: 2 additions & 0 deletions testdata/var_inheritance/v3/entrypoint-task-dotenv/task.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
VAR=entrypoint-task-dotenv
ENV=entrypoint-task-dotenv
21 changes: 21 additions & 0 deletions testdata/var_inheritance/v3/entrypoint-task-vars/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
version: '3'

silent: true
dotenv:
- 'global.env'
vars:
VAR: entrypoint-global-vars
env:
ENV: entrypoint-global-vars

tasks:
default:
dotenv:
- 'task.env'
vars:
VAR: entrypoint-task-vars
env:
ENV: entrypoint-task-vars
cmds:
- 'echo "{{.VAR}}"'
- 'echo "$ENV"'
2 changes: 2 additions & 0 deletions testdata/var_inheritance/v3/entrypoint-task-vars/global.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
VAR=entrypoint-global-dotenv
ENV=entrypoint-global-dotenv
2 changes: 2 additions & 0 deletions testdata/var_inheritance/v3/entrypoint-task-vars/task.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
VAR=entrypoint-task-dotenv
ENV=entrypoint-task-dotenv
12 changes: 12 additions & 0 deletions testdata/var_inheritance/v3/included-global-vars/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: '3'

silent: true
dotenv:
- 'global.env'
vars:
VAR: entrypoint-global-vars
env:
ENV: entrypoint-global-vars

includes:
included: included.yml
2 changes: 2 additions & 0 deletions testdata/var_inheritance/v3/included-global-vars/global.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
VAR=entrypoint-global-dotenv
ENV=entrypoint-global-dotenv
13 changes: 13 additions & 0 deletions testdata/var_inheritance/v3/included-global-vars/included.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: '3'

silent: true
vars:
VAR: included-global-vars
env:
ENV: included-global-vars

tasks:
default:
cmds:
- 'echo "{{.VAR}}"'
- 'echo "$ENV"'
12 changes: 12 additions & 0 deletions testdata/var_inheritance/v3/included-task-call-dotenv/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: '3'

silent: true
dotenv:
- 'global.env'
vars:
VAR: entrypoint-global-vars
env:
ENV: entrypoint-global-vars

includes:
included: included.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
VAR=entrypoint-global-dotenv
ENV=entrypoint-global-dotenv
21 changes: 21 additions & 0 deletions testdata/var_inheritance/v3/included-task-call-dotenv/included.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
version: '3'

silent: true
vars:
VAR: included-global-vars
env:
ENV: included-global-vars

tasks:
default:
dotenv:
- 'task.env'
cmds:
- task: called-task
vars:
VAR: included-task-call-vars

called-task:
cmds:
- 'echo "{{.VAR}}"'
- 'echo "$ENV"'
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
VAR=included-task-dotenv
ENV=included-task-dotenv
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: '3'

silent: true
dotenv:
- 'global.env'
vars:
VAR: entrypoint-global-vars
env:
ENV: entrypoint-global-vars

includes:
included: included.yml
Loading

0 comments on commit b5b1524

Please sign in to comment.