diff options
author | Valery Piashchynski <[email protected]> | 2022-01-20 23:39:56 +0300 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2022-01-20 23:39:56 +0300 |
commit | 97e992236a7249bf0a8b7fa815c744d92dde4089 (patch) | |
tree | 239faaa1c5df93016040df7c911a4d66579e9fee | |
parent | cf8037d3d87d20f94945b3988e1e7cfc98583ae5 (diff) |
error on missing env if used
Signed-off-by: Valery Piashchynski <[email protected]>
-rw-r--r-- | internal/cli/root.go | 5 | ||||
-rw-r--r-- | internal/cli/root_test.go | 51 |
2 files changed, 55 insertions, 1 deletions
diff --git a/internal/cli/root.go b/internal/cli/root.go index 29fd14b8..723e8a01 100644 --- a/internal/cli/root.go +++ b/internal/cli/root.go @@ -60,7 +60,10 @@ func NewCommand(cmdName string) *cobra.Command { //nolint:funlen } if dotenv != "" { - _ = godotenv.Load(dotenv) // error ignored because dotenv is optional feature + err := godotenv.Load(dotenv) + if err != nil { + return err + } } cfg := &config.Plugin{Path: cfgFile, Prefix: "rr", Flags: override} diff --git a/internal/cli/root_test.go b/internal/cli/root_test.go index 28e7d237..806669f7 100644 --- a/internal/cli/root_test.go +++ b/internal/cli/root_test.go @@ -1,9 +1,12 @@ package cli_test import ( + "os" + "path" "testing" "github.com/roadrunner-server/roadrunner/v2/internal/cli" + "github.com/stretchr/testify/require" "github.com/spf13/cobra" "github.com/stretchr/testify/assert" @@ -83,3 +86,51 @@ func TestCommandSimpleExecuting(t *testing.T) { assert.NoError(t, cmd.Execute()) assert.True(t, executed) } + +func TestCommandNoEnvFileError(t *testing.T) { + cmd := cli.NewCommand("unit test") + cmd.SetArgs([]string{"-c", "./../../.rr.yaml", "--dotenv", "foo/bar"}) + + var executed bool + + if cmd.Run == nil { // override "Run" property for test (if it was not set) + cmd.Run = func(cmd *cobra.Command, args []string) { + executed = true + } + } + + assert.Error(t, cmd.Execute()) + assert.False(t, executed) +} + +func TestCommandNoEnvFileNoError(t *testing.T) { + tmp := os.TempDir() + + cmd := cli.NewCommand("unit test") + cmd.SetArgs([]string{"-c", path.Join(tmp, ".rr.yaml"), "--dotenv", path.Join(tmp, ".env")}) + + var executed bool + + f, err := os.Create(path.Join(tmp, ".env")) + require.NoError(t, err) + f2, err := os.Create(path.Join(tmp, ".rr.yaml")) + require.NoError(t, err) + defer func() { + _ = f.Close() + _ = f2.Close() + }() + + if cmd.Run == nil { // override "Run" property for test (if it was not set) + cmd.Run = func(cmd *cobra.Command, args []string) { + executed = true + } + } + + assert.NoError(t, cmd.Execute()) + assert.True(t, executed) + + t.Cleanup(func() { + _ = os.RemoveAll(path.Join(tmp, ".env")) + _ = os.RemoveAll(path.Join(tmp, ".rr.yaml")) + }) +} |