2015-03-06 14:10:14 -05:00
|
|
|
library angular2.test.transform.common.read_file;
|
|
|
|
|
2015-03-12 16:31:32 -04:00
|
|
|
import 'dart:async';
|
2015-04-13 19:54:09 -04:00
|
|
|
import 'dart:convert';
|
2015-03-06 14:10:14 -05:00
|
|
|
import 'dart:io';
|
|
|
|
|
2015-03-12 16:31:32 -04:00
|
|
|
import 'package:angular2/src/transform/common/asset_reader.dart';
|
|
|
|
import 'package:barback/barback.dart';
|
|
|
|
|
2015-03-06 14:10:14 -05:00
|
|
|
/// Smooths over differences in CWD between IDEs and running tests in Travis.
|
|
|
|
String readFile(String path) {
|
|
|
|
for (var myPath in [path, 'test/transform/${path}']) {
|
|
|
|
var file = new File(myPath);
|
|
|
|
if (file.existsSync()) {
|
|
|
|
return file.readAsStringSync();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2015-03-12 16:31:32 -04:00
|
|
|
|
|
|
|
class TestAssetReader implements AssetReader {
|
2015-05-08 20:29:21 -04:00
|
|
|
/// This allows "faking"
|
|
|
|
final Map<AssetId, String> _overrideAssets = <AssetId, String>{};
|
|
|
|
|
|
|
|
Future<String> readAsString(AssetId id, {Encoding encoding}) {
|
|
|
|
if (_overrideAssets.containsKey(id)) {
|
|
|
|
return new Future.value(_overrideAssets[id]);
|
|
|
|
} else {
|
|
|
|
return new Future.value(readFile(id.path));
|
|
|
|
}
|
|
|
|
}
|
2015-03-12 16:31:32 -04:00
|
|
|
|
|
|
|
Future<bool> hasInput(AssetId id) {
|
2015-05-08 20:29:21 -04:00
|
|
|
var exists = _overrideAssets.containsKey(id);
|
|
|
|
if (exists) return new Future.value(true);
|
|
|
|
|
2015-03-12 16:31:32 -04:00
|
|
|
for (var myPath in [id.path, 'test/transform/${id.path}']) {
|
|
|
|
var file = new File(myPath);
|
|
|
|
exists = exists || file.existsSync();
|
|
|
|
}
|
|
|
|
return new Future.value(exists);
|
|
|
|
}
|
2015-05-08 20:29:21 -04:00
|
|
|
|
|
|
|
void addAsset(AssetId id, String contents) {
|
|
|
|
_overrideAssets[id] = contents;
|
|
|
|
}
|
2015-03-12 16:31:32 -04:00
|
|
|
}
|