|
| 1 | +// Copyright (c) 2016, 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 | +@TestOn('vm') |
| 5 | +import 'package:test/test.dart'; |
| 6 | + |
| 7 | +import 'package:build/build.dart'; |
| 8 | +import 'package:build/src/builder/build_step_impl.dart'; |
| 9 | + |
| 10 | +import '../common/common.dart'; |
| 11 | + |
| 12 | +main() { |
| 13 | + group('BuildStepImpl ', () { |
| 14 | + AssetWriter writer; |
| 15 | + AssetReader reader; |
| 16 | + |
| 17 | + group('with reader/writer stub', () { |
| 18 | + Asset primary; |
| 19 | + BuildStepImpl buildStep; |
| 20 | + |
| 21 | + setUp(() { |
| 22 | + reader = new StubAssetReader(); |
| 23 | + writer = new StubAssetWriter(); |
| 24 | + primary = makeAsset(); |
| 25 | + buildStep = new BuildStepImpl(primary, [], reader, writer); |
| 26 | + }); |
| 27 | + |
| 28 | + test('tracks dependencies on read', () async { |
| 29 | + expect(buildStep.dependencies, [primary.id]); |
| 30 | + |
| 31 | + var a1 = makeAssetId(); |
| 32 | + await buildStep.readAsString(a1); |
| 33 | + expect(buildStep.dependencies, [primary.id, a1]); |
| 34 | + |
| 35 | + var a2 = makeAssetId(); |
| 36 | + await buildStep.readAsString(a2); |
| 37 | + expect(buildStep.dependencies, [primary.id, a1, a2]); |
| 38 | + }); |
| 39 | + |
| 40 | + test('addDependency adds dependencies', () { |
| 41 | + expect(buildStep.dependencies, [primary.id]); |
| 42 | + |
| 43 | + var a1 = makeAssetId(); |
| 44 | + buildStep.addDependency(a1); |
| 45 | + expect(buildStep.dependencies, [primary.id, a1]); |
| 46 | + |
| 47 | + var a2 = makeAssetId(); |
| 48 | + buildStep.addDependency(a2); |
| 49 | + expect(buildStep.dependencies, [primary.id, a1, a2]); |
| 50 | + }); |
| 51 | + |
| 52 | + test('tracks outputs', () async { |
| 53 | + var a1 = makeAsset(); |
| 54 | + var a2 = makeAsset(); |
| 55 | + buildStep = new BuildStepImpl(primary, [a1.id, a2.id], reader, writer); |
| 56 | + |
| 57 | + buildStep.writeAsString(a1); |
| 58 | + expect(buildStep.outputs, [a1]); |
| 59 | + |
| 60 | + buildStep.writeAsString(a2); |
| 61 | + expect(buildStep.outputs, [a1, a2]); |
| 62 | + |
| 63 | + expect(buildStep.outputsCompleted, completes); |
| 64 | + }); |
| 65 | + |
| 66 | + test('doesnt allow non-expected outputs', () { |
| 67 | + var asset = makeAsset(); |
| 68 | + expect(() => buildStep.writeAsString(asset), |
| 69 | + throwsA(new isInstanceOf<UnexpectedOutputException>())); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + group('with in memory file system', () { |
| 74 | + InMemoryAssetWriter writer; |
| 75 | + InMemoryAssetReader reader; |
| 76 | + |
| 77 | + setUp(() { |
| 78 | + writer = new InMemoryAssetWriter(); |
| 79 | + reader = new InMemoryAssetReader(writer.assets); |
| 80 | + }); |
| 81 | + |
| 82 | + test('tracks dependencies and outputs when used by a builder', () async { |
| 83 | + var fileCombiner = new FileCombinerBuilder(); |
| 84 | + var primary = 'a|web/primary.txt'; |
| 85 | + var unUsed = 'a|web/not_used.txt'; |
| 86 | + var inputs = makeAssets({ |
| 87 | + primary: 'a|web/a.txt\na|web/b.txt', |
| 88 | + 'a|web/a.txt': 'A', |
| 89 | + 'a|web/b.txt': 'B', |
| 90 | + unUsed: 'C', |
| 91 | + }); |
| 92 | + addAssets(inputs.values, writer); |
| 93 | + var outputId = new AssetId.parse('$primary.combined'); |
| 94 | + var buildStep = new BuildStepImpl( |
| 95 | + inputs[new AssetId.parse(primary)], [outputId], reader, writer); |
| 96 | + |
| 97 | + await fileCombiner.build(buildStep); |
| 98 | + await buildStep.outputsCompleted; |
| 99 | + |
| 100 | + // All the assets should be read and marked as deps, except [unUsed]. |
| 101 | + expect(buildStep.dependencies, |
| 102 | + inputs.keys.where((k) => k != new AssetId.parse(unUsed))); |
| 103 | + |
| 104 | + // One output. |
| 105 | + expect(buildStep.outputs[0].id, outputId); |
| 106 | + expect(buildStep.outputs[0].stringContents, 'AB'); |
| 107 | + expect(writer.assets[outputId], 'AB'); |
| 108 | + }); |
| 109 | + }); |
| 110 | + }); |
| 111 | +} |
0 commit comments