| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 // Dart test program for testing native extensions. | 5 // Dart test program for testing native extensions. |
| 6 | 6 |
| 7 #import("dart:io"); | 7 #import('dart:io'); |
| 8 #import("dart:isolate"); | |
| 9 | 8 |
| 10 // The following source statements, hidden in a string, fool the test script | 9 Future copyFileToDirectory(Path file, Path directory) { |
| 11 // tools/testing/dart/multitest.dart | 10 String src = file.toNativePath(); |
| 12 // into copying the files into the generated_tests directory. | 11 String dst = directory.toNativePath(); |
| 13 // TODO(3919): Rewrite this test, not as a multitest, to copy them manually. | 12 switch (Platform.operatingSystem) { |
| 14 const dummyString = ''' | 13 case 'linux': |
| 15 #source('test_extension_tester.dart'); | 14 case 'macos': |
| 16 #source('test_extension.dart'); | 15 return Process.run('cp', [src, dst]); |
| 17 '''; | 16 case 'windows': |
| 17 return Process.run('cmd.exe', ['/C', 'copy $src $dst']); |
| 18 default: |
| 19 Expect.fail('Unknown operating system ${Platform.operatingSystem}'); |
| 20 } |
| 21 } |
| 22 |
| 23 Path getExtensionPath(Path buildDirectory) { |
| 24 switch (Platform.operatingSystem) { |
| 25 case 'linux': |
| 26 return buildDirectory.append('lib.target/libtest_extension.so'); |
| 27 case 'macos': |
| 28 return buildDirectory.append('libtest_extension.dylib'); |
| 29 case 'windows': |
| 30 return buildDirectory.append('test_extension.dll'); |
| 31 default: |
| 32 Expect.fail('Unknown operating system ${Platform.operatingSystem}'); |
| 33 } |
| 34 } |
| 18 | 35 |
| 19 void main() { | 36 void main() { |
| 20 Options options = new Options(); | 37 Options options = new Options(); |
| 21 | 38 |
| 22 // Make this a multitest so that the test scripts run a copy of it in | 39 Path scriptDirectory = new Path.fromNative(options.script).directoryPath; |
| 23 // [build directory]/generated_tests. This way, we can copy the shared | 40 Path buildDirectory = new Path.fromNative(options.executable).directoryPath; |
| 24 // library for test_extension.dart to the test directory. | 41 Directory tempDirectory = new Directory('').createTempSync(); |
| 25 // The "none" case of the multitest, without the following | 42 Path testDirectory = new Path.fromNative(tempDirectory.path); |
| 26 // line, is the one that runs the test of the extension. | |
| 27 foo foo foo foo foo; /// 01: compile-time error | |
| 28 | 43 |
| 29 Path testDirectory = new Path.fromNative(options.script).directoryPath; | 44 // Copy test_extension shared library, test_extension.dart and |
| 30 Path buildDirectory = new Path.fromNative(options.executable).directoryPath; | 45 // test_extension_tester.dart to the temporary test directory. |
| 31 | 46 copyFileToDirectory(getExtensionPath(buildDirectory), |
| 32 // Copy test_extension shared library from the build directory to the | 47 testDirectory).chain((_) { |
| 33 // test directory. | 48 Path extensionDartFile = scriptDirectory.append('test_extension.dart'); |
| 34 Future sharedLibraryCopied; | 49 return copyFileToDirectory(extensionDartFile, testDirectory); |
| 35 // Use the platforms' copy file commands, to preserve executable privilege. | 50 }).chain((_) { |
| 36 switch (Platform.operatingSystem) { | 51 Path testExtensionTesterFile = |
| 37 case 'linux': | 52 scriptDirectory.append('test_extension_tester.dart'); |
| 38 var source = buildDirectory.append('lib.target/libtest_extension.so'); | 53 return copyFileToDirectory(testExtensionTesterFile, testDirectory); |
| 39 sharedLibraryCopied = Process.run('cp', | 54 }).chain((_) { |
| 40 [source.toNativePath(), | 55 Path script = testDirectory.append('test_extension_tester.dart'); |
| 41 testDirectory.toNativePath()]); | 56 return Process.run(options.executable, [script.toNativePath()]); |
| 42 break; | 57 })..then((processResult result) { |
| 43 case 'macos': | 58 Expect.equals(0, result.exitCode); |
| 44 var source = buildDirectory.append('libtest_extension.dylib'); | 59 tempDirectory.deleteSync(recursive: true); |
| 45 sharedLibraryCopied = Process.run('cp', | 60 })..handleException((_) { |
| 46 [source.toNativePath(), | 61 tempDirectory.deleteSync(recursive: true); |
| 47 testDirectory.toNativePath()]); | |
| 48 break; | |
| 49 case 'windows': | |
| 50 var source = buildDirectory.append('test_extension.dll'); | |
| 51 sharedLibraryCopied = Process.run('cmd.exe', | |
| 52 ['/C', | |
| 53 'copy ${source.toNativePath()} ${testDirectory.toNativePath()}']); | |
| 54 break; | |
| 55 default: | |
| 56 Expect.fail("Unknown operating system ${Platform.operatingSystem}"); | |
| 57 } | |
| 58 | |
| 59 sharedLibraryCopied.handleException((e) { | |
| 60 print('Copying of shared library test_extension failed.'); | |
| 61 throw e; | |
| 62 }); | |
| 63 sharedLibraryCopied.then((ignore) { | |
| 64 print('Shared library copied to test directory.'); | |
| 65 Path copiedTest = testDirectory.append("test_extension_tester.dart"); | |
| 66 var result = Process.run(options.executable, | |
| 67 [copiedTest.toNativePath()]); | |
| 68 result.then((processResult) { | |
| 69 print('Output of test_extension_tester.dart:'); | |
| 70 print(' stdout:'); | |
| 71 print(processResult.stdout); | |
| 72 print(' stderr:'); | |
| 73 print(processResult.stderr); | |
| 74 stdout.flush(); | |
| 75 exit(processResult.exitCode); | |
| 76 }); | |
| 77 }); | 62 }); |
| 78 } | 63 } |
| OLD | NEW |