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

Commit e3c552f

Browse files
committed
Refactor readFile and osStat functions used when testing
1 parent d008233 commit e3c552f

1 file changed

Lines changed: 33 additions & 33 deletions

File tree

internals/secrethub/run_test.go

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -613,18 +613,18 @@ func TestRunCommand_Run(t *testing.T) {
613613
}
614614
}
615615

616-
func readFileFuncFromMap(files map[string]string) func(string) ([]byte, error) {
616+
func readFileFunc(name string, content string) func(string) ([]byte, error) {
617617
return func(filename string) ([]byte, error) {
618-
if data, ok := files[filename]; ok {
619-
return []byte(data), nil
618+
if filename == name {
619+
return []byte(content), nil
620620
}
621621
return nil, os.ErrNotExist
622622
}
623623
}
624624

625-
func osStatFuncFromMap(errs map[string]error) func(string) (os.FileInfo, error) {
625+
func osStatFunc(name string, err error) func(string) (os.FileInfo, error) {
626626
return func(filename string) (os.FileInfo, error) {
627-
if err, ok := errs[filename]; ok {
627+
if name == filename {
628628
return nil, err
629629
}
630630
return nil, os.ErrNotExist
@@ -641,21 +641,21 @@ func TestRunCommand_environment(t *testing.T) {
641641
"invalid template syntax": {
642642
command: RunCommand{
643643
command: []string{"echo", "test"},
644-
readFile: readFileFuncFromMap(map[string]string{"secrethub.env": "TEST={{path/to/secret}"}),
645-
osStat: osStatFuncFromMap(map[string]error{"secrethub.env": nil}),
644+
readFile: readFileFunc("secrethub.env", "TEST={{path/to/secret}"),
645+
osStat: osStatFunc("secrethub.env", nil),
646646
envFile: "secrethub.env",
647647
templateVersion: "2",
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
},
651651
"default env file does not exist": {
652652
command: RunCommand{
653-
osStat: osStatFuncFromMap(nil),
653+
osStat: osStatFunc("secrethub.env", os.ErrNotExist),
654654
},
655655
},
656656
"default env file exists but cannot be read": {
657657
command: RunCommand{
658-
osStat: osStatFuncFromMap(map[string]error{"secrethub.env": os.ErrPermission}),
658+
osStat: osStatFunc("secrethub.env", os.ErrPermission),
659659
},
660660
err: ErrReadDefaultEnvFile(defaultEnvFile, os.ErrPermission),
661661
},
@@ -675,16 +675,16 @@ func TestRunCommand_environment(t *testing.T) {
675675
command: RunCommand{
676676
envFile: "foo.env",
677677
templateVersion: "2",
678-
osStat: osStatFuncFromMap(map[string]error{"foo.env": nil}),
679-
readFile: readFileFuncFromMap(map[string]string{"foo.env": "TEST=test"}),
678+
osStat: osStatFunc("foo.env", nil),
679+
readFile: readFileFunc("foo.env", "TEST=test"),
680680
},
681681
expectedEnv: []string{"TEST=test"},
682682
},
683683
"env file secret does not exist": {
684684
command: RunCommand{
685685
command: []string{"echo", "test"},
686-
readFile: readFileFuncFromMap(map[string]string{"secrethub.env": "TEST= {{ unexistent/secret/path }}"}),
687-
osStat: osStatFuncFromMap(map[string]error{"secrethub.env": nil}),
686+
readFile: readFileFunc("secrethub.env", "TEST= {{ unexistent/secret/path }}"),
687+
osStat: osStatFunc("secrethub.env", nil),
688688
envFile: "secrethub.env",
689689
templateVersion: "2",
690690
newClient: func() (secrethub.ClientInterface, error) {
@@ -703,8 +703,8 @@ func TestRunCommand_environment(t *testing.T) {
703703
},
704704
"envar flag has precedence over env file": {
705705
command: RunCommand{
706-
readFile: readFileFuncFromMap(map[string]string{"secrethub.env": "TEST=aaa"}),
707-
osStat: osStatFuncFromMap(map[string]error{"secrethub.env": nil}),
706+
readFile: readFileFunc("secrethub.env", "TEST=aaa"),
707+
osStat: osStatFunc("secrethub.env", nil),
708708
envFile: "secrethub.env",
709709
envar: map[string]string{
710710
"TEST": "test/test/test",
@@ -728,8 +728,8 @@ func TestRunCommand_environment(t *testing.T) {
728728
// TODO Add test case for: envar flag has precedence over secret reference - requires refactoring of fakeclient
729729
"secret reference has precedence over .env file": {
730730
command: RunCommand{
731-
readFile: readFileFuncFromMap(map[string]string{"secrethub.env": "TEST=aaa"}),
732-
osStat: osStatFuncFromMap(map[string]error{"secrethub.env": nil}),
731+
readFile: readFileFunc("secrethub.env", "TEST=aaa"),
732+
osStat: osStatFunc("secrethub.env", nil),
733733
dontPromptMissingTemplateVar: true,
734734
templateVersion: "2",
735735
osEnv: []string{"TEST=secrethub://test/test/test"},
@@ -750,8 +750,8 @@ func TestRunCommand_environment(t *testing.T) {
750750
},
751751
".env file has precedence over other os variables": {
752752
command: RunCommand{
753-
readFile: readFileFuncFromMap(map[string]string{"secrethub.env": "TEST=aaa"}),
754-
osStat: osStatFuncFromMap(map[string]error{"secrethub.env": nil}),
753+
readFile: readFileFunc("secrethub.env", "TEST=aaa"),
754+
osStat: osStatFunc("secrethub.env", nil),
755755
dontPromptMissingTemplateVar: true,
756756
templateVersion: "2",
757757
osEnv: []string{"TEST=bbb"},
@@ -761,8 +761,8 @@ func TestRunCommand_environment(t *testing.T) {
761761
},
762762
".env file secret has precedence over other os variables": {
763763
command: RunCommand{
764-
readFile: readFileFuncFromMap(map[string]string{"secrethub.env": "TEST={{path/to/secret}}"}),
765-
osStat: osStatFuncFromMap(map[string]error{"secrethub.env": nil}),
764+
readFile: readFileFunc("secrethub.env", "TEST={{path/to/secret}}"),
765+
osStat: osStatFunc("secrethub.env", nil),
766766
dontPromptMissingTemplateVar: true,
767767
templateVersion: "2",
768768
osEnv: []string{"TEST=bbb"},
@@ -785,8 +785,8 @@ func TestRunCommand_environment(t *testing.T) {
785785
command: RunCommand{
786786
ignoreMissingSecrets: true,
787787
envFile: "secrethub.env",
788-
readFile: readFileFuncFromMap(map[string]string{"secrethub.env": ""}),
789-
osStat: osStatFuncFromMap(map[string]error{"secrethub.env": nil}),
788+
readFile: readFileFunc("secrethub.env", ""),
789+
osStat: osStatFunc("secrethub.env", nil),
790790
envar: map[string]string{
791791
"TEST": "test/test/test",
792792
},
@@ -808,8 +808,8 @@ func TestRunCommand_environment(t *testing.T) {
808808
},
809809
"--no-prompt": {
810810
command: RunCommand{
811-
readFile: readFileFuncFromMap(map[string]string{"secrethub.env": "TEST = {{ test/$variable/test }}"}),
812-
osStat: osStatFuncFromMap(map[string]error{"secrethub.env": nil}),
811+
readFile: readFileFunc("secrethub.env", "TEST = {{ test/$variable/test }}"),
812+
osStat: osStatFunc("secrethub.env", nil),
813813
noMasking: true,
814814
dontPromptMissingTemplateVar: true,
815815
envFile: "secrethub.env",
@@ -830,8 +830,8 @@ func TestRunCommand_environment(t *testing.T) {
830830
},
831831
"template var set in os environment": {
832832
command: RunCommand{
833-
readFile: readFileFuncFromMap(map[string]string{"secrethub.env": "TEST = {{ test/$variable/test }}"}),
834-
osStat: osStatFuncFromMap(map[string]error{"secrethub.env": nil}),
833+
readFile: readFileFunc("secrethub.env", "TEST = {{ test/$variable/test }}"),
834+
osStat: osStatFunc("secrethub.env", nil),
835835
noMasking: true,
836836
dontPromptMissingTemplateVar: true,
837837
templateVersion: "2",
@@ -853,8 +853,8 @@ func TestRunCommand_environment(t *testing.T) {
853853
"template var set by flag": {
854854
command: RunCommand{
855855
command: []string{"/bin/sh", "./test.sh"},
856-
readFile: readFileFuncFromMap(map[string]string{"secrethub.env": "TEST = {{ test/$variable/test }}"}),
857-
osStat: osStatFuncFromMap(map[string]error{"secrethub.env": nil}),
856+
readFile: readFileFunc("secrethub.env", "TEST = {{ test/$variable/test }}"),
857+
osStat: osStatFunc("secrethub.env", nil),
858858
dontPromptMissingTemplateVar: true,
859859
templateVersion: "2",
860860
templateVars: map[string]string{"variable": "test"},
@@ -875,8 +875,8 @@ func TestRunCommand_environment(t *testing.T) {
875875
"template var set by flag has precedence over var set by environment": {
876876
command: RunCommand{
877877
command: []string{"/bin/sh", "./test.sh"},
878-
readFile: readFileFuncFromMap(map[string]string{"secrethub.env": "TEST=$variable"}),
879-
osStat: osStatFuncFromMap(map[string]error{"secrethub.env": nil}),
878+
readFile: readFileFunc("secrethub.env", "TEST=$variable"),
879+
osStat: osStatFunc("secrethub.env", nil),
880880
dontPromptMissingTemplateVar: true,
881881
templateVersion: "2",
882882
templateVars: map[string]string{"variable": "foo"},
@@ -898,8 +898,8 @@ func TestRunCommand_environment(t *testing.T) {
898898
"v1 template syntax success": {
899899
command: RunCommand{
900900
command: []string{"/bin/sh", "./test.sh"},
901-
readFile: readFileFuncFromMap(map[string]string{"secrethub.env": "TEST= ${path/to/secret}"}),
902-
osStat: osStatFuncFromMap(map[string]error{"secrethub.env": nil}),
901+
readFile: readFileFunc("secrethub.env", "TEST= ${path/to/secret}"),
902+
osStat: osStatFunc("secrethub.env", nil),
903903
templateVersion: "1",
904904
newClient: func() (secrethub.ClientInterface, error) {
905905
return fakeclient.Client{

0 commit comments

Comments
 (0)