| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library barback.test.utils; | 5 library barback.test.utils; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:collection'; |
| 8 import 'dart:convert' show Encoding; | 9 import 'dart:convert' show Encoding; |
| 9 | 10 |
| 10 import 'package:barback/barback.dart'; | 11 import 'package:barback/barback.dart'; |
| 12 import 'package:barback/src/cancelable_future.dart'; |
| 11 import 'package:barback/src/utils.dart'; | 13 import 'package:barback/src/utils.dart'; |
| 12 import 'package:barback/src/utils/cancelable_future.dart'; | |
| 13 import 'package:path/path.dart' as pathos; | 14 import 'package:path/path.dart' as pathos; |
| 14 import 'package:scheduled_test/scheduled_test.dart'; | 15 import 'package:scheduled_test/scheduled_test.dart'; |
| 15 import 'package:stack_trace/stack_trace.dart'; | 16 import 'package:stack_trace/stack_trace.dart'; |
| 16 import 'package:unittest/compact_vm_config.dart'; | 17 import 'package:unittest/compact_vm_config.dart'; |
| 17 | 18 |
| 18 export 'transformer/bad.dart'; | 19 export 'transformer/bad.dart'; |
| 19 export 'transformer/bad_log.dart'; | 20 export 'transformer/bad_log.dart'; |
| 20 export 'transformer/catch_asset_not_found.dart'; | 21 export 'transformer/catch_asset_not_found.dart'; |
| 21 export 'transformer/check_content.dart'; | 22 export 'transformer/check_content.dart'; |
| 22 export 'transformer/check_content_and_rename.dart'; | 23 export 'transformer/check_content_and_rename.dart'; |
| 23 export 'transformer/conditionally_consume_primary.dart'; | 24 export 'transformer/conditionally_consume_primary.dart'; |
| 24 export 'transformer/create_asset.dart'; | 25 export 'transformer/create_asset.dart'; |
| 25 export 'transformer/declare_assets.dart'; | |
| 26 export 'transformer/declaring_bad.dart'; | |
| 27 export 'transformer/declaring_check_content_and_rename.dart'; | |
| 28 export 'transformer/declaring_rewrite.dart'; | |
| 29 export 'transformer/emit_nothing.dart'; | 26 export 'transformer/emit_nothing.dart'; |
| 30 export 'transformer/has_input.dart'; | 27 export 'transformer/has_input.dart'; |
| 31 export 'transformer/lazy_assets.dart'; | |
| 32 export 'transformer/lazy_bad.dart'; | 28 export 'transformer/lazy_bad.dart'; |
| 33 export 'transformer/lazy_check_content_and_rename.dart'; | |
| 34 export 'transformer/lazy_many_to_one.dart'; | 29 export 'transformer/lazy_many_to_one.dart'; |
| 35 export 'transformer/lazy_rewrite.dart'; | 30 export 'transformer/lazy_rewrite.dart'; |
| 36 export 'transformer/many_to_one.dart'; | 31 export 'transformer/many_to_one.dart'; |
| 37 export 'transformer/mock.dart'; | 32 export 'transformer/mock.dart'; |
| 38 export 'transformer/one_to_many.dart'; | 33 export 'transformer/one_to_many.dart'; |
| 39 export 'transformer/rewrite.dart'; | 34 export 'transformer/rewrite.dart'; |
| 40 export 'transformer/sync_rewrite.dart'; | |
| 41 | 35 |
| 42 var _configured = false; | 36 var _configured = false; |
| 43 | 37 |
| 44 MockProvider _provider; | 38 MockProvider _provider; |
| 45 Barback _barback; | 39 Barback _barback; |
| 46 | 40 |
| 47 /// Calls to [buildShouldSucceed] and [buildShouldFail] set expectations on | 41 /// Calls to [buildShouldSucceed] and [buildShouldFail] set expectations on |
| 48 /// successive [BuildResult]s from [_barback]. This keeps track of how many | 42 /// successive [BuildResult]s from [_barback]. This keeps track of how many |
| 49 /// calls have already been made so later calls know which result to look for. | 43 /// calls have already been made so later calls know which result to look for. |
| 50 int _nextBuildResult; | 44 int _nextBuildResult; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 74 /// [AssetId] and the value should be a string defining the contents of that | 68 /// [AssetId] and the value should be a string defining the contents of that |
| 75 /// asset. | 69 /// asset. |
| 76 /// | 70 /// |
| 77 /// [transformers] is a map from package names to the transformers for each | 71 /// [transformers] is a map from package names to the transformers for each |
| 78 /// package. | 72 /// package. |
| 79 void initGraph([assets, | 73 void initGraph([assets, |
| 80 Map<String, Iterable<Iterable<Transformer>>> transformers]) { | 74 Map<String, Iterable<Iterable<Transformer>>> transformers]) { |
| 81 if (assets == null) assets = []; | 75 if (assets == null) assets = []; |
| 82 if (transformers == null) transformers = {}; | 76 if (transformers == null) transformers = {}; |
| 83 | 77 |
| 84 _provider = new MockProvider(assets, additionalPackages: transformers.keys); | 78 var assetList; |
| 79 if (assets is Map) { |
| 80 assetList = assets.keys.map((asset) { |
| 81 var id = new AssetId.parse(asset); |
| 82 return new _MockAsset(id, assets[asset]); |
| 83 }); |
| 84 } else if (assets is Iterable) { |
| 85 assetList = assets.map((asset) { |
| 86 var id = new AssetId.parse(asset); |
| 87 var contents = pathos.basenameWithoutExtension(id.path); |
| 88 return new _MockAsset(id, contents); |
| 89 }); |
| 90 } |
| 91 |
| 92 var assetMap = mapMapValues(groupBy(assetList, (asset) => asset.id.package), |
| 93 (package, assets) => new AssetSet.from(assets)); |
| 94 |
| 95 // Make sure that packages that have transformers but no assets are considered |
| 96 // by MockProvider to exist. |
| 97 for (var package in transformers.keys) { |
| 98 assetMap.putIfAbsent(package, () => new AssetSet()); |
| 99 } |
| 100 |
| 101 _provider = new MockProvider(assetMap); |
| 85 _barback = new Barback(_provider); | 102 _barback = new Barback(_provider); |
| 86 // Add a dummy listener to the log so it doesn't print to stdout. | 103 // Add a dummy listener to the log so it doesn't print to stdout. |
| 87 _barback.log.listen((_) {}); | 104 _barback.log.listen((_) {}); |
| 88 _nextBuildResult = 0; | 105 _nextBuildResult = 0; |
| 89 _nextLog = 0; | 106 _nextLog = 0; |
| 90 | 107 |
| 91 schedule(() => transformers.forEach(_barback.updateTransformers)); | 108 schedule(() => transformers.forEach(_barback.updateTransformers)); |
| 92 | 109 |
| 93 // There should be one successful build after adding all the transformers but | 110 // There should be one successful build after adding all the transformers but |
| 94 // before adding any sources. | 111 // before adding any sources. |
| (...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 457 currentSchedule.signalError(error); | 474 currentSchedule.signalError(error); |
| 458 }); | 475 }); |
| 459 | 476 |
| 460 return delay.then((_) => cancelable.cancel()); | 477 return delay.then((_) => cancelable.cancel()); |
| 461 } | 478 } |
| 462 | 479 |
| 463 /// An [AssetProvider] that provides the given set of assets. | 480 /// An [AssetProvider] that provides the given set of assets. |
| 464 class MockProvider implements PackageProvider { | 481 class MockProvider implements PackageProvider { |
| 465 Iterable<String> get packages => _assets.keys; | 482 Iterable<String> get packages => _assets.keys; |
| 466 | 483 |
| 467 final Map<String, AssetSet> _assets; | 484 Map<String, AssetSet> _assets; |
| 468 | 485 |
| 469 /// The set of assets for which [MockLoadException]s should be emitted if | 486 /// The set of assets for which [MockLoadException]s should be emitted if |
| 470 /// they're loaded. | 487 /// they're loaded. |
| 471 final _errors = new Set<AssetId>(); | 488 final _errors = new Set<AssetId>(); |
| 472 | 489 |
| 473 /// The completer that [getAsset()] is waiting on to complete when paused. | 490 /// The completer that [getAsset()] is waiting on to complete when paused. |
| 474 /// | 491 /// |
| 475 /// If `null` it will return the asset immediately. | 492 /// If `null` it will return the asset immediately. |
| 476 Completer _pauseCompleter; | 493 Completer _pauseCompleter; |
| 477 | 494 |
| 478 /// Tells the provider to wait during [getAsset] until [complete()] | 495 /// Tells the provider to wait during [getAsset] until [complete()] |
| 479 /// is called. | 496 /// is called. |
| 480 /// | 497 /// |
| 481 /// Lets you test the asynchronous behavior of loading. | 498 /// Lets you test the asynchronous behavior of loading. |
| 482 void _pause() { | 499 void _pause() { |
| 483 _pauseCompleter = new Completer(); | 500 _pauseCompleter = new Completer(); |
| 484 } | 501 } |
| 485 | 502 |
| 486 void _resume() { | 503 void _resume() { |
| 487 _pauseCompleter.complete(); | 504 _pauseCompleter.complete(); |
| 488 _pauseCompleter = null; | 505 _pauseCompleter = null; |
| 489 } | 506 } |
| 490 | 507 |
| 491 MockProvider(assets, {Iterable<String> additionalPackages}) | 508 MockProvider(this._assets) { |
| 492 : _assets = _normalizeAssets(assets, additionalPackages); | |
| 493 | |
| 494 static Map<String, AssetSet> _normalizeAssets(assets, | |
| 495 Iterable<String> additionalPackages) { | |
| 496 var assetList; | |
| 497 if (assets is Map) { | |
| 498 assetList = assets.keys.map((asset) { | |
| 499 var id = new AssetId.parse(asset); | |
| 500 return new _MockAsset(id, assets[asset]); | |
| 501 }); | |
| 502 } else if (assets is Iterable) { | |
| 503 assetList = assets.map((asset) { | |
| 504 var id = new AssetId.parse(asset); | |
| 505 var contents = pathos.basenameWithoutExtension(id.path); | |
| 506 return new _MockAsset(id, contents); | |
| 507 }); | |
| 508 } | |
| 509 | |
| 510 var assetMap = mapMapValues(groupBy(assetList, (asset) => asset.id.package), | |
| 511 (package, assets) => new AssetSet.from(assets)); | |
| 512 | |
| 513 // Make sure that packages that have transformers but no assets are | |
| 514 // considered by MockProvider to exist. | |
| 515 if (additionalPackages != null) { | |
| 516 for (var package in additionalPackages) { | |
| 517 assetMap.putIfAbsent(package, () => new AssetSet()); | |
| 518 } | |
| 519 } | |
| 520 | |
| 521 // If there are no assets or transformers, add a dummy package. This better | 509 // If there are no assets or transformers, add a dummy package. This better |
| 522 // simulates the real world, where there'll always be at least the | 510 // simulates the real world, where there'll always be at least the |
| 523 // entrypoint package. | 511 // entrypoint package. |
| 524 return assetMap.isEmpty ? {"app": new AssetSet()} : assetMap; | 512 if (_assets.isEmpty) { |
| 513 _assets = {"app": new AssetSet()}; |
| 514 } |
| 525 } | 515 } |
| 526 | 516 |
| 527 void _modifyAsset(String name, String contents) { | 517 void _modifyAsset(String name, String contents) { |
| 528 var id = new AssetId.parse(name); | 518 var id = new AssetId.parse(name); |
| 529 _errors.remove(id); | 519 _errors.remove(id); |
| 530 (_assets[id.package][id] as _MockAsset).contents = contents; | 520 (_assets[id.package][id] as _MockAsset).contents = contents; |
| 531 } | 521 } |
| 532 | 522 |
| 533 void _setAssetError(String name) { | 523 void _setAssetError(String name) { |
| 534 _errors.add(new AssetId.parse(name)); | 524 _errors.add(new AssetId.parse(name)); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 582 | 572 |
| 583 _MockAsset(this.id, this.contents); | 573 _MockAsset(this.id, this.contents); |
| 584 | 574 |
| 585 Future<String> readAsString({Encoding encoding}) => | 575 Future<String> readAsString({Encoding encoding}) => |
| 586 new Future.value(contents); | 576 new Future.value(contents); |
| 587 | 577 |
| 588 Stream<List<int>> read() => throw new UnimplementedError(); | 578 Stream<List<int>> read() => throw new UnimplementedError(); |
| 589 | 579 |
| 590 String toString() => "MockAsset $id $contents"; | 580 String toString() => "MockAsset $id $contents"; |
| 591 } | 581 } |
| OLD | NEW |