-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswitcher_test.go
More file actions
92 lines (81 loc) · 2.16 KB
/
switcher_test.go
File metadata and controls
92 lines (81 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"fmt"
"strings"
"testing"
)
func TestSwitchProfile_Success(t *testing.T) {
var calls []string
mock := func(name string, args ...string) (string, error) {
calls = append(calls, name+" "+strings.Join(args, " "))
return "", nil
}
profile := Profile{
Name: "personal",
GitName: "Dan K",
GitEmail: "dan@example.com",
SSHKey: "~/.ssh/id_ed25519",
GHUser: "dankozlowski",
}
warnings, err := SwitchProfile(mock, profile)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(warnings) != 0 {
t.Errorf("unexpected warnings: %v", warnings)
}
expected := []string{
"git config --local user.name Dan K",
"git config --local user.email dan@example.com",
}
// SSH command uses expanded path — just check it contains core.sshCommand
if len(calls) < 4 {
t.Fatalf("expected 4 calls, got %d: %v", len(calls), calls)
}
for i, e := range expected {
if calls[i] != e {
t.Errorf("call[%d] = %q, want %q", i, calls[i], e)
}
}
if !strings.Contains(calls[2], "core.sshCommand") {
t.Errorf("call[2] should set core.sshCommand, got %q", calls[2])
}
if !strings.Contains(calls[3], "gh auth switch") {
t.Errorf("call[3] should run gh auth switch, got %q", calls[3])
}
}
func TestSwitchProfile_GHFails(t *testing.T) {
mock := func(name string, args ...string) (string, error) {
if name == "gh" {
return "", fmt.Errorf("account not found")
}
return "", nil
}
profile := Profile{
Name: "test", GitName: "Test", GitEmail: "test@test.com",
SSHKey: "~/.ssh/key", GHUser: "nobody",
}
warnings, err := SwitchProfile(mock, profile)
if err != nil {
t.Fatalf("gh failure should not cause error, got: %v", err)
}
if len(warnings) == 0 {
t.Error("expected a warning about gh failure")
}
}
func TestSwitchProfile_GitFails(t *testing.T) {
mock := func(name string, args ...string) (string, error) {
if name == "git" {
return "", fmt.Errorf("git error")
}
return "", nil
}
profile := Profile{
Name: "test", GitName: "Test", GitEmail: "test@test.com",
SSHKey: "~/.ssh/key", GHUser: "nobody",
}
_, err := SwitchProfile(mock, profile)
if err == nil {
t.Fatal("expected error when git fails")
}
}