Skip to content
This repository was archived by the owner on Feb 16, 2023. It is now read-only.

Commit fc7c6cb

Browse files
committed
Add tests for non-existent default env file and refactor error
1 parent fb71da6 commit fc7c6cb

2 files changed

Lines changed: 15 additions & 3 deletions

File tree

internals/secrethub/run.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ var (
3737
ErrSignalFailed = errRun.Code("signal_failed").ErrorPref("error while propagating signal to process: %s")
3838
ErrReadEnvDir = errRun.Code("env_dir_read_error").ErrorPref("could not read the environment directory: %s")
3939
ErrReadEnvFile = errRun.Code("env_file_read_error").ErrorPref("could not read the environment file %s: %s")
40+
ErrReadDefaultEnvFile = errRun.Code("default_env_file_read_error").ErrorPref("could not read default run env-file %s: %s")
4041
ErrEnvDirNotFound = errRun.Code("env_dir_not_found").Error(fmt.Sprintf("could not find specified environment. Make sure you have executed `%s set`.", ApplicationName))
4142
ErrTemplate = errRun.Code("invalid_template").ErrorPref("could not parse template at line %d: %s")
4243
ErrParsingTemplate = errRun.Code("template_parsing_failed").ErrorPref("error while processing template file '%s': %s")
@@ -45,7 +46,8 @@ var (
4546
)
4647

4748
const (
48-
maskString = "<redacted by SecretHub>"
49+
defaultEnvFile = "secrethub.env"
50+
maskString = "<redacted by SecretHub>"
4951
// templateVarEnvVarPrefix is used to prefix environment variables
5052
// that should be used as template variables.
5153
templateVarEnvVarPrefix = "SECRETHUB_VAR_"
@@ -224,12 +226,11 @@ func (cmd *RunCommand) sourceEnvironment() ([]string, []string, error) {
224226
envSources = append(envSources, flagSource)
225227

226228
if cmd.envFile == "" {
227-
const defaultEnvFile = "secrethub.env"
228229
_, err := cmd.osStat(defaultEnvFile)
229230
if err == nil {
230231
cmd.envFile = defaultEnvFile
231232
} else if !os.IsNotExist(err) {
232-
return nil, nil, fmt.Errorf("could not read default run env-file %s: %s", defaultEnvFile, err)
233+
return nil, nil, ErrReadDefaultEnvFile(defaultEnvFile, err)
233234
}
234235
}
235236

internals/secrethub/run_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,17 @@ func TestRunCommand_environment(t *testing.T) {
648648
},
649649
err: ErrParsingTemplate("secrethub.env", "template syntax error at 1:23: expected the closing of a secret tag `}}`, but reached the end of the template. (template.secret_tag_not_closed) "),
650650
},
651+
"default env file does not exist": {
652+
command: RunCommand{
653+
osStat: osStatFuncFromMap(nil),
654+
},
655+
},
656+
"default env file exists but cannot be read": {
657+
command: RunCommand{
658+
osStat: osStatFuncFromMap(map[string]error{"secrethub.env": os.ErrPermission}),
659+
},
660+
err: ErrReadDefaultEnvFile(defaultEnvFile, os.ErrPermission),
661+
},
651662
"env file secret does not exist": {
652663
command: RunCommand{
653664
command: []string{"echo", "test"},

0 commit comments

Comments
 (0)