From 2a1516ee9aad9f885242e0c9990001541eca2577 Mon Sep 17 00:00:00 2001 From: nxtcoder17 Date: Sun, 20 Oct 2024 10:00:00 +0530 Subject: [PATCH] feat: adds support for global dot-env files --- pkg/runfile/task-parser.go | 10 ++++++ pkg/runfile/task-parser_test.go | 57 +++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/pkg/runfile/task-parser.go b/pkg/runfile/task-parser.go index 30f1d0c..1a23635 100644 --- a/pkg/runfile/task-parser.go +++ b/pkg/runfile/task-parser.go @@ -36,6 +36,16 @@ func ParseTask(ctx Context, rf *Runfile, task Task) (*ParsedTask, *Error) { } } + if rf.DotEnv != nil { + m, err := parseDotEnvFiles(rf.DotEnv...) + if err != nil { + return nil, err + } + for k, v := range m { + globalEnv[k] = v + } + } + for _, requirement := range task.Requires { if requirement == nil { continue diff --git a/pkg/runfile/task-parser_test.go b/pkg/runfile/task-parser_test.go index 40a75da..e00e246 100644 --- a/pkg/runfile/task-parser_test.go +++ b/pkg/runfile/task-parser_test.go @@ -792,9 +792,66 @@ echo "hi" }, } + testGlobalDotEnv := []test{ + { + name: "1. testing global env key-value item", + args: args{ + ctx: nil, + rf: &Runfile{ + DotEnv: []string{ + dotenvTestFile.Name(), + }, + Tasks: map[string]Task{ + "test": { + ignoreSystemEnv: true, + Commands: []any{ + "echo hi", + }, + }, + }, + }, + taskName: "test", + }, + want: &ParsedTask{ + Shell: []string{"sh", "-c"}, + WorkingDir: fn.Must(os.Getwd()), + Env: map[string]string{ + "hello": "world", + }, + Commands: []CommandJson{ + {Command: "echo hi"}, + }, + }, + wantErr: false, + }, + { + name: "2. fails when dotenv file not found", + args: args{ + ctx: nil, + rf: &Runfile{ + DotEnv: []string{ + dotenvTestFile.Name() + "2", + }, + Tasks: map[string]Task{ + "test": { + ignoreSystemEnv: true, + Commands: []any{ + "echo hi", + }, + }, + }, + }, + taskName: "test", + }, + want: nil, + wantErr: true, + }, + } + tests = append(tests, testRequires...) tests = append(tests, testEnviroments...) tests = append(tests, testGlobalEnvVars...) + tests = append(tests, testGlobalDotEnv...) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {