-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
109 lines (99 loc) · 2.91 KB
/
Copy pathmain_test.go
File metadata and controls
109 lines (99 loc) · 2.91 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"path/filepath"
"testing"
)
func TestParseArgs(t *testing.T) {
t.Parallel()
tests := []struct {
name string
args []string
expectKind commandKind
path string
sessionName string
wantErr bool
}{
{"no args", nil, commandSelector, "", "", false},
{"version", []string{"--version"}, commandVersion, "", "", false},
{"help", []string{"--help"}, commandHelp, "", "", false},
{"history", []string{"--log"}, commandHistory, "", "", false},
{"create", []string{"proj"}, commandCreate, "proj", "", false},
{"create name", []string{"proj", "--name", "custom"}, commandCreate, "proj", "custom", false},
{"create name equals", []string{"proj", "--name=nice"}, commandCreate, "proj", "nice", false},
{"history extra", []string{"--log", "foo"}, 0, "", "", true},
{"unknown flag", []string{"proj", "--foo"}, 0, "", "", true},
{"name missing value", []string{"proj", "--name"}, 0, "", "", true},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
cmd, err := parseArgs(tt.args)
if tt.wantErr {
if err == nil {
t.Fatalf("expected error, got nil")
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if cmd.kind != tt.expectKind {
t.Fatalf("kind mismatch: got %v want %v", cmd.kind, tt.expectKind)
}
if cmd.path != tt.path {
t.Fatalf("path mismatch: got %q want %q", cmd.path, tt.path)
}
if cmd.sessionName != tt.sessionName {
t.Fatalf("session name mismatch: got %q want %q", cmd.sessionName, tt.sessionName)
}
})
}
}
func TestOwnerRepoSessionName(t *testing.T) {
t.Parallel()
cases := []struct {
in string
name string
ok bool
}{
{in: "savvyai/pr", name: "savvyai-pr", ok: true},
{in: "foo/bar", name: "foo-bar", ok: true},
{in: "foo_bar/baz.qux", name: "foo_bar-baz.qux", ok: true},
{in: "", ok: false},
{in: "/foo/bar", ok: false},
{in: "~/foo/bar", ok: false},
{in: "./foo/bar", ok: false},
{in: "../foo/bar", ok: false},
{in: "foo/bar/", ok: false},
{in: "foo//bar", ok: false},
{in: "foo/bar/baz", ok: false},
{in: "foo/..", ok: false},
{in: "foo/.", ok: false},
{in: "foo/ba r", ok: false},
{in: "foo/bar\\baz", ok: false},
}
for _, tc := range cases {
tc := tc
t.Run(tc.in, func(t *testing.T) {
t.Parallel()
got, ok := ownerRepoSessionName(tc.in)
if ok != tc.ok {
t.Fatalf("ok=%v, want %v (name=%q)", ok, tc.ok, got)
}
if got != tc.name {
t.Fatalf("name=%q, want %q", got, tc.name)
}
})
}
}
func TestInferSessionName(t *testing.T) {
t.Parallel()
resolved := filepath.Join(string(filepath.Separator), "tmp", "pr")
if got := inferSessionName("savvyai/pr", resolved); got != "savvyai-pr" {
t.Fatalf("got %q, want %q", got, "savvyai-pr")
}
if got := inferSessionName("./savvyai/pr", resolved); got != filepath.Base(resolved) {
t.Fatalf("got %q, want %q", got, filepath.Base(resolved))
}
}