Skip to content

Commit 2272e67

Browse files
committed
test: use mocks
1 parent 8ff7202 commit 2272e67

File tree

3 files changed

+28
-20
lines changed

3 files changed

+28
-20
lines changed

examples/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func listProviders() {
118118
}
119119

120120
func checkEnvironmentVariables(configPath string) error {
121-
file, err := os.Open(configPath)
121+
file, err := os.Open(filepath.Clean(configPath))
122122
if err != nil {
123123
return err
124124
}

external_test.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,6 @@ import (
99
"github.com/flowexec/vault"
1010
)
1111

12-
// mockCommandContext creates mock commands for testing
13-
func mockCommandContext(
14-
outputs map[string]string,
15-
errors map[string]error,
16-
) func(ctx context.Context, cmd, input, dir string, envList []string) (string, error) {
17-
return func(ctx context.Context, cmd, input, dir string, envList []string) (string, error) {
18-
for _, output := range outputs {
19-
return output, nil
20-
}
21-
22-
for range errors {
23-
return "", fmt.Errorf("mock error")
24-
}
25-
26-
return "mock", nil
27-
}
28-
}
29-
3012
func TestNewExternalVaultProvider(t *testing.T) {
3113
tests := []struct {
3214
name string
@@ -430,3 +412,21 @@ func TestExternalVaultProvider_Metadata(t *testing.T) {
430412
})
431413
}
432414
}
415+
416+
// mockCommandContext creates mock commands for testing
417+
func mockCommandContext(
418+
outputs map[string]string,
419+
errors map[string]error,
420+
) func(ctx context.Context, cmd, input, dir string, envList []string) (string, error) {
421+
return func(ctx context.Context, cmd, input, dir string, envList []string) (string, error) {
422+
for _, output := range outputs {
423+
return output, nil
424+
}
425+
426+
for range errors {
427+
return "", fmt.Errorf("mock error")
428+
}
429+
430+
return "mock", nil
431+
}
432+
}

keyring_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ import (
44
"errors"
55
"testing"
66

7+
"github.com/zalando/go-keyring"
8+
79
"github.com/flowexec/vault"
810
)
911

1012
const testKeyringService = "flowexec-vault-test"
1113

1214
func TestKeyringVault_New(t *testing.T) {
15+
keyring.MockInit()
1316
vlt, cfg, err := vault.New("test-keyring-vault",
1417
vault.WithProvider(vault.ProviderTypeKeyring),
1518
vault.WithKeyringService(testKeyringService),
@@ -37,6 +40,7 @@ func TestKeyringVault_New(t *testing.T) {
3740
}
3841

3942
func TestKeyringVault_SecretOperations(t *testing.T) {
43+
keyring.MockInit()
4044
vlt, _, err := vault.New("test-keyring-vault",
4145
vault.WithProvider(vault.ProviderTypeKeyring),
4246
vault.WithKeyringService(testKeyringService),
@@ -45,7 +49,6 @@ func TestKeyringVault_SecretOperations(t *testing.T) {
4549
t.Fatalf("Failed to create keyring vault: %v", err)
4650
}
4751
defer func() {
48-
// Clean up any remaining secrets before closing
4952
secrets, _ := vlt.ListSecrets()
5053
for _, key := range secrets {
5154
_ = vlt.DeleteSecret(key)
@@ -123,6 +126,7 @@ func TestKeyringVault_SecretOperations(t *testing.T) {
123126
}
124127

125128
func TestKeyringVault_MultipleSecrets(t *testing.T) {
129+
keyring.MockInit()
126130
vlt, _, err := vault.New("test-keyring-vault-multiple",
127131
vault.WithProvider(vault.ProviderTypeKeyring),
128132
vault.WithKeyringService(testKeyringService),
@@ -205,6 +209,7 @@ func TestKeyringVault_MultipleSecrets(t *testing.T) {
205209
}
206210

207211
func TestKeyringVault_Persistence(t *testing.T) {
212+
keyring.MockInit()
208213
testKey := "persistent-keyring-key"
209214
testValue := "persistent-keyring-value"
210215

@@ -258,6 +263,7 @@ func TestKeyringVault_Persistence(t *testing.T) {
258263
}
259264

260265
func TestKeyringVault_Metadata(t *testing.T) {
266+
keyring.MockInit()
261267
vlt, _, err := vault.New("test-keyring-metadata",
262268
vault.WithProvider(vault.ProviderTypeKeyring),
263269
vault.WithKeyringService(testKeyringService),
@@ -293,6 +299,7 @@ func TestKeyringVault_Metadata(t *testing.T) {
293299
}
294300

295301
func TestKeyringVault_InvalidKeyValidation(t *testing.T) {
302+
keyring.MockInit()
296303
vlt, _, err := vault.New("test-keyring-validation",
297304
vault.WithProvider(vault.ProviderTypeKeyring),
298305
vault.WithKeyringService(testKeyringService),
@@ -325,6 +332,7 @@ func TestKeyringVault_InvalidKeyValidation(t *testing.T) {
325332
}
326333

327334
func TestKeyringVault_SortedOutput(t *testing.T) {
335+
keyring.MockInit()
328336
vlt, _, err := vault.New("test-keyring-sorted",
329337
vault.WithProvider(vault.ProviderTypeKeyring),
330338
vault.WithKeyringService(testKeyringService),

0 commit comments

Comments
 (0)