| 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 "package:expect/expect.dart"; | 7 import "package:expect/expect.dart"; |
| 8 import "package:path/path.dart"; | 8 import "package:path/path.dart"; |
| 9 import 'dart:async'; | 9 import 'dart:async'; |
| 10 import 'dart:io'; | 10 import 'dart:io'; |
| 11 import 'dart:isolate'; | 11 import 'dart:isolate'; |
| 12 | 12 |
| 13 Future copyFileToDirectory(String file, String directory) { | 13 Future copyFileToDirectory(String file, String directory) { |
| 14 switch (Platform.operatingSystem) { | 14 switch (Platform.operatingSystem) { |
| 15 case 'android': |
| 15 case 'linux': | 16 case 'linux': |
| 16 case 'macos': | 17 case 'macos': |
| 17 return Process.run('cp', [file, directory]); | 18 return Process.run('cp', [file, directory]); |
| 18 case 'windows': | 19 case 'windows': |
| 19 return Process.run('cmd.exe', ['/C', 'copy $file $directory']); | 20 return Process.run('cmd.exe', ['/C', 'copy $file $directory']); |
| 20 default: | 21 default: |
| 21 Expect.fail('Unknown operating system ${Platform.operatingSystem}'); | 22 Expect.fail('Unknown operating system ${Platform.operatingSystem}'); |
| 22 } | 23 } |
| 23 } | 24 } |
| 24 | 25 |
| 25 String getExtensionPath(String buildDirectory) { | 26 // Returns a list containing the source file name in the first element and the |
| 27 // target file name in the second element. |
| 28 List<String> getExtensionNames(String arch) { |
| 26 switch (Platform.operatingSystem) { | 29 switch (Platform.operatingSystem) { |
| 30 case 'android': |
| 27 case 'linux': | 31 case 'linux': |
| 28 return join(buildDirectory, 'lib.target', 'libtest_extension.so'); | 32 return ['libtest_extension.so', 'libtest_extension$arch.so']; |
| 29 case 'macos': | 33 case 'macos': |
| 30 return join(buildDirectory, 'libtest_extension.dylib'); | 34 return ['libtest_extension.dylib', 'libtest_extension$arch.dylib']; |
| 31 case 'windows': | 35 case 'windows': |
| 32 return join(buildDirectory, 'test_extension.dll'); | 36 return ['test_extension.dll','test_extension$arch.dll']; |
| 33 default: | 37 default: |
| 34 Expect.fail('Unknown operating system ${Platform.operatingSystem}'); | 38 Expect.fail('Unknown operating system ${Platform.operatingSystem}'); |
| 35 } | 39 } |
| 36 } | 40 } |
| 37 | 41 |
| 38 void main() { | 42 String getExtensionPath(String buildDirectory, String filename) { |
| 43 switch (Platform.operatingSystem) { |
| 44 case 'android': |
| 45 case 'linux': |
| 46 return join(buildDirectory, 'lib.target', filename); |
| 47 case 'macos': |
| 48 case 'windows': |
| 49 return join(buildDirectory, filename); |
| 50 default: |
| 51 Expect.fail('Unknown operating system ${Platform.operatingSystem}'); |
| 52 } |
| 53 } |
| 54 |
| 55 String getArchFromBuildDir(String buildDirectory) { |
| 56 if (buildDirectory.endsWith('SIMARM')) return ''; |
| 57 if (buildDirectory.endsWith('SIMARM64')) return ''; |
| 58 if (buildDirectory.endsWith('SIMDBC')) return ''; |
| 59 if (buildDirectory.endsWith('SIMMIPS')) return ''; |
| 60 if (buildDirectory.endsWith('ARM')) return '-arm'; |
| 61 if (buildDirectory.endsWith('ARM64')) return '-arm64'; |
| 62 if (buildDirectory.endsWith('IA32')) return '-ia32'; |
| 63 if (buildDirectory.endsWith('MIPS')) return '-mips'; |
| 64 if (buildDirectory.endsWith('X64')) return '-x64'; |
| 65 return 'unknown'; |
| 66 } |
| 67 |
| 68 Future testExtension(bool withArchSuffix) { |
| 39 String scriptDirectory = dirname(Platform.script.toFilePath()); | 69 String scriptDirectory = dirname(Platform.script.toFilePath()); |
| 40 String buildDirectory = dirname(Platform.executable); | 70 String buildDirectory = dirname(Platform.executable); |
| 41 Directory tempDirectory = | 71 Directory tempDirectory = |
| 42 Directory.systemTemp.createTempSync('dart_test_extension'); | 72 Directory.systemTemp.createTempSync('dart_test_extension'); |
| 43 String testDirectory = tempDirectory.path; | 73 String testDirectory = tempDirectory.path; |
| 44 | 74 |
| 75 List<String> fileNames; |
| 76 if (withArchSuffix) { |
| 77 String arch = getArchFromBuildDir(buildDirectory); |
| 78 fileNames = getExtensionNames(arch); |
| 79 } else { |
| 80 fileNames = getExtensionNames(''); |
| 81 } |
| 82 |
| 45 // Copy test_extension shared library, test_extension.dart and | 83 // Copy test_extension shared library, test_extension.dart and |
| 46 // test_extension_tester.dart to the temporary test directory. | 84 // test_extension_tester.dart to the temporary test directory. |
| 47 copyFileToDirectory(getExtensionPath(buildDirectory), | 85 return copyFileToDirectory(getExtensionPath(buildDirectory, fileNames[0]), |
| 48 testDirectory).then((_) { | 86 join(testDirectory, fileNames[1])).then((_) { |
| 49 var extensionDartFile = join(scriptDirectory, 'test_extension.dart'); | 87 var extensionDartFile = join(scriptDirectory, 'test_extension.dart'); |
| 50 return copyFileToDirectory(extensionDartFile, testDirectory); | 88 return copyFileToDirectory(extensionDartFile, testDirectory); |
| 51 }).then((_) { | 89 }).then((_) { |
| 52 var testExtensionTesterFile = | 90 var testExtensionTesterFile = |
| 53 join(scriptDirectory, 'test_extension_tester.dart'); | 91 join(scriptDirectory, 'test_extension_tester.dart'); |
| 54 return copyFileToDirectory(testExtensionTesterFile, testDirectory); | 92 return copyFileToDirectory(testExtensionTesterFile, testDirectory); |
| 55 }).then((_) { | 93 }).then((_) { |
| 56 var script = join(testDirectory, 'test_extension_tester.dart'); | 94 var script = join(testDirectory, 'test_extension_tester.dart'); |
| 57 return Process.run(Platform.executable, [script]); | 95 return Process.run(Platform.executable, [script]); |
| 58 })..then((ProcessResult result) { | 96 })..then((ProcessResult result) { |
| 59 if (result.exitCode != 0) { | 97 if (result.exitCode != 0) { |
| 60 print('Subprocess failed with exit code ${result.exitCode}'); | 98 print('Subprocess failed with exit code ${result.exitCode}'); |
| 61 print('stdout:'); | 99 print('stdout:'); |
| 62 print('${result.stdout}'); | 100 print('${result.stdout}'); |
| 63 print('stderr:'); | 101 print('stderr:'); |
| 64 print('${result.stderr}'); | 102 print('${result.stderr}'); |
| 65 } | 103 } |
| 66 Expect.equals(0, result.exitCode); | 104 Expect.equals(0, result.exitCode); |
| 67 tempDirectory.deleteSync(recursive: true); | 105 tempDirectory.deleteSync(recursive: true); |
| 68 })..catchError((_) { | 106 })..catchError((_) { |
| 69 tempDirectory.deleteSync(recursive: true); | 107 tempDirectory.deleteSync(recursive: true); |
| 70 }); | 108 }); |
| 71 } | 109 } |
| 110 |
| 111 Future testWithArchSuffix() { |
| 112 return testExtension(true); |
| 113 } |
| 114 |
| 115 Future testWithoutArchSuffix() { |
| 116 return testExtension(false); |
| 117 } |
| 118 |
| 119 main() async { |
| 120 await testWithArchSuffix(); |
| 121 await testWithoutArchSuffix(); |
| 122 } |
| OLD | NEW |