Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(741)

Side by Side Diff: client/internal/common/filesystem_view_linux_test.go

Issue 2981243002: isolate: (refactor) extract blacklisting into common package (Closed)
Patch Set: Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The LUCI Authors.
mithro 2017/07/19 04:48:41 Why filesystem_view_linux_test.go ? Is there a wi
mcgreevy 2017/07/19 05:11:04 The presence of "_linux" before "_test" will cause
mithro 2017/07/19 05:46:18 Are blacklist patterns OS dependent? I feel like
mcgreevy 2017/07/21 04:15:50 In practice, blacklists look to be pretty simple,
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package common
16
17 import (
18 "testing"
19
20 . "github.com/smartystreets/goconvey/convey"
21 )
22
23 func TestValidatesBlacklist(t *testing.T) {
24 type testCase struct {
25 desc string
26 blacklist []string
27 wantErr bool
28 }
29
30 testCases := []testCase{
31 {
32 desc: "no patterns",
33 blacklist: []string{},
34 wantErr: false,
35 },
36 {
37 desc: "good pattern",
38 blacklist: []string{"*"},
39 wantErr: false,
40 },
41 {
42 desc: "good patterns",
43 blacklist: []string{"a*", "b*"},
44 wantErr: false,
45 },
46 {
47 desc: "bad pattern",
48 blacklist: []string{"["},
49 wantErr: true,
50 },
51 {
52 desc: "bad first pattern",
53 blacklist: []string{"[", "*"},
54 wantErr: true,
55 },
56 {
57 desc: "bad last pattern",
58 blacklist: []string{"*", "["},
59 wantErr: true,
mithro 2017/07/19 04:48:41 bad middle pattern?
mcgreevy 2017/07/19 05:11:04 I don't think that's a case worth testing.
mithro 2017/07/19 05:46:19 If you don't really think it is worth testing, I'm
mcgreevy 2017/07/21 04:15:50 The main failure mode I'm trying to cover here is
60 },
61 }
62
63 for _, tc := range testCases {
64 tc := tc
65 Convey(tc.desc, t, func() {
66 _, err := NewFilesystemView("/root", tc.blacklist)
67 if tc.wantErr {
68 So(err, ShouldNotBeNil)
69 } else {
70 So(err, ShouldBeNil)
71 }
72 })
73 }
74
75 }
76
77 func TestCalculatesRelativePaths(t *testing.T) {
78 type testCase struct {
79 desc string
80 root string
81 absPath string
82 wantRelPath string
83 wantErr bool
84 }
85
86 testCases := []testCase{
87 {
88 desc: "no trailing slash",
89 root: "/a",
90 absPath: "/a/b",
91 wantRelPath: "b",
92 wantErr: false,
93 },
94 {
95 desc: "trailing slash",
96 root: "/a",
97 absPath: "/a/b/",
98 wantRelPath: "b",
99 wantErr: false,
100 },
101 {
102 desc: "no common root",
103 root: "/a",
104 absPath: "/x/y",
105 wantRelPath: "../x/y",
106 wantErr: false,
107 },
108 {
109 desc: "no computable relative path",
110 root: "/a",
111 absPath: "./x/y",
112 wantRelPath: "",
113 wantErr: true,
114 },
115 {
116 desc: "root",
117 root: "/a",
118 absPath: "/a",
119 wantRelPath: ".",
120 wantErr: false,
121 },
122 }
123
124 for _, tc := range testCases {
125 tc := tc
126 Convey(tc.desc, t, func() {
127 fsView, err := NewFilesystemView(tc.root, nil)
128
129 So(err, ShouldBeNil)
130
131 relPath, err := fsView.RelativePath(tc.absPath)
132 So(relPath, ShouldEqual, tc.wantRelPath)
133 if tc.wantErr {
134 So(err, ShouldNotBeNil)
135 } else {
136 So(err, ShouldBeNil)
137 }
138 })
139 }
140 }
141
142 func TestAppliesBlacklist(t *testing.T) {
mithro 2017/07/19 05:46:19 BTW Does blacklisting (via filepath.Match) support
mcgreevy 2017/07/21 04:15:50 As mentioned in a comment above, this feature is a
143 type testCase struct {
144 desc string
145 root string
146 blacklist []string
147 absPath string
148 wantRelPath string
149 }
150
151 testCases := []testCase{
152 {
153 desc: "no blacklist",
154 root: "/a",
155 blacklist: []string{},
156 absPath: "/a/x/y",
157 wantRelPath: "x/y",
158 },
159 {
160 desc: "blacklist matches relative path",
161 root: "/a",
162 blacklist: []string{"?/z"},
163 absPath: "/a/x/z",
164 wantRelPath: "",
165 },
166 {
167 desc: "blacklist doesn't match relative path",
168 root: "/a",
169 blacklist: []string{"?/z"},
170 absPath: "/a/x/y",
171 wantRelPath: "x/y",
172 },
173 {
174 desc: "blacklist matches basename",
175 root: "/a",
176 blacklist: []string{"z"},
177 absPath: "/a/x/z",
178 wantRelPath: "",
179 },
180 {
181 desc: "blacklist doesn't match basename",
182 root: "/a",
183 blacklist: []string{"z"},
184 absPath: "/a/z/y",
185 wantRelPath: "z/y",
186 },
187 {
188 desc: "root never matches blacklist",
189 root: "/a",
190 blacklist: []string{"?"},
191 absPath: "/a",
192 wantRelPath: ".",
193 },
194 {
195 desc: "only one blacklist need match path (1)",
196 root: "/a",
197 blacklist: []string{"z", "abc"},
198 absPath: "/a/x/z",
199 wantRelPath: "",
200 },
201 {
202 desc: "only one blacklist need match path (2)",
203 root: "/a",
204 blacklist: []string{"abc", "z"},
205 absPath: "/a/x/z",
206 wantRelPath: "",
207 },
208 }
209
210 for _, tc := range testCases {
211 tc := tc
212 Convey(tc.desc, t, func() {
213 fsView, err := NewFilesystemView(tc.root, tc.blacklist)
214
215 // These test cases contain only valid blacklists.
216 // Invalid blacklists are tested in TestValidatesBlackli st.
217 So(err, ShouldBeNil)
mithro 2017/07/19 05:46:19 My mind keeps reading this as "Should Be Neil" :-P
mcgreevy 2017/07/21 04:15:50 Acknowledged.
218
219 relPath, err := fsView.RelativePath(tc.absPath)
220 So(relPath, ShouldEqual, tc.wantRelPath)
mithro 2017/07/19 05:46:19 It "feels" like you be checking err value first an
mcgreevy 2017/07/21 04:15:50 Done.
221
222 // These test cases contain only valid relative paths.
223 // non-computable relative paths are tested in TestCalcu latesRelativePaths.
224 So(err, ShouldBeNil)
225 })
226 }
227 }
OLDNEW
« client/internal/common/filesystem_view.go ('K') | « client/internal/common/filesystem_view.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698