Skip to content

Commit 49c9c6a

Browse files
committed
add FileBasedAsset* tests
1 parent 32a3b04 commit 49c9c6a

File tree

19 files changed

+138
-12
lines changed

19 files changed

+138
-12
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ build/
55
pubspec.lock
66

77
# Include .packages files from tests which are hand coded
8-
!test/package_graph/**/.packages
8+
!test/fixtures/**/.packages

lib/build.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44
export 'src/asset/asset.dart';
55
export 'src/asset/exceptions.dart';
6+
export 'src/asset/file_based.dart';
67
export 'src/asset/id.dart';
78
export 'src/asset/reader.dart';
89
export 'src/asset/writer.dart';

lib/src/asset/exceptions.dart

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,16 @@ class InvalidOutputException implements Exception {
2828
InvalidOutputException(this.asset);
2929

3030
@override
31-
String toString() => 'InvalidOutputException: $asset';
31+
String toString() => 'InvalidOutputException: $asset\n'
32+
'Files may only be output in the root (application) package.';
33+
}
34+
35+
class InvalidInputException implements Exception {
36+
final AssetId assetId;
37+
38+
InvalidInputException(this.assetId);
39+
40+
@override
41+
String toString() => 'InvalidInputException: $assetId\n'
42+
'For package dependencies, only files under `lib` may be used as inputs.';
3243
}

lib/src/asset/file_based.dart

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,29 @@ class FileBasedAssetReader implements AssetReader {
2323
FileBasedAssetReader(this.packageGraph);
2424

2525
@override
26-
Future<bool> hasInput(AssetId id) => _fileFor(id, packageGraph).exists();
26+
Future<bool> hasInput(AssetId id) async {
27+
_checkInput(id);
28+
return _fileFor(id, packageGraph).exists();
29+
}
2730

2831
@override
2932
Future<String> readAsString(AssetId id, {Encoding encoding: UTF8}) async {
33+
_checkInput(id);
34+
3035
var file = await _fileFor(id, packageGraph);
3136
if (!await file.exists()) {
3237
throw new AssetNotFoundException(id);
3338
}
3439
return file.readAsString(encoding: encoding);
3540
}
41+
42+
/// Checks that [id] is a valid input, and throws an [InvalidInputException]
43+
/// if its not.
44+
void _checkInput(AssetId id) {
45+
if (id.package != packageGraph.root.name && !id.path.startsWith('lib/')) {
46+
throw new InvalidInputException(id);
47+
}
48+
}
3649
}
3750

3851
/// Basic [AssetWriter] which uses a [PackageGraph] to look up where to write
@@ -43,17 +56,14 @@ class FileBasedAssetWriter implements AssetWriter {
4356
FileBasedAssetWriter(this.packageGraph);
4457

4558
@override
46-
// Not using async/await here so that the first check can throw synchronously
47-
// which helps with debugging.
48-
Future writeAsString(Asset asset, {Encoding encoding: UTF8}) {
59+
Future writeAsString(Asset asset, {Encoding encoding: UTF8}) async {
4960
if (asset.id.package != packageGraph.root.name) {
5061
throw new InvalidOutputException(asset);
5162
}
5263

5364
var file = _fileFor(asset.id, packageGraph);
54-
return file.create(recursive: true).then((_) {
55-
return file.writeAsString(asset.stringContents, encoding: encoding);
56-
});
65+
await file.create(recursive: true);
66+
await file.writeAsString(asset.stringContents, encoding: encoding);
5767
}
5868
}
5969

test/asset/file_based_test.dart

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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 'dart:async';
6+
import 'dart:io';
7+
8+
import 'package:test/test.dart';
9+
10+
import 'package:build/build.dart';
11+
import '../common/common.dart';
12+
13+
main() async {
14+
final packageGraph = new PackageGraph.forPath('test/fixtures/basic_pkg');
15+
final assetNotFoundException = new isInstanceOf<AssetNotFoundException>();
16+
final invalidInputException = new isInstanceOf<InvalidInputException>();
17+
final invalidOutputException = new isInstanceOf<InvalidOutputException>();
18+
final packageNotFoundException = new isInstanceOf<PackageNotFoundException>();
19+
20+
group('FileBasedAssetReader', () {
21+
final reader = new FileBasedAssetReader(packageGraph);
22+
23+
test('can read any application package files', () async {
24+
expect(await reader.readAsString(makeAssetId('basic_pkg|hello.txt')),
25+
'world\n');
26+
expect(await reader.readAsString(makeAssetId('basic_pkg|lib/hello.txt')),
27+
'world\n');
28+
expect(await reader.readAsString(makeAssetId('basic_pkg|web/hello.txt')),
29+
'world\n');
30+
});
31+
32+
test('can only read package dependency files in the lib dir', () async {
33+
expect(await reader.readAsString(makeAssetId('a|lib/a.txt')), 'A\n');
34+
expect(reader.readAsString(makeAssetId('a|web/a.txt')),
35+
throwsA(invalidInputException));
36+
expect(reader.readAsString(makeAssetId('a|a.txt')),
37+
throwsA(invalidInputException));
38+
});
39+
40+
test('can check for existence of any application package files', () async {
41+
expect(await reader.hasInput(makeAssetId('basic_pkg|hello.txt')), isTrue);
42+
expect(await reader.hasInput(makeAssetId('basic_pkg|lib/hello.txt')),
43+
isTrue);
44+
expect(await reader.hasInput(makeAssetId('basic_pkg|web/hello.txt')),
45+
isTrue);
46+
47+
expect(await reader.hasInput(makeAssetId('basic_pkg|a.txt')), isFalse);
48+
expect(
49+
await reader.hasInput(makeAssetId('basic_pkg|lib/a.txt')), isFalse);
50+
});
51+
52+
test('can only check for existence of package dependency files in lib',
53+
() async {
54+
expect(await reader.hasInput(makeAssetId('a|lib/a.txt')), isTrue);
55+
expect(await reader.hasInput(makeAssetId('a|lib/b.txt')), isFalse);
56+
expect(reader.hasInput(makeAssetId('a|web/a.txt')),
57+
throwsA(invalidInputException));
58+
expect(reader.hasInput(makeAssetId('a|a.txt')),
59+
throwsA(invalidInputException));
60+
expect(reader.hasInput(makeAssetId('foo|bar.txt')),
61+
throwsA(invalidInputException));
62+
});
63+
64+
test('throws when attempting to read a non-existent file', () async {
65+
expect(reader.readAsString(makeAssetId('basic_pkg|foo.txt')),
66+
throwsA(assetNotFoundException));
67+
expect(reader.readAsString(makeAssetId('a|lib/b.txt')),
68+
throwsA(assetNotFoundException));
69+
expect(reader.readAsString(makeAssetId('foo|lib/bar.txt')),
70+
throwsA(packageNotFoundException));
71+
});
72+
});
73+
74+
group('FileBasedAssetWriter', () {
75+
final writer = new FileBasedAssetWriter(packageGraph);
76+
77+
test('can output files in the application package', () async {
78+
var asset = makeAsset('basic_pkg|test_file.txt', 'test');
79+
await writer.writeAsString(asset);
80+
var id = asset.id;
81+
var file = new File('test/fixtures/${id.package}/${id.path}');
82+
expect(await file.exists(), isTrue);
83+
expect(await file.readAsString(), 'test');
84+
await file.delete();
85+
});
86+
87+
test('can\'t output files in package dependencies', () async {
88+
var asset = makeAsset('a|test.txt');
89+
expect(writer.writeAsString(asset), throwsA(invalidOutputException));
90+
});
91+
92+
test('can\'t output files in arbitrary packages', () async {
93+
var asset = makeAsset('foo|bar.txt');
94+
expect(writer.writeAsString(asset), throwsA(invalidOutputException));
95+
});
96+
});
97+
}
File renamed without changes.

test/fixtures/basic_pkg/hello.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
world
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
world
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
A
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
A

0 commit comments

Comments
 (0)