| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 The LUCI Authors. |
| 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, |
| 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) { |
| 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) |
| 218 |
| 219 relPath, err := fsView.RelativePath(tc.absPath) |
| 220 // These test cases contain only valid relative paths. |
| 221 // non-computable relative paths are tested in TestCalcu
latesRelativePaths. |
| 222 So(err, ShouldBeNil) |
| 223 So(relPath, ShouldEqual, tc.wantRelPath) |
| 224 }) |
| 225 } |
| 226 } |
| OLD | NEW |