| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library barback.test.asset_graph.transform_test; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 | |
| 9 import 'package:barback/barback.dart'; | |
| 10 import 'package:barback/src/asset_graph.dart'; | |
| 11 import 'package:scheduled_test/scheduled_test.dart'; | |
| 12 | |
| 13 import '../utils.dart'; | |
| 14 | |
| 15 main() { | |
| 16 initConfig(); | |
| 17 test("gets a transformed asset with a different path", () { | |
| 18 initGraph(["app|foo.blub"], {"app": [ | |
| 19 [new RewriteTransformer("blub", "blab")] | |
| 20 ]}); | |
| 21 updateSources(["app|foo.blub"]); | |
| 22 expectAsset("app|foo.blab", "foo.blab"); | |
| 23 }); | |
| 24 | |
| 25 test("gets a transformed asset with the same path", () { | |
| 26 initGraph(["app|foo.blub"], {"app": [ | |
| 27 [new RewriteTransformer("blub", "blub")] | |
| 28 ]}); | |
| 29 updateSources(["app|foo.blub"]); | |
| 30 expectAsset("app|foo.blub", "foo.blub"); | |
| 31 }); | |
| 32 | |
| 33 test("doesn't find an output from a later phase", () { | |
| 34 initGraph(["app|foo.a"], {"app": [ | |
| 35 [new RewriteTransformer("b", "c")], | |
| 36 [new RewriteTransformer("a", "b")] | |
| 37 ]}); | |
| 38 updateSources(["app|foo.a"]); | |
| 39 expectNoAsset("app|foo.c"); | |
| 40 }); | |
| 41 | |
| 42 test("doesn't find an output from the same phase", () { | |
| 43 initGraph(["app|foo.a"], {"app": [ | |
| 44 [ | |
| 45 new RewriteTransformer("a", "b"), | |
| 46 new RewriteTransformer("b", "c") | |
| 47 ] | |
| 48 ]}); | |
| 49 updateSources(["app|foo.a"]); | |
| 50 expectAsset("app|foo.b", "foo.b"); | |
| 51 expectNoAsset("app|foo.c"); | |
| 52 }); | |
| 53 | |
| 54 test("finds the latest output before the transformer's phase", () { | |
| 55 initGraph(["app|foo.blub"], {"app": [ | |
| 56 [new RewriteTransformer("blub", "blub")], | |
| 57 [ | |
| 58 new RewriteTransformer("blub", "blub"), | |
| 59 new RewriteTransformer("blub", "done") | |
| 60 ], | |
| 61 [new RewriteTransformer("blub", "blub")] | |
| 62 ]}); | |
| 63 updateSources(["app|foo.blub"]); | |
| 64 expectAsset("app|foo.done", "foo.blub.done"); | |
| 65 }); | |
| 66 | |
| 67 test("applies multiple transformations to an asset", () { | |
| 68 initGraph(["app|foo.a"], {"app": [ | |
| 69 [new RewriteTransformer("a", "b")], | |
| 70 [new RewriteTransformer("b", "c")], | |
| 71 [new RewriteTransformer("c", "d")], | |
| 72 [new RewriteTransformer("d", "e")], | |
| 73 [new RewriteTransformer("e", "f")], | |
| 74 [new RewriteTransformer("f", "g")], | |
| 75 [new RewriteTransformer("g", "h")], | |
| 76 [new RewriteTransformer("h", "i")], | |
| 77 [new RewriteTransformer("i", "j")], | |
| 78 [new RewriteTransformer("j", "k")], | |
| 79 ]}); | |
| 80 updateSources(["app|foo.a"]); | |
| 81 expectAsset("app|foo.k", "foo.b.c.d.e.f.g.h.i.j.k"); | |
| 82 }); | |
| 83 | |
| 84 test("only runs a transform once for all of its outputs", () { | |
| 85 var transformer = new RewriteTransformer("blub", "a b c"); | |
| 86 initGraph(["app|foo.blub"], {"app": [[transformer]]}); | |
| 87 updateSources(["app|foo.blub"]); | |
| 88 expectAsset("app|foo.a", "foo.a"); | |
| 89 expectAsset("app|foo.b", "foo.b"); | |
| 90 expectAsset("app|foo.c", "foo.c"); | |
| 91 schedule(() { | |
| 92 expect(transformer.numRuns, equals(1)); | |
| 93 }); | |
| 94 }); | |
| 95 | |
| 96 test("runs transforms in the same phase in parallel", () { | |
| 97 var transformerA = new RewriteTransformer("txt", "a"); | |
| 98 var transformerB = new RewriteTransformer("txt", "b"); | |
| 99 initGraph(["app|foo.txt"], {"app": [[transformerA, transformerB]]}); | |
| 100 | |
| 101 transformerA.wait(); | |
| 102 transformerB.wait(); | |
| 103 | |
| 104 schedule(() { | |
| 105 updateSources(["app|foo.txt"]); | |
| 106 | |
| 107 // Wait for them both to start. | |
| 108 return Future.wait([transformerA.started, transformerB.started]); | |
| 109 }); | |
| 110 | |
| 111 schedule(() { | |
| 112 // They should both still be running. | |
| 113 expect(transformerA.isRunning, isTrue); | |
| 114 expect(transformerB.isRunning, isTrue); | |
| 115 | |
| 116 transformerA.complete(); | |
| 117 transformerB.complete(); | |
| 118 }); | |
| 119 | |
| 120 expectAsset("app|foo.a", "foo.a"); | |
| 121 expectAsset("app|foo.b", "foo.b"); | |
| 122 }); | |
| 123 | |
| 124 test("does not reapply transform when inputs are not modified", () { | |
| 125 var transformer = new RewriteTransformer("blub", "blab"); | |
| 126 initGraph(["app|foo.blub"], {"app": [[transformer]]}); | |
| 127 updateSources(["app|foo.blub"]); | |
| 128 expectAsset("app|foo.blab", "foo.blab"); | |
| 129 expectAsset("app|foo.blab", "foo.blab"); | |
| 130 expectAsset("app|foo.blab", "foo.blab"); | |
| 131 | |
| 132 schedule(() { | |
| 133 expect(transformer.numRuns, equals(1)); | |
| 134 }); | |
| 135 }); | |
| 136 | |
| 137 test("reapplies a transform when its input is modified", () { | |
| 138 var transformer = new RewriteTransformer("blub", "blab"); | |
| 139 initGraph(["app|foo.blub"], {"app": [[transformer]]}); | |
| 140 | |
| 141 schedule(() { | |
| 142 updateSources(["app|foo.blub"]); | |
| 143 }); | |
| 144 | |
| 145 expectAsset("app|foo.blab", "foo.blab"); | |
| 146 | |
| 147 schedule(() { | |
| 148 updateSources(["app|foo.blub"]); | |
| 149 }); | |
| 150 | |
| 151 expectAsset("app|foo.blab", "foo.blab"); | |
| 152 | |
| 153 schedule(() { | |
| 154 updateSources(["app|foo.blub"]); | |
| 155 }); | |
| 156 | |
| 157 expectAsset("app|foo.blab", "foo.blab"); | |
| 158 | |
| 159 schedule(() { | |
| 160 expect(transformer.numRuns, equals(3)); | |
| 161 }); | |
| 162 }); | |
| 163 | |
| 164 test("does not reapply transform when a removed input is modified", () { | |
| 165 var transformer = new ManyToOneTransformer("txt"); | |
| 166 initGraph({ | |
| 167 "app|a.txt": "a.inc,b.inc", | |
| 168 "app|a.inc": "a", | |
| 169 "app|b.inc": "b" | |
| 170 }, {"app": [[transformer]]}); | |
| 171 | |
| 172 updateSources(["app|a.txt", "app|a.inc", "app|b.inc"]); | |
| 173 | |
| 174 expectAsset("app|a.out", "ab"); | |
| 175 | |
| 176 // Remove the dependency on the non-primary input. | |
| 177 schedule(() { | |
| 178 modifyAsset("app|a.txt", "a.inc"); | |
| 179 updateSources(["app|a.txt"]); | |
| 180 }); | |
| 181 | |
| 182 // Process it again. | |
| 183 expectAsset("app|a.out", "a"); | |
| 184 | |
| 185 // Now touch the removed input. It should not trigger another build. | |
| 186 schedule(() { | |
| 187 updateSources(["app|b.inc"]); | |
| 188 }); | |
| 189 | |
| 190 expectAsset("app|a.out", "a"); | |
| 191 | |
| 192 schedule(() { | |
| 193 expect(transformer.numRuns, equals(2)); | |
| 194 }); | |
| 195 }); | |
| 196 | |
| 197 test("allows a transform to generate multiple outputs", () { | |
| 198 initGraph({"app|foo.txt": "a.out,b.out"}, {"app": [ | |
| 199 [new OneToManyTransformer("txt")] | |
| 200 ]}); | |
| 201 | |
| 202 updateSources(["app|foo.txt"]); | |
| 203 | |
| 204 expectAsset("app|a.out", "spread txt"); | |
| 205 expectAsset("app|b.out", "spread txt"); | |
| 206 }); | |
| 207 | |
| 208 test("does not rebuild transforms that don't use modified source", () { | |
| 209 var a = new RewriteTransformer("a", "aa"); | |
| 210 var aa = new RewriteTransformer("aa", "aaa"); | |
| 211 var b = new RewriteTransformer("b", "bb"); | |
| 212 var bb = new RewriteTransformer("bb", "bbb"); | |
| 213 initGraph(["app|foo.a", "app|foo.b"], {"app": [ | |
| 214 [a, b], | |
| 215 [aa, bb], | |
| 216 ]}); | |
| 217 | |
| 218 updateSources(["app|foo.a"]); | |
| 219 updateSources(["app|foo.b"]); | |
| 220 | |
| 221 expectAsset("app|foo.aaa", "foo.aa.aaa"); | |
| 222 expectAsset("app|foo.bbb", "foo.bb.bbb"); | |
| 223 | |
| 224 schedule(() { | |
| 225 updateSources(["app|foo.a"]); | |
| 226 }); | |
| 227 | |
| 228 expectAsset("app|foo.aaa", "foo.aa.aaa"); | |
| 229 expectAsset("app|foo.bbb", "foo.bb.bbb"); | |
| 230 | |
| 231 schedule(() { | |
| 232 expect(aa.numRuns, equals(2)); | |
| 233 expect(bb.numRuns, equals(1)); | |
| 234 }); | |
| 235 }); | |
| 236 | |
| 237 test("doesn't get an output from a transform whose primary input is removed", | |
| 238 () { | |
| 239 initGraph(["app|foo.txt"], {"app": [ | |
| 240 [new RewriteTransformer("txt", "out")] | |
| 241 ]}); | |
| 242 | |
| 243 updateSources(["app|foo.txt"]); | |
| 244 expectAsset("app|foo.out", "foo.out"); | |
| 245 | |
| 246 schedule(() { | |
| 247 removeSources(["app|foo.txt"]); | |
| 248 }); | |
| 249 | |
| 250 expectNoAsset("app|foo.out"); | |
| 251 }); | |
| 252 | |
| 253 test("reapplies a transform when a non-primary input changes", () { | |
| 254 initGraph({ | |
| 255 "app|a.txt": "a.inc", | |
| 256 "app|a.inc": "a" | |
| 257 }, {"app": [[new ManyToOneTransformer("txt")]]}); | |
| 258 | |
| 259 updateSources(["app|a.txt", "app|a.inc"]); | |
| 260 expectAsset("app|a.out", "a"); | |
| 261 | |
| 262 schedule(() { | |
| 263 modifyAsset("app|a.inc", "after"); | |
| 264 updateSources(["app|a.inc"]); | |
| 265 }); | |
| 266 | |
| 267 expectAsset("app|a.out", "after"); | |
| 268 }); | |
| 269 | |
| 270 test("restarts processing if a change occurs during processing", () { | |
| 271 var transformer = new RewriteTransformer("txt", "out"); | |
| 272 initGraph(["app|foo.txt"], {"app": [[transformer]]}); | |
| 273 | |
| 274 transformer.wait(); | |
| 275 | |
| 276 schedule(() { | |
| 277 updateSources(["app|foo.txt"]); | |
| 278 | |
| 279 // Wait for the transform to start. | |
| 280 return transformer.started; | |
| 281 }); | |
| 282 | |
| 283 schedule(() { | |
| 284 // Now update the graph during it. | |
| 285 updateSources(["app|foo.txt"]); | |
| 286 }); | |
| 287 | |
| 288 schedule(() { | |
| 289 transformer.complete(); | |
| 290 }); | |
| 291 | |
| 292 expectAsset("app|foo.out", "foo.out"); | |
| 293 | |
| 294 schedule(() { | |
| 295 expect(transformer.numRuns, equals(2)); | |
| 296 }); | |
| 297 }); | |
| 298 | |
| 299 test("handles an output moving from one transformer to another", () { | |
| 300 // In the first run, "shared.out" is created by the "a.a" transformer. | |
| 301 initGraph({ | |
| 302 "app|a.a": "a.out,shared.out", | |
| 303 "app|b.b": "b.out" | |
| 304 }, {"app": [ | |
| 305 [new OneToManyTransformer("a"), new OneToManyTransformer("b")] | |
| 306 ]}); | |
| 307 | |
| 308 updateSources(["app|a.a", "app|b.b"]); | |
| 309 | |
| 310 expectAsset("app|a.out", "spread a"); | |
| 311 expectAsset("app|b.out", "spread b"); | |
| 312 expectAsset("app|shared.out", "spread a"); | |
| 313 | |
| 314 // Now switch their contents so that "shared.out" will be output by "b.b"'s | |
| 315 // transformer. | |
| 316 schedule(() { | |
| 317 modifyAsset("app|a.a", "a.out"); | |
| 318 modifyAsset("app|b.b", "b.out,shared.out"); | |
| 319 updateSources(["app|a.a", "app|b.b"]); | |
| 320 }); | |
| 321 | |
| 322 expectAsset("app|a.out", "spread a"); | |
| 323 expectAsset("app|b.out", "spread b"); | |
| 324 expectAsset("app|shared.out", "spread b"); | |
| 325 }); | |
| 326 | |
| 327 test("restarts before finishing later phases when a change occurs", () { | |
| 328 var txtToInt = new RewriteTransformer("txt", "int"); | |
| 329 var intToOut = new RewriteTransformer("int", "out"); | |
| 330 initGraph(["app|foo.txt", "app|bar.txt"], | |
| 331 {"app": [[txtToInt], [intToOut]]}); | |
| 332 | |
| 333 txtToInt.wait(); | |
| 334 | |
| 335 schedule(() { | |
| 336 updateSources(["app|foo.txt"]); | |
| 337 | |
| 338 // Wait for the first transform to start. | |
| 339 return txtToInt.started; | |
| 340 }); | |
| 341 | |
| 342 schedule(() { | |
| 343 // Now update the graph during it. | |
| 344 updateSources(["app|bar.txt"]); | |
| 345 }); | |
| 346 | |
| 347 schedule(() { | |
| 348 txtToInt.complete(); | |
| 349 }); | |
| 350 | |
| 351 expectAsset("app|foo.out", "foo.int.out"); | |
| 352 expectAsset("app|bar.out", "bar.int.out"); | |
| 353 | |
| 354 schedule(() { | |
| 355 // Should only have run each transform once for each primary. | |
| 356 expect(txtToInt.numRuns, equals(2)); | |
| 357 expect(intToOut.numRuns, equals(2)); | |
| 358 }); | |
| 359 }); | |
| 360 | |
| 361 test("applies transforms to the correct packages", () { | |
| 362 var rewrite1 = new RewriteTransformer("txt", "out1"); | |
| 363 var rewrite2 = new RewriteTransformer("txt", "out2"); | |
| 364 initGraph([ | |
| 365 "pkg1|foo.txt", | |
| 366 "pkg2|foo.txt" | |
| 367 ], {"pkg1": [[rewrite1]], "pkg2": [[rewrite2]]}); | |
| 368 | |
| 369 updateSources(["pkg1|foo.txt", "pkg2|foo.txt"]); | |
| 370 expectAsset("pkg1|foo.out1", "foo.out1"); | |
| 371 expectAsset("pkg2|foo.out2", "foo.out2"); | |
| 372 buildShouldSucceed(); | |
| 373 }); | |
| 374 | |
| 375 test("transforms don't see generated assets in other packages", () { | |
| 376 var fooToBar = new RewriteTransformer("foo", "bar"); | |
| 377 var barToBaz = new RewriteTransformer("bar", "baz"); | |
| 378 initGraph(["pkg1|file.foo"], {"pkg1": [[fooToBar]], "pkg2": [[barToBaz]]}); | |
| 379 | |
| 380 updateSources(["pkg1|file.foo"]); | |
| 381 expectAsset("pkg1|file.bar", "file.bar"); | |
| 382 expectNoAsset("pkg2|file.baz"); | |
| 383 buildShouldSucceed(); | |
| 384 }); | |
| 385 | |
| 386 test("doesn't emit a result until all builds are finished", () { | |
| 387 var rewrite = new RewriteTransformer("txt", "out"); | |
| 388 initGraph([ | |
| 389 "pkg1|foo.txt", | |
| 390 "pkg2|foo.txt" | |
| 391 ], {"pkg1": [[rewrite]], "pkg2": [[rewrite]]}); | |
| 392 | |
| 393 // First, run both packages' transformers so both packages are successful. | |
| 394 updateSources(["pkg1|foo.txt", "pkg2|foo.txt"]); | |
| 395 expectAsset("pkg1|foo.out", "foo.out"); | |
| 396 expectAsset("pkg2|foo.out", "foo.out"); | |
| 397 buildShouldSucceed(); | |
| 398 | |
| 399 // pkg1 is still successful, but pkg2 is waiting on the provider, so the | |
| 400 // overall build shouldn't finish. | |
| 401 pauseProvider(); | |
| 402 schedule(() => updateSources(["pkg2|foo.txt"])); | |
| 403 expectAsset("pkg1|foo.out", "foo.out"); | |
| 404 buildShouldNotBeDone(); | |
| 405 | |
| 406 // Now that the provider is unpaused, pkg2's transforms finish and the | |
| 407 // overall build succeeds. | |
| 408 resumeProvider(); | |
| 409 buildShouldSucceed(); | |
| 410 }); | |
| 411 } | |
| OLD | NEW |