1
0
mirror of https://github.com/go-task/task.git synced 2025-03-21 21:27:07 +02:00

Merge pull request from chris-garrett/dev/378-missing-env

Resolves  - allow for missing env files as they may be bootstrapped.
This commit is contained in:
Andrey Nering 2020-10-12 21:12:08 -03:00 committed by GitHub
commit 582a66bb2f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 30 additions and 31 deletions
task_test.go
taskfile/read
testdata/dotenv
default
error_included_envs
missing_env

@ -829,7 +829,7 @@ func TestShortTaskNotation(t *testing.T) {
func TestDotenvShouldIncludeAllEnvFiles(t *testing.T) { func TestDotenvShouldIncludeAllEnvFiles(t *testing.T) {
tt := fileContentTest{ tt := fileContentTest{
Dir: "testdata/dotenv", Dir: "testdata/dotenv/default",
Target: "default", Target: "default",
TrimSpace: false, TrimSpace: false,
Files: map[string]string{ Files: map[string]string{
@ -839,26 +839,9 @@ func TestDotenvShouldIncludeAllEnvFiles(t *testing.T) {
tt.Run(t) tt.Run(t)
} }
func TestDotenvShouldErrorWithIncludeEnvPath(t *testing.T) {
const dir = "testdata/dotenv"
const entry = "Taskfile-errors1.yml"
var buff bytes.Buffer
e := task.Executor{
Dir: dir,
Entrypoint: entry,
Summary: true,
Stdout: &buff,
Stderr: &buff,
}
err := e.Setup()
assert.Error(t, err)
assert.Contains(t, err.Error(), "no such file")
}
func TestDotenvShouldErrorWhenIncludingDependantDotenvs(t *testing.T) { func TestDotenvShouldErrorWhenIncludingDependantDotenvs(t *testing.T) {
const dir = "testdata/dotenv" const dir = "testdata/dotenv/error_included_envs"
const entry = "Taskfile-errors2.yml" const entry = "Taskfile.yml"
var buff bytes.Buffer var buff bytes.Buffer
e := task.Executor{ e := task.Executor{
@ -873,3 +856,15 @@ func TestDotenvShouldErrorWhenIncludingDependantDotenvs(t *testing.T) {
assert.Error(t, err) assert.Error(t, err)
assert.Contains(t, err.Error(), "move the dotenv") assert.Contains(t, err.Error(), "move the dotenv")
} }
func TestDotenvShouldAllowMissingEnv(t *testing.T) {
tt := fileContentTest{
Dir: "testdata/dotenv/missing_env",
Target: "default",
TrimSpace: false,
Files: map[string]string{
"include.txt": "INCLUDE1='' INCLUDE2=''\n",
},
}
tt.Run(t)
}

@ -42,14 +42,18 @@ func Taskfile(dir string, entrypoint string) (*taskfile.Taskfile, error) {
if !filepath.IsAbs(dotEnvPath) { if !filepath.IsAbs(dotEnvPath) {
dotEnvPath = filepath.Join(dir, dotEnvPath) dotEnvPath = filepath.Join(dir, dotEnvPath)
} }
envs, err := godotenv.Read(dotEnvPath) // allow for missing env files since they may be created by a bootstrap task
if err != nil { if _, err := os.Stat(dotEnvPath); !os.IsNotExist(err) {
return nil, err envs, err := godotenv.Read(dotEnvPath)
} if err != nil {
for key, value := range envs { return nil, err
if _, ok := t.Env.Mapping[key]; !ok {
t.Env.Set(key, taskfile.Var{Static: value})
} }
for key, value := range envs {
if _, ok := t.Env.Mapping[key]; !ok {
t.Env.Set(key, taskfile.Var{Static: value})
}
}
} else {
} }
} }
} }

@ -1,8 +1,8 @@
version: '3' version: '3'
dotenv: ['include1/.env', 'include1/envs/.env', 'file-does-not-exist'] dotenv: ['../include1/.env', '../include1/envs/.env']
tasks: tasks:
default: default:
cmds: cmds:
- echo "INCLUDE1='$INCLUDE1' INCLUDE2='$INCLUDE2'" > include-errors1.txt - echo "INCLUDE1='$INCLUDE1' INCLUDE2='$INCLUDE2'" > include.txt

@ -1,7 +1,7 @@
version: '3' version: '3'
includes: includes:
include1: './include1' include1: '../include1'
tasks: tasks:
default: default:

@ -1,6 +1,6 @@
version: '3' version: '3'
dotenv: ['include1/.env', 'include1/envs/.env'] dotenv: ['.env']
tasks: tasks:
default: default: