github.com/bazelbuild/rules_webtesting@v0.2.0/go/metadata/metadata_test.go (about) 1 // Copyright 2016 Google Inc. 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 metadata 16 17 import ( 18 "reflect" 19 "testing" 20 21 "github.com/bazelbuild/rules_webtesting/go/bazel" 22 ) 23 24 const ( 25 allFields = "testdata/all-fields.json" 26 chromeLinux = "testdata/chrome-linux.json" 27 androidBrowser = "testdata/android-browser-gingerbread-nexus-s.json" 28 fakeBrowser = "testdata/merge-from-file-result.json" 29 badNamedFiles = "testdata/bad-named-files.json" 30 ) 31 32 func TestFromFile(t *testing.T) { 33 t.Run("valid file", func(t *testing.T) { 34 f, err := bazel.Runfile(allFields) 35 if err != nil { 36 t.Fatal(err) 37 } 38 file, err := FromFile(f, nil) 39 if err != nil { 40 t.Fatal(err) 41 } 42 43 expected := &Metadata{ 44 Capabilities: map[string]interface{}{}, 45 Environment: "chromeos", 46 BrowserLabel: "//browsers:figaro", 47 TestLabel: "//go/wtl:tests", 48 Extension: &extension{}, 49 } 50 51 if !reflect.DeepEqual(expected, file) { 52 t.Errorf("Got %#v, expected %#v", file, expected) 53 } 54 }) 55 56 t.Run("bad named files", func(t *testing.T) { 57 f, err := bazel.Runfile(badNamedFiles) 58 if err != nil { 59 t.Fatal(err) 60 } 61 d, err := FromFile(f, nil) 62 if err == nil { 63 t.Errorf("Got %#v, expected err", d) 64 } 65 }) 66 } 67 68 func TestMergeFromFile(t *testing.T) { 69 f1, err := bazel.Runfile(chromeLinux) 70 if err != nil { 71 t.Fatal(err) 72 } 73 cl, err := FromFile(f1, nil) 74 if err != nil { 75 t.Fatal(err) 76 } 77 78 f2, err := bazel.Runfile(androidBrowser) 79 if err != nil { 80 t.Fatal(err) 81 } 82 ab, err := FromFile(f2, nil) 83 if err != nil { 84 t.Fatal(err) 85 } 86 87 f3, err := bazel.Runfile(fakeBrowser) 88 if err != nil { 89 t.Fatal(err) 90 } 91 fb, err := FromFile(f3, nil) 92 if err != nil { 93 t.Fatal(err) 94 } 95 96 merged, err := Merge(cl, ab) 97 if err != nil { 98 t.Fatal(err) 99 } 100 if !reflect.DeepEqual(merged, fb) { 101 t.Errorf("Got %#v, expected %#v", merged, fb) 102 } 103 } 104 105 func TestMerge(t *testing.T) { 106 testCases := []struct { 107 name string 108 input1 *Metadata 109 input2 *Metadata 110 result *Metadata 111 }{ 112 { 113 "Environment override", 114 &Metadata{Environment: "linux"}, 115 &Metadata{Environment: "android"}, 116 &Metadata{Environment: "android"}, 117 }, 118 { 119 "Environment no override", 120 &Metadata{Environment: "linux"}, 121 &Metadata{Environment: ""}, 122 &Metadata{Environment: "linux"}, 123 }, 124 { 125 "BrowserLabel override", 126 &Metadata{BrowserLabel: "//browsers:figaro"}, 127 &Metadata{BrowserLabel: "//browsers:murphy"}, 128 &Metadata{BrowserLabel: "//browsers:murphy"}, 129 }, 130 { 131 "BrowserLabel no override", 132 &Metadata{BrowserLabel: "//browsers:figaro"}, 133 &Metadata{BrowserLabel: ""}, 134 &Metadata{BrowserLabel: "//browsers:figaro"}, 135 }, 136 { 137 "TestLabel override", 138 &Metadata{TestLabel: "//browsers:figaro"}, 139 &Metadata{TestLabel: "//browsers:murphy"}, 140 &Metadata{TestLabel: "//browsers:murphy"}, 141 }, 142 { 143 "TestLabel no override", 144 &Metadata{TestLabel: "//browsers:figaro"}, 145 &Metadata{TestLabel: ""}, 146 &Metadata{TestLabel: "//browsers:figaro"}, 147 }, 148 { 149 "EnableDebugger, no override", 150 &Metadata{DebuggerPort: 1}, 151 &Metadata{DebuggerPort: 0}, 152 &Metadata{DebuggerPort: 1}, 153 }, 154 { 155 "EnableDebugger, override", 156 &Metadata{DebuggerPort: 1}, 157 &Metadata{DebuggerPort: 2}, 158 &Metadata{DebuggerPort: 2}, 159 }, 160 { 161 "EnableDebugger, not set", 162 &Metadata{DebuggerPort: 0}, 163 &Metadata{DebuggerPort: 0}, 164 &Metadata{DebuggerPort: 0}, 165 }, 166 } 167 168 for _, tc := range testCases { 169 t.Run(tc.name, func(t *testing.T) { 170 a, err := Merge(tc.input1, tc.input2) 171 if err != nil { 172 t.Fatal(err) 173 } 174 if !reflect.DeepEqual(a, tc.result) { 175 t.Errorf("Got %#v, expected %#v", a, tc.result) 176 } 177 }) 178 } 179 } 180 181 func TestMergeNamedFiles(t *testing.T) { 182 testCases := []struct { 183 name string 184 input1 map[string]string 185 input2 map[string]string 186 result map[string]string // nil indicates should return an error 187 }{ 188 { 189 "empty", 190 map[string]string{}, 191 map[string]string{}, 192 map[string]string{}, 193 }, 194 { 195 "duplicate names, different paths", 196 map[string]string{"a": "b"}, 197 map[string]string{"a": "c"}, 198 nil, 199 }, 200 { 201 "duplicate names, same paths", 202 map[string]string{"a": "b"}, 203 map[string]string{"a": "b"}, 204 map[string]string{"a": "b"}, 205 }, 206 { 207 "multiple names, successful", 208 map[string]string{"a": "A", "b": "B", "c": "C"}, 209 map[string]string{"a": "A", "d": "D", "e": "E"}, 210 map[string]string{"a": "A", "b": "B", "c": "C", "d": "D", "e": "E"}, 211 }, 212 { 213 "multiple names, unsuccessful", 214 map[string]string{"a": "A", "b": "B", "c": "C"}, 215 map[string]string{"a": "A", "d": "D", "e": "E", "c": "X"}, 216 nil, 217 }, 218 } 219 220 for _, tc := range testCases { 221 t.Run(tc.name, func(t *testing.T) { 222 result, err := mergeNamedFiles(tc.input1, tc.input2) 223 if err != nil { 224 if tc.result != nil { 225 t.Error(err) 226 } 227 return 228 } 229 if tc.result == nil { 230 t.Errorf("Got %#v, expected error", result) 231 return 232 } 233 if !reflect.DeepEqual(result, tc.result) { 234 t.Errorf("Got %v, expected %v", result, tc.result) 235 } 236 }) 237 } 238 } 239 240 func TestMergeWebTestFiles(t *testing.T) { 241 testCases := []struct { 242 name string 243 input1 *WebTestFiles 244 input2 *WebTestFiles 245 result *WebTestFiles // nil indicates should return an error 246 }{ 247 { 248 "empty", 249 &WebTestFiles{NamedFiles: map[string]string{}}, 250 &WebTestFiles{NamedFiles: map[string]string{}}, 251 &WebTestFiles{NamedFiles: map[string]string{}}, 252 }, 253 { 254 "different archive paths", 255 &WebTestFiles{ArchiveFile: "a", NamedFiles: map[string]string{}}, 256 &WebTestFiles{ArchiveFile: "b", NamedFiles: map[string]string{}}, 257 nil, 258 }, 259 { 260 "different named file paths", 261 &WebTestFiles{ArchiveFile: "a", NamedFiles: map[string]string{"a": "A"}}, 262 &WebTestFiles{ArchiveFile: "a", NamedFiles: map[string]string{"a": "X"}}, 263 nil, 264 }, 265 { 266 "same named file paths", 267 &WebTestFiles{ArchiveFile: "a", NamedFiles: map[string]string{"a": "A"}}, 268 &WebTestFiles{ArchiveFile: "a", NamedFiles: map[string]string{"a": "A"}}, 269 &WebTestFiles{ArchiveFile: "a", NamedFiles: map[string]string{"a": "A"}}, 270 }, 271 { 272 "multiple names, successful", 273 &WebTestFiles{ArchiveFile: "a", NamedFiles: map[string]string{"a": "A", "b": "B", "c": "C"}}, 274 &WebTestFiles{ArchiveFile: "a", NamedFiles: map[string]string{"a": "A", "d": "D", "e": "E"}}, 275 &WebTestFiles{ArchiveFile: "a", NamedFiles: map[string]string{"a": "A", "b": "B", "c": "C", "d": "D", "e": "E"}}, 276 }, 277 { 278 "multiple names, unsuccessful", 279 &WebTestFiles{ArchiveFile: "a", NamedFiles: map[string]string{"a": "A", "b": "B", "c": "C"}}, 280 &WebTestFiles{ArchiveFile: "a", NamedFiles: map[string]string{"a": "A", "d": "D", "e": "E", "c": "X"}}, 281 nil, 282 }, 283 } 284 285 for _, tc := range testCases { 286 t.Run(tc.name, func(t *testing.T) { 287 result, err := mergeWebTestFiles(tc.input1, tc.input2) 288 if err != nil { 289 if tc.result != nil { 290 t.Fatal(err) 291 } 292 return 293 } 294 if tc.result == nil { 295 t.Fatalf("Got %#v, expected error", result) 296 } 297 if !reflect.DeepEqual(result, tc.result) { 298 t.Errorf("Got %#v, expected %#v", result, tc.result) 299 } 300 }) 301 } 302 303 } 304 305 func TestNormalizeWebTestFiles(t *testing.T) { 306 testCases := []struct { 307 name string 308 input []*WebTestFiles 309 // map of archive paths to NamedFiles maps 310 // nil indicates should return an error 311 result []*WebTestFiles 312 err bool 313 }{ 314 { 315 "empty", 316 nil, 317 nil, 318 false, 319 }, 320 { 321 "unnormalizable WebTestFiles", 322 []*WebTestFiles{ 323 {ArchiveFile: "a", NamedFiles: map[string]string{"a": "A"}}, 324 {ArchiveFile: "a", NamedFiles: map[string]string{"a": "X"}}, 325 }, 326 nil, 327 true, 328 }, 329 { 330 "normalizable WebTestFiles", 331 []*WebTestFiles{ 332 {ArchiveFile: "a", NamedFiles: map[string]string{"a": "A"}}, 333 {ArchiveFile: "a", NamedFiles: map[string]string{"a": "A"}}, 334 }, 335 []*WebTestFiles{ 336 {ArchiveFile: "a", NamedFiles: map[string]string{"a": "A"}}, 337 }, 338 false, 339 }, 340 { 341 "multiple WebTestFiles, success", 342 []*WebTestFiles{ 343 {ArchiveFile: "a", NamedFiles: map[string]string{"a": "A"}}, 344 {ArchiveFile: "b", NamedFiles: map[string]string{"b": "B"}}, 345 {ArchiveFile: "a", NamedFiles: map[string]string{"a": "A", "d": "D"}}, 346 {ArchiveFile: "c", NamedFiles: map[string]string{"c": "C"}}, 347 }, 348 []*WebTestFiles{ 349 {ArchiveFile: "a", NamedFiles: map[string]string{"a": "A", "d": "D"}}, 350 {ArchiveFile: "b", NamedFiles: map[string]string{"b": "B"}}, 351 {ArchiveFile: "c", NamedFiles: map[string]string{"c": "C"}}, 352 }, 353 false, 354 }, 355 { 356 "multiple WebTestFiles, failure", 357 []*WebTestFiles{ 358 {ArchiveFile: "a", NamedFiles: map[string]string{"a": "A"}}, 359 {ArchiveFile: "b", NamedFiles: map[string]string{"b": "B"}}, 360 {ArchiveFile: "a", NamedFiles: map[string]string{"d": "D"}}, 361 {ArchiveFile: "c", NamedFiles: map[string]string{"a": "A", "c": "C"}}, 362 }, 363 nil, 364 true, 365 }, 366 } 367 368 for _, tc := range testCases { 369 t.Run(tc.name, func(t *testing.T) { 370 result, err := normalizeWebTestFiles(tc.input) 371 if err != nil { 372 if !tc.err { 373 t.Fatal(err) 374 } 375 return 376 } 377 if tc.err { 378 t.Fatalf("Got %#v, expected error", result) 379 } 380 381 if !reflect.DeepEqual(result, tc.result) { 382 t.Fatalf("Got %#v, expected %#v", result, tc.result) 383 } 384 }) 385 } 386 }