Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pkg/runfile/task-parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ func ParseTask(ctx Context, rf *Runfile, task Task) (*ParsedTask, *Error) {
}
}

if rf.DotEnv != nil {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (performance): Add a check for empty rf.DotEnv slice to avoid unnecessary function calls.

Consider adding a check for len(rf.DotEnv) > 0 before calling parseDotEnvFiles to avoid unnecessary function calls when the slice is empty.

Suggested change
if rf.DotEnv != nil {
if rf.DotEnv != nil && len(rf.DotEnv) > 0 {

m, err := parseDotEnvFiles(rf.DotEnv...)
if err != nil {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Add context to the returned error for better debugging.

When returning the error from parseDotEnvFiles, consider wrapping it with additional context using fmt.Errorf or a custom error type. This will make it easier to trace the source of the error during debugging.

Suggested change
if err != nil {
if err != nil {
return nil, fmt.Errorf("failed to parse .env files: %w", err)

return nil, err
}
for k, v := range m {
globalEnv[k] = v
}
}

for _, requirement := range task.Requires {
if requirement == nil {
continue
Expand Down
57 changes: 57 additions & 0 deletions pkg/runfile/task-parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down