diff --git a/fixtures/valid.env b/fixtures/valid.env new file mode 100644 index 0000000..6ac867a --- /dev/null +++ b/fixtures/valid.env @@ -0,0 +1 @@ +FOO=BAR diff --git a/godotenv.go b/godotenv.go index 975d7bc..e624874 100644 --- a/godotenv.go +++ b/godotenv.go @@ -137,7 +137,10 @@ func Unmarshal(str string) (envMap map[string]string, err error) { // If you want more fine grained control over your command it's recommended // that you use `Load()` or `Read()` and the `os/exec` package yourself. func Exec(filenames []string, cmd string, cmdArgs []string) error { - Load(filenames...) + err := Load(filenames...) + if err != nil { + return err + } command := exec.Command(cmd, cmdArgs...) command.Stdin = os.Stdin diff --git a/godotenv_test.go b/godotenv_test.go index 49f6fe0..1930533 100644 --- a/godotenv_test.go +++ b/godotenv_test.go @@ -2,10 +2,12 @@ package godotenv import ( "bytes" + "errors" "fmt" "os" "reflect" "strings" + "syscall" "testing" ) @@ -472,3 +474,11 @@ func TestRoundtrip(t *testing.T) { } } + +func TestExecWhenFileNotFound(t *testing.T) { + fileNames := []string{"not-existed-file-name.env"} + err := Exec(fileNames, "", []string{}) + if !errors.Is(err, syscall.ERROR_FILE_NOT_FOUND) { + t.Error("Expected: return error when system cannot find the file specified") + } +}