Skip to content

Commit 582a66b

Browse files
authored
Merge pull request #385 from chris-garrett/dev/378-missing-env
Resolves #378 - allow for missing env files as they may be bootstrapped.
2 parents d78f78b + c6138a0 commit 582a66b

File tree

5 files changed

+30
-31
lines changed

5 files changed

+30
-31
lines changed

task_test.go

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,7 @@ func TestShortTaskNotation(t *testing.T) {
829829

830830
func TestDotenvShouldIncludeAllEnvFiles(t *testing.T) {
831831
tt := fileContentTest{
832-
Dir: "testdata/dotenv",
832+
Dir: "testdata/dotenv/default",
833833
Target: "default",
834834
TrimSpace: false,
835835
Files: map[string]string{
@@ -839,26 +839,9 @@ func TestDotenvShouldIncludeAllEnvFiles(t *testing.T) {
839839
tt.Run(t)
840840
}
841841

842-
func TestDotenvShouldErrorWithIncludeEnvPath(t *testing.T) {
843-
const dir = "testdata/dotenv"
844-
const entry = "Taskfile-errors1.yml"
845-
846-
var buff bytes.Buffer
847-
e := task.Executor{
848-
Dir: dir,
849-
Entrypoint: entry,
850-
Summary: true,
851-
Stdout: &buff,
852-
Stderr: &buff,
853-
}
854-
err := e.Setup()
855-
assert.Error(t, err)
856-
assert.Contains(t, err.Error(), "no such file")
857-
}
858-
859842
func TestDotenvShouldErrorWhenIncludingDependantDotenvs(t *testing.T) {
860-
const dir = "testdata/dotenv"
861-
const entry = "Taskfile-errors2.yml"
843+
const dir = "testdata/dotenv/error_included_envs"
844+
const entry = "Taskfile.yml"
862845

863846
var buff bytes.Buffer
864847
e := task.Executor{
@@ -873,3 +856,15 @@ func TestDotenvShouldErrorWhenIncludingDependantDotenvs(t *testing.T) {
873856
assert.Error(t, err)
874857
assert.Contains(t, err.Error(), "move the dotenv")
875858
}
859+
860+
func TestDotenvShouldAllowMissingEnv(t *testing.T) {
861+
tt := fileContentTest{
862+
Dir: "testdata/dotenv/missing_env",
863+
Target: "default",
864+
TrimSpace: false,
865+
Files: map[string]string{
866+
"include.txt": "INCLUDE1='' INCLUDE2=''\n",
867+
},
868+
}
869+
tt.Run(t)
870+
}

taskfile/read/taskfile.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,18 @@ func Taskfile(dir string, entrypoint string) (*taskfile.Taskfile, error) {
4242
if !filepath.IsAbs(dotEnvPath) {
4343
dotEnvPath = filepath.Join(dir, dotEnvPath)
4444
}
45-
envs, err := godotenv.Read(dotEnvPath)
46-
if err != nil {
47-
return nil, err
48-
}
49-
for key, value := range envs {
50-
if _, ok := t.Env.Mapping[key]; !ok {
51-
t.Env.Set(key, taskfile.Var{Static: value})
45+
// allow for missing env files since they may be created by a bootstrap task
46+
if _, err := os.Stat(dotEnvPath); !os.IsNotExist(err) {
47+
envs, err := godotenv.Read(dotEnvPath)
48+
if err != nil {
49+
return nil, err
50+
}
51+
for key, value := range envs {
52+
if _, ok := t.Env.Mapping[key]; !ok {
53+
t.Env.Set(key, taskfile.Var{Static: value})
54+
}
5255
}
56+
} else {
5357
}
5458
}
5559
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
version: '3'
22

3-
dotenv: ['include1/.env', 'include1/envs/.env', 'file-does-not-exist']
3+
dotenv: ['../include1/.env', '../include1/envs/.env']
44

55
tasks:
66
default:
77
cmds:
8-
- echo "INCLUDE1='$INCLUDE1' INCLUDE2='$INCLUDE2'" > include-errors1.txt
8+
- echo "INCLUDE1='$INCLUDE1' INCLUDE2='$INCLUDE2'" > include.txt

testdata/dotenv/Taskfile-errors2.yml renamed to testdata/dotenv/error_included_envs/Taskfile.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
version: '3'
22

33
includes:
4-
include1: './include1'
4+
include1: '../include1'
55

66
tasks:
77
default:

testdata/dotenv/Taskfile.yml renamed to testdata/dotenv/missing_env/Taskfile.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
version: '3'
22

3-
dotenv: ['include1/.env', 'include1/envs/.env']
3+
dotenv: ['.env']
44

55
tasks:
66
default:

0 commit comments

Comments
 (0)