| 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 // Test that the compiler can handle imports when package root has not been set. | 5 // Test that the compiler can handle imports when package root has not been set. |
| 6 | 6 |
| 7 library dart2js.test.package_root; | 7 library dart2js.test.package_root; |
| 8 | 8 |
| 9 import 'dart:async'; | 9 import 'dart:async'; |
| 10 | 10 |
| 11 import 'package:async_helper/async_helper.dart'; | 11 import 'package:async_helper/async_helper.dart'; |
| 12 import 'package:expect/expect.dart'; | 12 import 'package:expect/expect.dart'; |
| 13 import 'package:compiler/compiler.dart' | 13 import 'package:compiler/compiler.dart' |
| 14 show DiagnosticHandler, Diagnostic, PackagesDiscoveryProvider; | 14 show DiagnosticHandler, Diagnostic, PackagesDiscoveryProvider; |
| 15 import 'package:compiler/src/dart2jslib.dart' |
| 16 show MessageKind; |
| 15 import 'package:package_config/packages.dart'; | 17 import 'package:package_config/packages.dart'; |
| 16 | 18 |
| 17 import 'memory_compiler.dart'; | 19 import 'memory_compiler.dart'; |
| 18 import 'memory_source_file_helper.dart'; | 20 import 'memory_source_file_helper.dart'; |
| 19 | 21 |
| 20 const MEMORY_SOURCE_FILES = const { | 22 const MEMORY_SOURCE_FILES = const { |
| 21 'main.dart': ''' | 23 'main.dart': ''' |
| 22 | 24 |
| 23 import 'package:foo/foo.dart'; | 25 import 'package:foo/foo.dart'; |
| 24 | 26 |
| 25 main() {} | 27 main() {} |
| 26 ''', | 28 ''', |
| 27 'package.config': ''' | 29 'package.config': ''' |
| 28 ''', | 30 ''', |
| 29 }; | 31 }; |
| 30 | 32 |
| 31 final Uri PACKAGE_CONFIG_URI = Uri.parse('memory:package.config'); | 33 final Uri PACKAGE_CONFIG_URI = Uri.parse('memory:package.config'); |
| 32 | 34 |
| 33 void runCompiler(Uri main, | 35 Future runTest(Uri main, |
| 34 bool checkError(DiagnosticMessage message), | 36 MessageKind expectedMessageKind, |
| 35 {Uri packageRoot, | 37 {Uri packageRoot, |
| 36 Uri packageConfig, | 38 Uri packageConfig, |
| 37 PackagesDiscoveryProvider packagesDiscoveryProvider}) { | 39 PackagesDiscoveryProvider packagesDiscoveryProvider}) async { |
| 38 DiagnosticCollector collector = new DiagnosticCollector(); | 40 DiagnosticCollector collector = new DiagnosticCollector(); |
| 39 Compiler compiler = compilerFor( | 41 await runCompiler( |
| 40 MEMORY_SOURCE_FILES, | 42 entryPoint: main, |
| 43 memorySourceFiles: MEMORY_SOURCE_FILES, |
| 41 diagnosticHandler: collector, | 44 diagnosticHandler: collector, |
| 42 packageRoot: packageRoot, | 45 packageRoot: packageRoot, |
| 43 packageConfig: packageConfig, | 46 packageConfig: packageConfig, |
| 44 packagesDiscoveryProvider: packagesDiscoveryProvider); | 47 packagesDiscoveryProvider: packagesDiscoveryProvider); |
| 45 | 48 Expect.equals(1, collector.errors.length, |
| 46 asyncTest(() => compiler.run(main).then((_) { | 49 "Unexpected errors: ${collector.errors}"); |
| 47 Expect.equals(1, collector.errors.length, | 50 Expect.equals(expectedMessageKind, collector.errors.first.message.kind, |
| 48 "Unexpected errors: ${collector.errors}"); | 51 "Unexpected error: ${collector.errors.first}"); |
| 49 Expect.isTrue(checkError(collector.errors.first), | |
| 50 "Unexpected error: ${collector.errors.first}"); | |
| 51 })); | |
| 52 } | 52 } |
| 53 | 53 |
| 54 void main() { | 54 void main() { |
| 55 Uri script = currentDirectory.resolveUri(Platform.script); | 55 asyncTest(() async { |
| 56 Uri packageRoot = script.resolve('./packages/'); | 56 Uri script = currentDirectory.resolveUri(Platform.script); |
| 57 Uri packageRoot = script.resolve('./packages/'); |
| 57 | 58 |
| 58 PackagesDiscoveryProvider noPackagesDiscovery = (Uri uri) { | 59 PackagesDiscoveryProvider noPackagesDiscovery = (Uri uri) { |
| 59 return new Future.value(Packages.noPackages); | 60 return new Future.value(Packages.noPackages); |
| 60 }; | 61 }; |
| 61 | 62 |
| 62 bool containsErrorReading(DiagnosticMessage message) { | 63 await runTest( |
| 63 return message.message.contains("Error reading "); | 64 Uri.parse('memory:main.dart'), |
| 64 } | 65 MessageKind.READ_SCRIPT_ERROR, |
| 66 packageRoot: packageRoot); |
| 67 await runTest( |
| 68 Uri.parse('memory:main.dart'), |
| 69 MessageKind.LIBRARY_NOT_FOUND, |
| 70 packageConfig: PACKAGE_CONFIG_URI); |
| 71 await runTest( |
| 72 Uri.parse('memory:main.dart'), |
| 73 MessageKind.LIBRARY_NOT_FOUND, |
| 74 packagesDiscoveryProvider: noPackagesDiscovery); |
| 65 | 75 |
| 66 bool isLibraryNotFound(DiagnosticMessage message) { | 76 await runTest( |
| 67 return message.message.startsWith("Library not found "); | 77 Uri.parse('package:foo/foo.dart'), |
| 68 } | 78 MessageKind.READ_SELF_ERROR, |
| 69 | 79 packageRoot: packageRoot); |
| 70 runCompiler(Uri.parse('memory:main.dart'), | 80 await runTest( |
| 71 containsErrorReading, | 81 Uri.parse('package:foo/foo.dart'), |
| 72 packageRoot: packageRoot); | 82 MessageKind.LIBRARY_NOT_FOUND, |
| 73 runCompiler(Uri.parse('memory:main.dart'), | 83 packageConfig: PACKAGE_CONFIG_URI); |
| 74 isLibraryNotFound, | 84 await runTest( |
| 75 packageConfig: PACKAGE_CONFIG_URI); | 85 Uri.parse('package:foo/foo.dart'), |
| 76 runCompiler(Uri.parse('memory:main.dart'), | 86 MessageKind.LIBRARY_NOT_FOUND, |
| 77 isLibraryNotFound, | 87 packagesDiscoveryProvider: noPackagesDiscovery); |
| 78 packagesDiscoveryProvider: noPackagesDiscovery); | 88 }); |
| 79 | |
| 80 runCompiler(Uri.parse('package:foo/foo.dart'), | |
| 81 containsErrorReading, | |
| 82 packageRoot: packageRoot); | |
| 83 runCompiler(Uri.parse('package:foo/foo.dart'), | |
| 84 isLibraryNotFound, | |
| 85 packageConfig: PACKAGE_CONFIG_URI); | |
| 86 runCompiler(Uri.parse('package:foo/foo.dart'), | |
| 87 isLibraryNotFound, | |
| 88 packagesDiscoveryProvider: noPackagesDiscovery); | |
| 89 } | 89 } |
| OLD | NEW |