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