Files
sdk/tests/standalone/io/process_environment_test.dart
T
Zichang Guo 0bd674c374 [VM-Runtime] set environment when creating detached process
environ variable should be updated if user provides environment. It only update for normal processes.
Bug= https://github.com/dart-lang/sdk/issues/36132

Change-Id: I2a4639ed53e7376534578a0b1b251025ffaa2278
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/96147
Reviewed-by: Siva Annamalai <asiva@google.com>
Commit-Queue: Zichang Guo <zichangguo@google.com>
2019-03-12 21:09:35 +00:00

63 lines
2.2 KiB
Dart

// Copyright (c) 2019, 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.
import 'dart:convert';
import 'dart:io';
import 'package:expect/expect.dart';
import 'package:path/path.dart' as path;
const String childFile = 'process_environment_lib.dart';
const String fakeKey = 'Artificial';
const String fakeValue = 'fakepath';
void main() async {
Map<String, String> environ = Platform.environment;
String baseDirectory = path.dirname(Platform.script.path);
//DETACHED PROCESS WITHOUT includeParentEnvironment
var WithoutEnviron = await Process.start(
Platform.executable, [path.join(baseDirectory, childFile)],
mode: ProcessStartMode.detachedWithStdio,
includeParentEnvironment: false,
environment: <String, String>{fakeKey: fakeValue});
Map<String, String> notInclude = new Map();
await for (final line in WithoutEnviron.stdout
.transform(systemEncoding.decoder)
.transform(LineSplitter())) {
notInclude = RestoreToMap(line);
}
//Ensure the child process has the passed environment
Expect.isTrue(notInclude.length >= 1);
Expect.isTrue(notInclude.keys.contains(fakeKey));
//DETACHED PROCESS WITH includeParentEnvironment
var WithEnviron = await Process.start(
Platform.executable, [path.join(baseDirectory, childFile)],
mode: ProcessStartMode.detachedWithStdio,
includeParentEnvironment: true,
environment: <String, String>{fakeKey: fakeValue});
Map<String, String> include = new Map();
await for (final line in WithEnviron.stdout
.transform(systemEncoding.decoder)
.transform(LineSplitter())) {
include = RestoreToMap(line);
}
//Parent environment and one fake path
Expect.isTrue(include.length == environ.length + 1);
Expect.isTrue(include[fakeKey] == fakeValue);
}
Map<String, String> RestoreToMap(String s) {
s = s.substring(1, s.length - 1);
Map<String, String> result = new Map();
for (String line in s.split(", ")) {
var i = line.indexOf(": ");
result.putIfAbsent(line.substring(0, i), () => line.substring(i + 2));
}
return result;
}