Skip to content

Commit aadc9f8

Browse files
committed
fix test s
1 parent 0c338ca commit aadc9f8

2 files changed

Lines changed: 33 additions & 14 deletions

File tree

internal/runtime/env_test.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ import (
1010
"testing"
1111

1212
"github.com/dagu-org/dagu/internal/common/collections"
13+
"github.com/dagu-org/dagu/internal/common/config"
1314
"github.com/dagu-org/dagu/internal/core"
1415
"github.com/dagu-org/dagu/internal/core/execution"
1516
"github.com/dagu-org/dagu/internal/runtime"
1617
"github.com/stretchr/testify/assert"
1718
"github.com/stretchr/testify/require"
1819
)
1920

20-
func TestEnv_VariablesMap(t *testing.T) {
21+
func TestEnv_AllEnvsMap(t *testing.T) {
2122
t.Parallel()
2223

2324
tests := []struct {
@@ -43,14 +44,16 @@ func TestEnv_VariablesMap(t *testing.T) {
4344
},
4445
},
4546
{
46-
name: "EnvsOverrideVariables",
47+
name: "VariablesOverrideEnvs",
4748
setupEnv: func(env runtime.Env) runtime.Env {
4849
env.Variables.Store("SAME_KEY", "SAME_KEY=from_variables")
4950
env.Envs["SAME_KEY"] = "from_envs"
5051
return env
5152
},
5253
expected: map[string]string{
53-
"SAME_KEY": "from_envs",
54+
// Variables (output from previous steps) are added last in AllEnvs(),
55+
// so they override Envs when there's a key conflict
56+
"SAME_KEY": "from_variables",
5457
execution.EnvKeyDAGRunStepName: "test-step",
5558
},
5659
},
@@ -98,20 +101,24 @@ func TestEnv_VariablesMap(t *testing.T) {
98101
// Create a temporary directory to use as DAG working directory
99102
tempDir := t.TempDir()
100103

101-
// Set up DAG context with WorkingDir
104+
// Set up DAG context with WorkingDir and BaseEnv
102105
dag := &core.DAG{
103106
Name: "test-dag",
104107
WorkingDir: tempDir,
105108
}
109+
baseEnv := config.NewBaseEnv(nil)
106110
dagCtx := execution.DAGContext{
107-
DAG: dag,
111+
DAG: dag,
112+
BaseEnv: &baseEnv,
108113
}
109114
ctx := execution.WithDAGContext(context.Background(), dagCtx)
110115

111116
env := runtime.NewEnvForStep(ctx, core.Step{Name: "test-step"})
112117
env = tt.setupEnv(env)
113118

114-
result := env.VariablesMap()
119+
// Use WithEnv to set the env in context, then call AllEnvsMap
120+
ctx = runtime.WithEnv(ctx, env)
121+
result := runtime.AllEnvsMap(ctx)
115122

116123
// Check that all expected keys exist with correct values
117124
for key, expectedValue := range tt.expected {

internal/runtime/eval_test.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"testing"
66

7+
"github.com/dagu-org/dagu/internal/common/config"
78
"github.com/dagu-org/dagu/internal/core"
89
"github.com/dagu-org/dagu/internal/core/execution"
910
"github.com/dagu-org/dagu/internal/runtime"
@@ -13,6 +14,17 @@ import (
1314
// Note: These tests use execution.SetupDAGContext to set up the DAG context,
1415
// then use runtime.NewEnvForStep/GetEnv/WithEnv to manage the runtime Env.
1516

17+
// setupTestContext creates a test context with properly initialized DAGContext
18+
func setupTestContext() context.Context {
19+
ctx := context.Background()
20+
baseEnv := config.NewBaseEnv(nil)
21+
dagCtx := execution.DAGContext{
22+
DAG: &core.DAG{Name: "test-dag"},
23+
BaseEnv: &baseEnv,
24+
}
25+
return execution.WithDAGContext(ctx, dagCtx)
26+
}
27+
1628
func TestEvalString(t *testing.T) {
1729
t.Parallel()
1830

@@ -157,8 +169,8 @@ type NestedStruct struct {
157169
}
158170

159171
func TestEvalObject(t *testing.T) {
160-
// Create a test context with environment variables
161-
ctx := context.Background()
172+
// Create a test context with DAGContext and environment variables
173+
ctx := setupTestContext()
162174
env := runtime.NewEnvForStep(ctx, core.Step{Name: "test-step"})
163175
env.Variables.Store("NAME_VAR", "NAME_VAR=John")
164176
env.Variables.Store("DESC_VAR", "DESC_VAR=Developer")
@@ -199,8 +211,8 @@ func TestEvalObject(t *testing.T) {
199211
func TestEvalObjectWithExecutorConfig(t *testing.T) {
200212
t.Parallel()
201213

202-
// Create a test context with environment variables
203-
ctx := context.Background()
214+
// Create a test context with DAGContext and environment variables
215+
ctx := setupTestContext()
204216
env := runtime.NewEnvForStep(ctx, core.Step{Name: "test-step"})
205217
env.Variables.Store("EXECUTOR_TYPE", "EXECUTOR_TYPE=docker")
206218
env.Variables.Store("HOST_VAR", "HOST_VAR=localhost")
@@ -308,8 +320,8 @@ func TestGenerateSubDAGRunID(t *testing.T) {
308320
func TestEvalObjectWithComplexNestedStructures(t *testing.T) {
309321
t.Parallel()
310322

311-
// Create a test context with environment variables
312-
ctx := context.Background()
323+
// Create a test context with DAGContext and environment variables
324+
ctx := setupTestContext()
313325
env := runtime.NewEnvForStep(ctx, core.Step{Name: "test-step"})
314326
env.Variables.Store("VAR1", "VAR1=value1")
315327
env.Variables.Store("VAR2", "VAR2=value2")
@@ -534,8 +546,8 @@ func TestEvalStringEdgeCases(t *testing.T) {
534546
}
535547

536548
func TestEvalObjectWithDirectStringEvaluation(t *testing.T) {
537-
// Create a test context with environment variables
538-
ctx := context.Background()
549+
// Create a test context with DAGContext and environment variables
550+
ctx := setupTestContext()
539551
env := runtime.NewEnvForStep(ctx, core.Step{Name: "test-step"})
540552
env.Variables.Store("STRING_VAR", "STRING_VAR=evaluated_string")
541553
env.Variables.Store("PATH_VAR", "PATH_VAR=/path/to/file")

0 commit comments

Comments
 (0)