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