This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrefname_test.go
123 lines (111 loc) · 2.55 KB
/
refname_test.go
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
/*
Copyright 2020 Google LLC
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file or at
https://developers.google.com/open-source/licenses/bsd
*/
package reftable
import "testing"
func TestValidateRefName(t *testing.T) {
for in, want := range map[string]bool{
"a//b": false,
"refs/heads/master": true,
"ab/": false,
"ab": true,
"": false,
"a/./b": false,
"a/../b": false,
"a/.../b": true,
} {
got := validateRefname(in)
if want != got {
t.Errorf("validateRefname(%q): got %v want %v", in, got, want)
}
}
}
func TestHasRef(t *testing.T) {
var refs []RefRecord
for _, s := range []string{
"a",
"a/b",
"prefix/a",
"prefix/b",
} {
refs = append(refs, RefRecord{RefName: s})
}
_, r := constructTestTable(t,
refs, nil, Config{})
add := []string{"a/c", "bb", "prefix/c"}
del := map[string]bool{
"a/b": true,
"prefix/b": true,
}
for nm, want := range map[string]bool{
"a": true, "aa": false,
"a/b": false, "a/": false,
"a/c": true, "bb": true, "prefix/c": true,
"prefix/b": false, "prefix/a": true,
"prefix": false,
} {
ok, err := hasRef(r, add, del, nm)
if err != nil {
t.Errorf("hasRef(%q): %v", nm, err)
}
if ok != want {
t.Errorf("hasRef(%q): got %v want %v", nm, ok, want)
}
}
for nm, want := range map[string]bool{
"a": true, "aa": false,
"a/": true,
"a/c": true, "b": true, "bb": true,
"prefix/": true,
} {
ok, err := hasRefWithPrefix(r, add, del, nm)
if err != nil {
t.Errorf("hasRef(%q): %v", nm, err)
}
if ok != want {
t.Errorf("hasRef(%q): got %v want %v", nm, ok, want)
}
}
}
func TestValidateAddition(t *testing.T) {
type testcase struct {
add []string
del []string
ok bool
}
var refs []RefRecord
for _, s := range []string{
"a/b",
"a/c",
"prefix/a",
"prefix/b",
} {
refs = append(refs, RefRecord{RefName: s})
}
_, r := constructTestTable(t,
refs, nil, Config{})
for _, tc := range []testcase{
{add: []string{"a"}, ok: false},
{
add: []string{"a"},
del: []string{"a/b", "a/c"},
ok: true,
},
{add: []string{"q", "q/r"}, ok: false},
{add: []string{"a/b/c"}, ok: false},
{add: []string{"q//r"}, ok: false},
{del: []string{"q//r"}, ok: true},
} {
asMap := map[string]bool{}
for _, d := range tc.del {
asMap[d] = true
}
err := validateAddition(r, tc.add, asMap)
if (err == nil) != tc.ok {
t.Errorf("case add %v, del %v: got err %v, want ok %v", tc.add, tc.del, err, tc.ok)
}
}
}