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

Commit a2da5d4

Browse files
Merge pull request #245 from secrethub/feature/mock-os-env
Allow mocking of OS environment
2 parents dfe5689 + 5ede32d commit a2da5d4

5 files changed

Lines changed: 21 additions & 7 deletions

File tree

internals/cli/env.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,12 @@ func (a *App) isExtraEnvVar(key string) bool {
103103
// of environment variables are not printed out for security reasons. The list
104104
// is limited to variables that are actually set in the environment. Setting
105105
// verbose to true will also include all known variables that are not set.
106-
func (a *App) PrintEnv(w io.Writer, verbose bool) error {
106+
func (a *App) PrintEnv(w io.Writer, verbose bool, osEnv func() []string) error {
107107
tabWriter := tabwriter.NewWriter(w, 0, 4, 4, ' ', 0)
108108
fmt.Fprintf(tabWriter, "%s\t%s\n", "NAME", "STATUS")
109109

110110
envVarStatus := make(map[string]string)
111-
for _, envVar := range os.Environ() {
111+
for _, envVar := range osEnv() {
112112
key, _, match := splitVar(a.name, a.separator, envVar)
113113
key = strings.ToUpper(key)
114114
if match {

internals/secrethub/inject.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type InjectCommand struct {
3434
useClipboard bool
3535
clearClipboardAfter time.Duration
3636
clipper clip.Clipper
37+
osEnv func() []string
3738
newClient newClientFunc
3839
templateVars map[string]string
3940
templateVersion string
@@ -44,6 +45,7 @@ type InjectCommand struct {
4445
func NewInjectCommand(io ui.IO, newClient newClientFunc) *InjectCommand {
4546
return &InjectCommand{
4647
clipper: clip.NewClipboard(),
48+
osEnv: os.Environ,
4749
clearClipboardAfter: defaultClearClipboardAfter,
4850
io: io,
4951
newClient: newClient,
@@ -99,7 +101,7 @@ func (cmd *InjectCommand) Run() error {
99101
}
100102
}
101103

102-
osEnv, _ := parseKeyValueStringsToMap(os.Environ())
104+
osEnv, _ := parseKeyValueStringsToMap(cmd.osEnv())
103105

104106
var templateVariableReader tpl.VariableReader
105107
templateVariableReader, err = newVariableReader(osEnv, cmd.templateVars)

internals/secrethub/printenv.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package secrethub
22

33
import (
4+
"os"
5+
46
"github.com/secrethub/secrethub-cli/internals/cli"
57
"github.com/secrethub/secrethub-cli/internals/cli/ui"
68
"github.com/secrethub/secrethub-cli/internals/secrethub/command"
@@ -10,20 +12,22 @@ import (
1012
type PrintEnvCommand struct {
1113
app *cli.App
1214
io ui.IO
15+
osEnv func() []string
1316
verbose bool
1417
}
1518

1619
// NewPrintEnvCommand creates a new PrintEnvCommand.
1720
func NewPrintEnvCommand(app *cli.App, io ui.IO) *PrintEnvCommand {
1821
return &PrintEnvCommand{
19-
app: app,
20-
io: io,
22+
app: app,
23+
io: io,
24+
osEnv: os.Environ,
2125
}
2226
}
2327

2428
// Run prints out debug statements about all environment variables.
2529
func (cmd *PrintEnvCommand) Run() error {
26-
err := cmd.app.PrintEnv(cmd.io.Stdout(), cmd.verbose)
30+
err := cmd.app.PrintEnv(cmd.io.Stdout(), cmd.verbose, cmd.osEnv)
2731
if err != nil {
2832
return err
2933
}

internals/secrethub/run.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ const (
5757
type RunCommand struct {
5858
command []string
5959
io ui.IO
60+
osEnv func() []string
6061
envar map[string]string
6162
envFile string
6263
templateVars map[string]string
@@ -73,6 +74,7 @@ type RunCommand struct {
7374
func NewRunCommand(io ui.IO, newClient newClientFunc) *RunCommand {
7475
return &RunCommand{
7576
io: io,
77+
osEnv: os.Environ,
7678
envar: make(map[string]string),
7779
templateVars: make(map[string]string),
7880
newClient: newClient,
@@ -129,7 +131,7 @@ func (cmd *RunCommand) Run() error {
129131
}
130132
}
131133

132-
osEnv, passthroughEnv := parseKeyValueStringsToMap(os.Environ())
134+
osEnv, passthroughEnv := parseKeyValueStringsToMap(cmd.osEnv())
133135
if err != nil {
134136
return err
135137
}

internals/secrethub/run_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,11 +458,13 @@ func TestRunCommand_Run(t *testing.T) {
458458
}{
459459
"success, no secrets": {
460460
command: RunCommand{
461+
osEnv: func() []string { return []string{} },
461462
command: []string{"echo", "test"},
462463
},
463464
},
464465
"missing secret": {
465466
command: RunCommand{
467+
osEnv: func() []string { return []string{} },
466468
command: []string{"echo", "test"},
467469
envar: map[string]string{
468470
"missing": "path/to/unexisting/secret",
@@ -484,6 +486,7 @@ func TestRunCommand_Run(t *testing.T) {
484486
},
485487
"missing secret ignored": {
486488
command: RunCommand{
489+
osEnv: func() []string { return []string{} },
487490
command: []string{"echo", "test"},
488491
envar: map[string]string{
489492
"missing": "path/to/unexisting/secret",
@@ -505,6 +508,7 @@ func TestRunCommand_Run(t *testing.T) {
505508
},
506509
"repo does not exist ignored": {
507510
command: RunCommand{
511+
osEnv: func() []string { return []string{} },
508512
command: []string{"echo", "test"},
509513
envar: map[string]string{
510514
"missing": "unexisting/repo/secret",
@@ -526,6 +530,7 @@ func TestRunCommand_Run(t *testing.T) {
526530
},
527531
"invalid template var: start with a number": {
528532
command: RunCommand{
533+
osEnv: func() []string { return []string{} },
529534
envFile: "secrethub.env",
530535
templateVars: map[string]string{
531536
"0foo": "value",
@@ -536,6 +541,7 @@ func TestRunCommand_Run(t *testing.T) {
536541
},
537542
"invalid template var: illegal character": {
538543
command: RunCommand{
544+
osEnv: func() []string { return []string{} },
539545
envFile: "secrethub.env",
540546
templateVars: map[string]string{
541547
"foo@bar": "value",

0 commit comments

Comments
 (0)