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

Commit 7f52f3c

Browse files
Merge pull request #348 from secrethub/fix/deterministic-test-2
Hotfix: fix test failing 50% of times
2 parents d5e091a + da83812 commit 7f52f3c

2 files changed

Lines changed: 29 additions & 16 deletions

File tree

internals/secrethub/env_source.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,12 @@ import (
2424
)
2525

2626
type errNameCollision struct {
27-
name string
28-
firstPath string
29-
secondPath string
27+
name string
28+
paths [2]string
3029
}
3130

3231
func (e errNameCollision) Error() string {
33-
return fmt.Sprintf("secrets at path %s and %s map to the same environment variable: %s. Rename one of the secrets or source them in a different way", e.firstPath, e.secondPath, e.name)
32+
return fmt.Sprintf("secrets at path %s and %s map to the same environment variable: %s. Rename one of the secrets or source them in a different way", e.paths[0], e.paths[1], e.name)
3433
}
3534

3635
type environment struct {
@@ -225,9 +224,11 @@ func (s *secretsDirEnv) env() (map[string]value, error) {
225224
envVarName := s.envVarName(path)
226225
if prevPath, found := paths[envVarName]; found {
227226
return nil, errNameCollision{
228-
name: envVarName,
229-
firstPath: prevPath,
230-
secondPath: path,
227+
name: envVarName,
228+
paths: [2]string{
229+
prevPath,
230+
path,
231+
},
231232
}
232233
}
233234
paths[envVarName] = path

internals/secrethub/env_source_test.go

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

33
import (
4+
"sort"
45
"testing"
56

67
"github.com/secrethub/secrethub-go/internals/api"
@@ -18,9 +19,9 @@ func TestSecretsDirEnv(t *testing.T) {
1819
secretUUID2 := uuid.New()
1920

2021
cases := map[string]struct {
21-
newClient newClientFunc
22-
expectedValues []string
23-
err error
22+
newClient newClientFunc
23+
expectedValues []string
24+
expectedCollission *errNameCollision
2425
}{
2526
"success": {
2627
newClient: func() (secrethub.ClientInterface, error) {
@@ -114,10 +115,12 @@ func TestSecretsDirEnv(t *testing.T) {
114115
},
115116
}, nil
116117
},
117-
err: errNameCollision{
118-
name: "FOO_BAR",
119-
firstPath: "namespace/repo/foo/bar",
120-
secondPath: "namespace/repo/foo_bar",
118+
expectedCollission: &errNameCollision{
119+
name: "FOO_BAR",
120+
paths: [2]string{
121+
"namespace/repo/foo/bar",
122+
"namespace/repo/foo_bar",
123+
},
121124
},
122125
},
123126
}
@@ -126,8 +129,17 @@ func TestSecretsDirEnv(t *testing.T) {
126129
t.Run(name, func(t *testing.T) {
127130
source := newSecretsDirEnv(tc.newClient, dirPath)
128131
secrets, err := source.env()
129-
if tc.err != nil {
130-
assert.Equal(t, err, tc.err)
132+
if tc.expectedCollission != nil {
133+
collisionErr, ok := err.(errNameCollision)
134+
assert.Equal(t, ok, true)
135+
assert.Equal(t, collisionErr.name, tc.expectedCollission.name)
136+
137+
gotPaths := collisionErr.paths[:]
138+
expectedPaths := tc.expectedCollission.paths[:]
139+
sort.Strings(gotPaths)
140+
sort.Strings(expectedPaths)
141+
142+
assert.Equal(t, gotPaths, expectedPaths)
131143
} else {
132144
assert.OK(t, err)
133145
assert.Equal(t, len(secrets), len(tc.expectedValues))

0 commit comments

Comments
 (0)