Files
sdk/tests/standalone/io/platform_resolved_executable_test.dart
T
Robert Nystrom 6129842d74 Opt multitests out of formatting in standalone/.
The new formatter supports opting a region of code out from being
formatted. I'm applying this marker to all of the multitests since
those tests are often very sensitive to formatting and easily broken.
This way, anyone touching a multitest (including me when I reformat
the tests) doesn't have to remember to not run the formatter on it.

Change-Id: I06928e08530ad658affd23e594702cff658d8035
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/396181
Commit-Queue: Bob Nystrom <rnystrom@google.com>
Commit-Queue: Brian Quinlan <bquinlan@google.com>
Auto-Submit: Bob Nystrom <rnystrom@google.com>
Reviewed-by: Brian Quinlan <bquinlan@google.com>
2024-11-19 00:57:42 +00:00

166 lines
4.7 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
//
// Process test program to test process communication.
// Formatting can break multitests, so don't format them.
// dart format off
library PlatformExecutableTest;
import "dart:io";
const _SCRIPT_KEY = '_test_script';
void expectEquals(a, b) {
if (a != b) {
throw 'Expected: $a\n'
' Actual: $b';
}
}
void verify(String exePath, {String? altPath}) {
var env = {_SCRIPT_KEY: 'yes'};
if (altPath != null) {
env['PATH'] = altPath;
}
List<String> execArgs =
([]..addAll(Platform.executableArguments)
..add('--verbosity=warning'));
var processResult = Process.runSync(
exePath, [...execArgs, scriptPath],
includeParentEnvironment: false, runInShell: true, environment: env);
if (processResult.exitCode != 0) {
throw 'Error with process\n'
'$scriptPath'
'Exit code: ${processResult.exitCode}\n'
' STDOUT: ${processResult.stdout}\n'
' STDERR: ${processResult.stderr}\n';
}
var result = processResult.stdout.trim();
expectEquals(Platform.resolvedExecutable, result);
}
void testDartExecShouldNotBeInCurrentDir() {
var type = FileSystemEntity.typeSync(platformExeName);
expectEquals(FileSystemEntityType.notFound, type);
}
void testShouldFailOutsidePath() {
var threw = false;
try {
Process.runSync(([platformExeName]..add('--verbosity=warning')).join(' '),
['--version'],
includeParentEnvironment: false,
environment: {_SCRIPT_KEY: 'yes', 'PATH': ''});
} catch (_) {
threw = true;
}
if (!threw) {
throw 'Expected running the dart executable "$platformExeName" without'
' the parent environment or path to fail.';
}
}
void testShouldSucceedWithSourcePlatformExecutable() {
verify(Platform.resolvedExecutable);
}
void testExeSymLinked(Directory dir) {
var dirUri = new Uri.directory(dir.path);
var link = new Link.fromUri(dirUri.resolve('dart_exe_link'));
link.createSync(Platform.resolvedExecutable);
verify(link.path);
}
void testPathToDirWithExeSymLinked(Directory dir) {
var dirUri = new Uri.directory(dir.path);
var link = new Link.fromUri(dirUri.resolve('dart_exe_link'));
link.createSync(Platform.resolvedExecutable);
verify('dart_exe_link', altPath: dir.path);
}
/// Create a sym-link to the SDK directory and run 'dart' from that path
void testExeDirSymLinked(Directory dir) {
var dirUri = new Uri.directory(dir.path);
var linkDirUri = dirUri.resolve('dart_bin_dir_link');
var link = new Link.fromUri(linkDirUri);
var exeFile = new File(Platform.resolvedExecutable);
link.createSync(exeFile.parent.path);
var linkedBin =
new Uri.directory(linkDirUri.toFilePath()).resolve(platformExeName);
verify(linkedBin.toFilePath());
}
void testPathPointsToSymLinkedSDKPath(Directory dir) {
var dirUri = new Uri.directory(dir.path);
var linkDirUri = dirUri.resolve('dart_bin_dir_link');
var link = new Link.fromUri(linkDirUri);
var exeFile = new File(Platform.resolvedExecutable);
link.createSync(exeFile.parent.path);
verify(platformExeName, altPath: link.path);
}
void testPathToSDKDir() {
var exeFile = new File(Platform.resolvedExecutable);
var binDirPath = exeFile.parent.path;
verify(platformExeName, altPath: binDirPath);
}
void withTempDir(void test(Directory dir)) {
var tempDir = Directory.systemTemp.createTempSync('dart.sdk.test.');
try {
test(tempDir);
} finally {
tempDir.deleteSync(recursive: true);
}
}
String get platformExeName {
var raw = new Uri.file(Platform.resolvedExecutable);
return raw.pathSegments.last;
}
String get scriptPath => Platform.script.toFilePath();
void main() {
// The same script is used for both running the tests and as for starting
// child verifying the value of Platform.resolvedExecutable. If the
// environment variable _SCRIPT_KEY is set this is a child process which
// should print the value of Platform.resolvedExecutable.
if (Platform.environment.containsKey(_SCRIPT_KEY)) {
print(Platform.resolvedExecutable);
return;
}
testDartExecShouldNotBeInCurrentDir();
testShouldSucceedWithSourcePlatformExecutable(); //# 00: ok
// dart:io does not support linking to files in Windows.
if (!Platform.isWindows) {
withTempDir(testExeSymLinked); //# 01: ok
}
withTempDir(testExeDirSymLinked); //# 02: ok
testPathToSDKDir(); //# 03: ok
withTempDir(testPathPointsToSymLinkedSDKPath); //# 04: ok
// dart:io does not support linking to files in Windows.
if (!Platform.isWindows) {
withTempDir(testPathToDirWithExeSymLinked); //# 05: ok
}
testShouldFailOutsidePath(); //# 06: ok
}