Skip to content

Commit c6138a0

Browse files
committed
#378 - allow for missing env files as they may be bootstrapped.
1 parent ce4ac97 commit c6138a0

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
@@ -819,7 +819,7 @@ func TestShortTaskNotation(t *testing.T) {
819819

820820
func TestDotenvShouldIncludeAllEnvFiles(t *testing.T) {
821821
tt := fileContentTest{
822-
Dir: "testdata/dotenv",
822+
Dir: "testdata/dotenv/default",
823823
Target: "default",
824824
TrimSpace: false,
825825
Files: map[string]string{
@@ -829,26 +829,9 @@ func TestDotenvShouldIncludeAllEnvFiles(t *testing.T) {
829829
tt.Run(t)
830830
}
831831

832-
func TestDotenvShouldErrorWithIncludeEnvPath(t *testing.T) {
833-
const dir = "testdata/dotenv"
834-
const entry = "Taskfile-errors1.yml"
835-
836-
var buff bytes.Buffer
837-
e := task.Executor{
838-
Dir: dir,
839-
Entrypoint: entry,
840-
Summary: true,
841-
Stdout: &buff,
842-
Stderr: &buff,
843-
}
844-
err := e.Setup()
845-
assert.Error(t, err)
846-
assert.Contains(t, err.Error(), "no such file")
847-
}
848-
849832
func TestDotenvShouldErrorWhenIncludingDependantDotenvs(t *testing.T) {
850-
const dir = "testdata/dotenv"
851-
const entry = "Taskfile-errors2.yml"
833+
const dir = "testdata/dotenv/error_included_envs"
834+
const entry = "Taskfile.yml"
852835

853836
var buff bytes.Buffer
854837
e := task.Executor{
@@ -863,3 +846,15 @@ func TestDotenvShouldErrorWhenIncludingDependantDotenvs(t *testing.T) {
863846
assert.Error(t, err)
864847
assert.Contains(t, err.Error(), "move the dotenv")
865848
}
849+
850+
func TestDotenvShouldAllowMissingEnv(t *testing.T) {
851+
tt := fileContentTest{
852+
Dir: "testdata/dotenv/missing_env",
853+
Target: "default",
854+
TrimSpace: false,
855+
Files: map[string]string{
856+
"include.txt": "INCLUDE1='' INCLUDE2=''\n",
857+
},
858+
}
859+
tt.Run(t)
860+
}

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)