Skip to content

Commit b5b1524

Browse files
authored
feat: variable inheritance tests (#2038)
1 parent 3aee0a0 commit b5b1524

File tree

49 files changed

+519
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+519
-2
lines changed

task_test.go

+106-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package task_test
22

33
import (
44
"bytes"
5+
"cmp"
56
"context"
67
"fmt"
78
"io"
@@ -134,8 +135,7 @@ func TestEnv(t *testing.T) {
134135
},
135136
}
136137
tt.Run(t)
137-
t.Setenv("TASK_X_ENV_PRECEDENCE", "1")
138-
experiments.EnvPrecedence = experiments.New("ENV_PRECEDENCE")
138+
enableExperimentForTest(t, &experiments.EnvPrecedence, "1")
139139
ttt := fileContentTest{
140140
Dir: "testdata/env",
141141
Target: "overridden",
@@ -3207,6 +3207,110 @@ func TestReference(t *testing.T) {
32073207
}
32083208
}
32093209

3210+
func TestVarInheritance(t *testing.T) {
3211+
enableExperimentForTest(t, &experiments.EnvPrecedence, "1")
3212+
tests := []struct {
3213+
name string
3214+
want string
3215+
call string
3216+
}{
3217+
{
3218+
name: "shell",
3219+
want: "shell\nshell\n",
3220+
},
3221+
{
3222+
name: "entrypoint-global-dotenv",
3223+
want: "entrypoint-global-dotenv\nentrypoint-global-dotenv\n",
3224+
},
3225+
{
3226+
name: "entrypoint-global-vars",
3227+
want: "entrypoint-global-vars\nentrypoint-global-vars\n",
3228+
},
3229+
{
3230+
// We can't send env vars to a called task, so the env var is not overridden
3231+
name: "entrypoint-task-call-vars",
3232+
want: "entrypoint-task-call-vars\nentrypoint-global-vars\n",
3233+
},
3234+
{
3235+
// Dotenv doesn't set variables
3236+
name: "entrypoint-task-call-dotenv",
3237+
want: "entrypoint-task-call-vars\nentrypoint-task-call-dotenv\n",
3238+
},
3239+
{
3240+
name: "entrypoint-task-call-task-vars",
3241+
want: "entrypoint-task-call-task-vars\nentrypoint-task-call-task-vars\n",
3242+
},
3243+
{
3244+
// Dotenv doesn't set variables
3245+
name: "entrypoint-task-dotenv",
3246+
want: "entrypoint-global-vars\nentrypoint-task-dotenv\n",
3247+
},
3248+
{
3249+
name: "entrypoint-task-vars",
3250+
want: "entrypoint-task-vars\nentrypoint-task-vars\n",
3251+
},
3252+
// {
3253+
// // Dotenv not currently allowed in included taskfiles
3254+
// name: "included-global-dotenv",
3255+
// want: "included-global-dotenv\nincluded-global-dotenv\n",
3256+
// },
3257+
{
3258+
name: "included-global-vars",
3259+
want: "included-global-vars\nincluded-global-vars\n",
3260+
call: "included",
3261+
},
3262+
{
3263+
// We can't send env vars to a called task, so the env var is not overridden
3264+
name: "included-task-call-vars",
3265+
want: "included-task-call-vars\nincluded-global-vars\n",
3266+
call: "included",
3267+
},
3268+
{
3269+
// Dotenv doesn't set variables
3270+
// Dotenv not currently allowed in included taskfiles (but doesn't error in a task)
3271+
name: "included-task-call-dotenv",
3272+
want: "included-task-call-vars\nincluded-global-vars\n",
3273+
call: "included",
3274+
},
3275+
{
3276+
name: "included-task-call-task-vars",
3277+
want: "included-task-call-task-vars\nincluded-task-call-task-vars\n",
3278+
call: "included",
3279+
},
3280+
{
3281+
// Dotenv doesn't set variables
3282+
// Somehow dotenv is working here!
3283+
name: "included-task-dotenv",
3284+
want: "included-global-vars\nincluded-task-dotenv\n",
3285+
call: "included",
3286+
},
3287+
{
3288+
name: "included-task-vars",
3289+
want: "included-task-vars\nincluded-task-vars\n",
3290+
call: "included",
3291+
},
3292+
}
3293+
3294+
for _, test := range tests {
3295+
t.Run(test.name, func(t *testing.T) {
3296+
var buff bytes.Buffer
3297+
t.Setenv("VAR", "shell")
3298+
t.Setenv("ENV", "shell")
3299+
e := task.Executor{
3300+
Dir: fmt.Sprintf("testdata/var_inheritance/v3/%s", test.name),
3301+
Stdout: &buff,
3302+
Stderr: &buff,
3303+
Silent: true,
3304+
Force: true,
3305+
}
3306+
call := cmp.Or(test.call, "default")
3307+
require.NoError(t, e.Setup())
3308+
require.NoError(t, e.Run(context.Background(), &ast.Call{Task: call}))
3309+
assert.Equal(t, test.want, buff.String())
3310+
})
3311+
}
3312+
}
3313+
32103314
// enableExperimentForTest enables the experiment behind pointer e for the duration of test t and sub-tests,
32113315
// with the experiment being restored to its previous state when tests complete.
32123316
//
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
version: '3'
2+
3+
silent: true
4+
dotenv:
5+
- 'global.env'
6+
7+
tasks:
8+
default:
9+
cmds:
10+
- 'echo "{{.VAR}}"'
11+
- 'echo "$ENV"'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
VAR=entrypoint-global-dotenv
2+
ENV=entrypoint-global-dotenv
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
version: '3'
2+
3+
silent: true
4+
dotenv:
5+
- 'global.env'
6+
vars:
7+
VAR: entrypoint-global-vars
8+
env:
9+
ENV: entrypoint-global-vars
10+
11+
tasks:
12+
default:
13+
cmds:
14+
- 'echo "{{.VAR}}"'
15+
- 'echo "$ENV"'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
VAR=entrypoint-global-dotenv
2+
ENV=entrypoint-global-dotenv
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
version: '3'
2+
3+
silent: true
4+
dotenv:
5+
- 'global.env'
6+
vars:
7+
VAR: entrypoint-global-vars
8+
env:
9+
ENV: entrypoint-global-vars
10+
11+
tasks:
12+
default:
13+
dotenv:
14+
- 'task.env'
15+
cmds:
16+
- task: called-task
17+
vars:
18+
VAR: entrypoint-task-call-vars
19+
20+
called-task:
21+
dotenv:
22+
- 'called-task.env'
23+
cmds:
24+
- 'echo "{{.VAR}}"'
25+
- 'echo "$ENV"'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
VAR=entrypoint-task-call-dotenv
2+
ENV=entrypoint-task-call-dotenv
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
VAR=entrypoint-global-dotenv
2+
ENV=entrypoint-global-dotenv
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
VAR=entrypoint-task-dotenv
2+
ENV=entrypoint-task-dotenv
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
version: '3'
2+
3+
silent: true
4+
dotenv:
5+
- 'global.env'
6+
vars:
7+
VAR: entrypoint-global-vars
8+
env:
9+
ENV: entrypoint-global-vars
10+
11+
tasks:
12+
default:
13+
dotenv:
14+
- 'task.env'
15+
cmds:
16+
- task: called-task
17+
vars:
18+
VAR: entrypoint-task-call-vars
19+
20+
called-task:
21+
vars:
22+
VAR: entrypoint-task-call-task-vars
23+
env:
24+
ENV: entrypoint-task-call-task-vars
25+
cmds:
26+
- 'echo "{{.VAR}}"'
27+
- 'echo "$ENV"'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
VAR=entrypoint-global-dotenv
2+
ENV=entrypoint-global-dotenv
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
VAR=entrypoint-task-dotenv
2+
ENV=entrypoint-task-dotenv
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
version: '3'
2+
3+
silent: true
4+
dotenv:
5+
- 'global.env'
6+
vars:
7+
VAR: entrypoint-global-vars
8+
env:
9+
ENV: entrypoint-global-vars
10+
11+
tasks:
12+
default:
13+
dotenv:
14+
- 'task.env'
15+
cmds:
16+
- task: called-task
17+
vars:
18+
VAR: entrypoint-task-call-vars
19+
20+
called-task:
21+
cmds:
22+
- 'echo "{{.VAR}}"'
23+
- 'echo "$ENV"'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
VAR=entrypoint-global-dotenv
2+
ENV=entrypoint-global-dotenv
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
VAR=entrypoint-task-dotenv
2+
ENV=entrypoint-task-dotenv
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
version: '3'
2+
3+
silent: true
4+
dotenv:
5+
- 'global.env'
6+
vars:
7+
VAR: entrypoint-global-vars
8+
env:
9+
ENV: entrypoint-global-vars
10+
11+
tasks:
12+
default:
13+
dotenv:
14+
- 'task.env'
15+
cmds:
16+
- 'echo "{{.VAR}}"'
17+
- 'echo "$ENV"'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
VAR=entrypoint-global-dotenv
2+
ENV=entrypoint-global-dotenv
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
VAR=entrypoint-task-dotenv
2+
ENV=entrypoint-task-dotenv
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
version: '3'
2+
3+
silent: true
4+
dotenv:
5+
- 'global.env'
6+
vars:
7+
VAR: entrypoint-global-vars
8+
env:
9+
ENV: entrypoint-global-vars
10+
11+
tasks:
12+
default:
13+
dotenv:
14+
- 'task.env'
15+
vars:
16+
VAR: entrypoint-task-vars
17+
env:
18+
ENV: entrypoint-task-vars
19+
cmds:
20+
- 'echo "{{.VAR}}"'
21+
- 'echo "$ENV"'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
VAR=entrypoint-global-dotenv
2+
ENV=entrypoint-global-dotenv
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
VAR=entrypoint-task-dotenv
2+
ENV=entrypoint-task-dotenv
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
version: '3'
2+
3+
silent: true
4+
dotenv:
5+
- 'global.env'
6+
vars:
7+
VAR: entrypoint-global-vars
8+
env:
9+
ENV: entrypoint-global-vars
10+
11+
includes:
12+
included: included.yml
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
VAR=entrypoint-global-dotenv
2+
ENV=entrypoint-global-dotenv
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
version: '3'
2+
3+
silent: true
4+
vars:
5+
VAR: included-global-vars
6+
env:
7+
ENV: included-global-vars
8+
9+
tasks:
10+
default:
11+
cmds:
12+
- 'echo "{{.VAR}}"'
13+
- 'echo "$ENV"'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
version: '3'
2+
3+
silent: true
4+
dotenv:
5+
- 'global.env'
6+
vars:
7+
VAR: entrypoint-global-vars
8+
env:
9+
ENV: entrypoint-global-vars
10+
11+
includes:
12+
included: included.yml
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
VAR=entrypoint-global-dotenv
2+
ENV=entrypoint-global-dotenv
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
version: '3'
2+
3+
silent: true
4+
vars:
5+
VAR: included-global-vars
6+
env:
7+
ENV: included-global-vars
8+
9+
tasks:
10+
default:
11+
dotenv:
12+
- 'task.env'
13+
cmds:
14+
- task: called-task
15+
vars:
16+
VAR: included-task-call-vars
17+
18+
called-task:
19+
cmds:
20+
- 'echo "{{.VAR}}"'
21+
- 'echo "$ENV"'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
VAR=included-task-dotenv
2+
ENV=included-task-dotenv
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
version: '3'
2+
3+
silent: true
4+
dotenv:
5+
- 'global.env'
6+
vars:
7+
VAR: entrypoint-global-vars
8+
env:
9+
ENV: entrypoint-global-vars
10+
11+
includes:
12+
included: included.yml

0 commit comments

Comments
 (0)