|
| 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 | +} |
0 commit comments