| 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.source_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 source asset", () { |
| 18 var provider = new MockProvider(["app|foo.txt"]); |
| 19 var graph = new AssetGraph(provider, []); |
| 20 graph.updateSources([new AssetId.parse("app|foo.txt")]); |
| 21 |
| 22 expectAsset(graph, "app|foo.txt"); |
| 23 }); |
| 24 |
| 25 test("doesn't get an unknown source", () { |
| 26 var provider = new MockProvider(["app|foo.txt"]); |
| 27 var graph = new AssetGraph(provider, []); |
| 28 |
| 29 expectNoAsset(graph, "app|unknown.txt"); |
| 30 }); |
| 31 |
| 32 test("doesn't get an asset that isn't an updated source", () { |
| 33 var provider = new MockProvider(["app|foo.txt"]); |
| 34 var graph = new AssetGraph(provider, []); |
| 35 |
| 36 // Sources must be explicitly made visible to barback by calling |
| 37 // updateSources() on them. It isn't enough for the provider to be able |
| 38 // to provide it. |
| 39 // |
| 40 // This lets you distinguish between sources that you want to be primaries |
| 41 // and the larger set of inputs that those primaries are allowed to pull in. |
| 42 expectNoAsset(graph, "app|foo.txt"); |
| 43 }); |
| 44 |
| 45 test("gets a source asset if not transformed", () { |
| 46 var provider = new MockProvider(["app|foo.txt"]); |
| 47 var graph = new AssetGraph(provider, [ |
| 48 [new RewriteTransformer("nottxt", "whatever")] |
| 49 ]); |
| 50 graph.updateSources([new AssetId.parse("app|foo.txt")]); |
| 51 |
| 52 expectAsset(graph, "app|foo.txt"); |
| 53 }); |
| 54 |
| 55 test("doesn't get a removed source", () { |
| 56 var provider = new MockProvider(["app|foo.txt"]); |
| 57 var graph = new AssetGraph(provider, [[]]); |
| 58 graph.updateSources([new AssetId.parse("app|foo.txt")]); |
| 59 |
| 60 expectAsset(graph, "app|foo.txt"); |
| 61 |
| 62 schedule(() { |
| 63 graph.removeSources([new AssetId.parse("app|foo.txt")]); |
| 64 }); |
| 65 |
| 66 expectNoAsset(graph, "app|foo.txt"); |
| 67 }); |
| 68 |
| 69 test("collapses redundant updates", () { |
| 70 var provider = new MockProvider(["app|foo.blub"]); |
| 71 var transformer = new RewriteTransformer("blub", "blab"); |
| 72 var graph = new AssetGraph(provider, [[transformer]]); |
| 73 |
| 74 schedule(() { |
| 75 // Make a bunch of synchronous update calls. |
| 76 graph.updateSources([new AssetId.parse("app|foo.blub")]); |
| 77 graph.updateSources([new AssetId.parse("app|foo.blub")]); |
| 78 graph.updateSources([new AssetId.parse("app|foo.blub")]); |
| 79 graph.updateSources([new AssetId.parse("app|foo.blub")]); |
| 80 }); |
| 81 |
| 82 expectAsset(graph, "app|foo.blab", "foo.blab"); |
| 83 |
| 84 schedule(() { |
| 85 expect(transformer.numRuns, equals(1)); |
| 86 }); |
| 87 }); |
| 88 |
| 89 test("a removal cancels out an update", () { |
| 90 var provider = new MockProvider(["app|foo.txt"]); |
| 91 var graph = new AssetGraph(provider, [[]]); |
| 92 |
| 93 schedule(() { |
| 94 graph.updateSources([new AssetId.parse("app|foo.txt")]); |
| 95 graph.removeSources([new AssetId.parse("app|foo.txt")]); |
| 96 }); |
| 97 |
| 98 expectNoAsset(graph, "app|foo.txt"); |
| 99 }); |
| 100 |
| 101 test("an update cancels out a removal", () { |
| 102 var provider = new MockProvider(["app|foo.txt"]); |
| 103 var graph = new AssetGraph(provider, [[]]); |
| 104 |
| 105 schedule(() { |
| 106 graph.removeSources([new AssetId.parse("app|foo.txt")]); |
| 107 graph.updateSources([new AssetId.parse("app|foo.txt")]); |
| 108 }); |
| 109 |
| 110 expectAsset(graph, "app|foo.txt"); |
| 111 }); |
| 112 |
| 113 test("restarts a build if a source is updated while sources are loading", () { |
| 114 var provider = new MockProvider(["app|foo.txt", "app|other.bar"]); |
| 115 var transformer = new RewriteTransformer("txt", "out"); |
| 116 var graph = new AssetGraph(provider, [[transformer]]); |
| 117 |
| 118 var numBuilds = 0; |
| 119 var buildCompleter = new Completer(); |
| 120 graph.results.listen(wrapAsync((result) { |
| 121 expect(result.error, isNull); |
| 122 numBuilds++; |
| 123 |
| 124 // There should be two builds, one for each update call. |
| 125 if (numBuilds == 2) buildCompleter.complete(); |
| 126 })); |
| 127 |
| 128 // Run the whole graph so all nodes are clean. |
| 129 graph.updateSources([ |
| 130 new AssetId.parse("app|foo.txt"), |
| 131 new AssetId.parse("app|other.bar") |
| 132 ]); |
| 133 expectAsset(graph, "app|foo.out", "foo.out"); |
| 134 expectAsset(graph, "app|other.bar"); |
| 135 |
| 136 schedule(() { |
| 137 // Make the provider slow to load a source. |
| 138 provider.wait(); |
| 139 |
| 140 // Update an asset that doesn't trigger any transformers. |
| 141 graph.updateSources([new AssetId.parse("app|other.bar")]); |
| 142 }); |
| 143 |
| 144 schedule(() { |
| 145 // Now update an asset that does trigger a transformer. |
| 146 graph.updateSources([new AssetId.parse("app|foo.txt")]); |
| 147 }); |
| 148 |
| 149 schedule(() { |
| 150 provider.complete(); |
| 151 }); |
| 152 |
| 153 schedule(() { |
| 154 // Wait until the build has completed. |
| 155 return buildCompleter.future; |
| 156 }); |
| 157 |
| 158 schedule(() { |
| 159 expect(transformer.numRuns, equals(2)); |
| 160 }); |
| 161 }); |
| 162 } |
| OLD | NEW |