Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Issue 175: Command does not load dotenv files if some do not exist [1] #181

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions fixtures/valid.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FOO=BAR
5 changes: 4 additions & 1 deletion godotenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions godotenv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package godotenv

import (
"bytes"
"errors"
"fmt"
"os"
"reflect"
"strings"
"syscall"
"testing"
)

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