Fixed Issue 175: Command does not load dotenv files if some do not exist

This commit is contained in:
Elchin Gasimov 2022-06-11 19:54:34 +04:00
parent e74c6cadd5
commit 68ecc23126
3 changed files with 15 additions and 1 deletions

1
fixtures/valid.env Normal file
View File

@ -0,0 +1 @@
FOO=BAR

View File

@ -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

View File

@ -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")
}
}