| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 // | |
| 5 // Process test program to test process communication. | |
| 6 | |
| 7 library PlatformExecutableTest; | |
| 8 | |
| 9 import "dart:io"; | |
| 10 | |
| 11 const _SCRIPT_KEY = '_test_script'; | |
| 12 | |
| 13 void expectEquals(a, b) { | |
| 14 if (a != b) { | |
| 15 throw 'Expected: $a\n' | |
| 16 ' Actual: $b'; | |
| 17 } | |
| 18 } | |
| 19 | |
| 20 void verify(String exePath, {String altPath}) { | |
| 21 var env = {_SCRIPT_KEY: 'yes'}; | |
| 22 if (altPath != null) { | |
| 23 env['PATH'] = altPath; | |
| 24 } | |
| 25 | |
| 26 var processResult = Process.runSync(exePath, [scriptPath], | |
| 27 includeParentEnvironment: false, runInShell: true, environment: env); | |
| 28 | |
| 29 if (processResult.exitCode != 0) { | |
| 30 throw 'Error with process\n' | |
| 31 '$scriptPath' | |
| 32 'Exit code: ${processResult.exitCode}\n' | |
| 33 ' STDOUT: ${processResult.stdout}\n' | |
| 34 ' STDERR: ${processResult.stderr}\n'; | |
| 35 } | |
| 36 | |
| 37 var result = processResult.stdout.trim(); | |
| 38 expectEquals(Platform.executable, result); | |
| 39 } | |
| 40 | |
| 41 void testDartExecShouldNotBeInCurrentDir() { | |
| 42 var type = FileSystemEntity.typeSync(platformExeName); | |
| 43 expectEquals(FileSystemEntityType.NOT_FOUND, type); | |
| 44 } | |
| 45 | |
| 46 void testShouldSucceedWithEmptyPathEnvironment() { | |
| 47 var command = Platform.isWindows ? 'cmd' : 'ls'; | |
| 48 Process.runSync(command, [], | |
| 49 includeParentEnvironment: false, | |
| 50 environment: {_SCRIPT_KEY: 'yes', 'PATH': ''}); | |
| 51 } | |
| 52 | |
| 53 void testShouldSucceedWithSourcePlatformExecutable() { | |
| 54 verify(Platform.executable); | |
| 55 } | |
| 56 | |
| 57 void testExeSymLinked(Directory dir) { | |
| 58 var dirUri = new Uri.directory(dir.path); | |
| 59 var link = new Link.fromUri(dirUri.resolve('dart_exe_link')); | |
| 60 link.createSync(Platform.executable); | |
| 61 verify(link.path); | |
| 62 } | |
| 63 | |
| 64 void testPathToDirWithExeSymLinked(Directory dir) { | |
| 65 var dirUri = new Uri.directory(dir.path); | |
| 66 var link = new Link.fromUri(dirUri.resolve('dart_exe_link')); | |
| 67 link.createSync(Platform.executable); | |
| 68 verify('dart_exe_link', altPath: dir.path); | |
| 69 } | |
| 70 | |
| 71 /// Create a sym-link to the SDK directory and run 'dart' from that path | |
| 72 void testExeDirSymLinked(Directory dir) { | |
| 73 var dirUri = new Uri.directory(dir.path); | |
| 74 | |
| 75 var linkDirUri = dirUri.resolve('dart_bin_dir_link'); | |
| 76 var link = new Link.fromUri(linkDirUri); | |
| 77 | |
| 78 var exeFile = new File(Platform.executable); | |
| 79 | |
| 80 link.createSync(exeFile.parent.path); | |
| 81 | |
| 82 var linkedBin = | |
| 83 new Uri.directory(linkDirUri.toFilePath()).resolve(platformExeName); | |
| 84 | |
| 85 verify(linkedBin.toFilePath()); | |
| 86 } | |
| 87 | |
| 88 void testPathPointsToSymLinkedSDKPath(Directory dir) { | |
| 89 var dirUri = new Uri.directory(dir.path); | |
| 90 | |
| 91 var linkDirUri = dirUri.resolve('dart_bin_dir_link'); | |
| 92 var link = new Link.fromUri(linkDirUri); | |
| 93 | |
| 94 var exeFile = new File(Platform.executable); | |
| 95 | |
| 96 link.createSync(exeFile.parent.path); | |
| 97 | |
| 98 verify(platformExeName, altPath: link.path); | |
| 99 } | |
| 100 | |
| 101 void testPathToSDKDir() { | |
| 102 var exeFile = new File(Platform.executable); | |
| 103 var binDirPath = exeFile.parent.path; | |
| 104 | |
| 105 verify(platformExeName, altPath: binDirPath); | |
| 106 } | |
| 107 | |
| 108 void withTempDir(void test(Directory dir)) { | |
| 109 var tempDir = Directory.systemTemp.createTempSync('dart.sdk.test.'); | |
| 110 try { | |
| 111 test(tempDir); | |
| 112 } finally { | |
| 113 tempDir.deleteSync(recursive: true); | |
| 114 } | |
| 115 } | |
| 116 | |
| 117 String get platformExeName { | |
| 118 var raw = new Uri.file(Platform.executable); | |
| 119 return raw.pathSegments.last; | |
| 120 } | |
| 121 | |
| 122 String get scriptPath => Platform.script.toFilePath(); | |
| 123 | |
| 124 void main() { | |
| 125 if (Platform.environment.containsKey(_SCRIPT_KEY)) { | |
| 126 print(Platform.executable); | |
| 127 return; | |
| 128 } | |
| 129 | |
| 130 testDartExecShouldNotBeInCurrentDir(); | |
| 131 testShouldSucceedWithSourcePlatformExecutable(); /// 00: ok | |
| 132 // dart:io does not support linking to files in Windows. | |
| 133 if (!Platform.isWindows) { | |
| 134 withTempDir(testExeSymLinked); /// 01: ok | |
| 135 } | |
| 136 withTempDir(testExeDirSymLinked); /// 02: ok | |
| 137 testPathToSDKDir(); /// 03: ok | |
| 138 withTempDir(testPathPointsToSymLinkedSDKPath); /// 04: ok | |
| 139 // dart:io does not support linking to files in Windows. | |
| 140 if (!Platform.isWindows) { | |
| 141 withTempDir(testPathToDirWithExeSymLinked); /// 05: ok | |
| 142 } | |
| 143 testShouldSucceedWithEmptyPathEnvironment(); /// 06: ok | |
| 144 } | |
| OLD | NEW |