Repository navigation
Expand file tree
/
Copy pathutil_test.go
More file actions
169 lines (154 loc) · 4.33 KB
/
Copy pathutil_test.go
File metadata and controls
169 lines (154 loc) · 4.33 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package patchbin
import (
"fmt"
"io"
"os"
"strings"
"testing"
)
func TestParsePatchsetWithCover(t *testing.T) {
file, err := os.Open("fixtures/with-cover.patch")
defer func() {
_ = file.Close()
}()
if err != nil {
t.Fatal(err.Error())
}
actual, err := ParsePatchset(file)
if err != nil {
t.Fatal(err.Error())
}
expected := []*Patch{
{Title: "Add torch deps"},
{Title: "feat: lets build an rnn"},
{Title: "chore: add torch to requirements"},
}
if len(actual) != len(expected) {
t.Fatalf("patches not same length (expected:%d, actual:%d)\n", len(expected), len(actual))
}
for idx, act := range actual {
exp := expected[idx]
if exp.Title != act.Title {
t.Fatalf("title does not match expected (expected:%s, actual:%s)", exp.Title, act.Title)
}
}
}
func TestParsePatchsetEmptyInput(t *testing.T) {
_, err := ParsePatchset(strings.NewReader(""))
if err == nil {
t.Fatal("expected error for empty patchset input, got nil")
}
}
func TestParsePatchsetWhitespaceOnlyInput(t *testing.T) {
_, err := ParsePatchset(strings.NewReader(" \n\n\t\n"))
if err == nil {
t.Fatal("expected error for whitespace-only patchset input, got nil")
}
}
func TestParsePatchsetGarbageInput(t *testing.T) {
_, err := ParsePatchset(strings.NewReader("this is not a patch\njust some random text\n"))
if err == nil {
t.Fatal("expected error for garbage patchset input, got nil")
}
}
func TestPatchToDiff(t *testing.T) {
file, err := os.Open("fixtures/single.patch")
defer func() {
_ = file.Close()
}()
if err != nil {
t.Fatal(err.Error())
}
fileExp, err := os.Open("fixtures/single.diff")
defer func() {
_ = file.Close()
}()
if err != nil {
t.Fatal(err.Error())
}
actual, err := patchToDiff(file)
if err != nil {
t.Fatal(err.Error())
}
by, err := io.ReadAll(fileExp)
if err != nil {
t.Fatal("cannot read expected file")
}
if actual != string(by) {
fmt.Println(actual)
t.Fatal("diff does not match expected")
}
}
func TestParseID(t *testing.T) {
tests := []struct {
input string
expected ParsedID
wantErr bool
}{
{input: "pr-1", expected: ParsedID{PrID: 1, Rev: 0}},
{input: "1", expected: ParsedID{PrID: 1, Rev: 0}},
{input: "pr-12.3", expected: ParsedID{PrID: 12, Rev: 3}},
{input: "12.3", expected: ParsedID{PrID: 12, Rev: 3}},
{input: "pr-5.v2", expected: ParsedID{PrID: 5, Rev: 2}},
{input: "invalid", wantErr: true},
{input: "", wantErr: true},
}
for _, tt := range tests {
got, err := ParseID(tt.input)
if tt.wantErr {
if err == nil {
t.Errorf("ParseID(%q) expected error, got nil", tt.input)
}
} else {
if err != nil {
t.Errorf("ParseID(%q) unexpected error: %v", tt.input, err)
}
if got != tt.expected {
t.Errorf("ParseID(%q) = %+v, expected %+v", tt.input, got, tt.expected)
}
}
}
}
func TestGetFormattedPatchsetID(t *testing.T) {
if got := getFormattedPatchsetID(1, 2); got != "1.2" {
t.Errorf("expected 1.2, got %q", got)
}
if got := getFormattedPatchsetID(0, 2); got != "" {
t.Errorf("expected empty string, got %q", got)
}
}
func TestParseTarget(t *testing.T) {
tests := []struct {
input string
expected Target
wantErr bool
}{
{input: "pico:feat/login", expected: Target{Repo: "pico", Slug: "feat/login", Rev: 0}},
{input: "pico:feat/login.2", expected: Target{Repo: "pico", Slug: "feat/login", Rev: 2}},
{input: "pico:feat/login.v3", expected: Target{Repo: "pico", Slug: "feat/login", Rev: 3}},
{input: "pico:feat/login.patch", expected: Target{Repo: "pico", Slug: "feat/login", Rev: 0}},
{input: "pico:feat/login.2.patch", expected: Target{Repo: "pico", Slug: "feat/login", Rev: 2}},
{input: "my-repo:123", expected: Target{Repo: "my-repo", Slug: "123", Rev: 0}},
{input: "my-repo:123.4", expected: Target{Repo: "my-repo", Slug: "123", Rev: 4}},
{input: "no-colon", wantErr: true},
{input: ":no-repo", wantErr: true},
{input: "no-slug:", wantErr: true},
{input: "repo:slug with spaces", wantErr: true},
{input: "", wantErr: true},
}
for _, tt := range tests {
got, err := ParseTarget(tt.input)
if tt.wantErr {
if err == nil {
t.Errorf("ParseTarget(%q) expected error, got nil", tt.input)
}
} else {
if err != nil {
t.Errorf("ParseTarget(%q) unexpected error: %v", tt.input, err)
}
if got != tt.expected {
t.Errorf("ParseTarget(%q) = %+v, expected %+v", tt.input, got, tt.expected)
}
}
}
}