diff --git a/runtime/bin/process_linux.cc b/runtime/bin/process_linux.cc index 98ab964aada..93c7f6cc9c4 100644 --- a/runtime/bin/process_linux.cc +++ b/runtime/bin/process_linux.cc @@ -546,6 +546,9 @@ class ProcessStarter { !Directory::SetCurrent(namespc_, working_directory_)) { ReportChildError(); } + if (program_environment_ != NULL) { + environ = program_environment_; + } // Report the final PID and do the exec. ReportPid(getpid()); // getpid cannot fail. diff --git a/tests/standalone/io/process_environment_lib.dart b/tests/standalone/io/process_environment_lib.dart new file mode 100644 index 00000000000..7446236c06e --- /dev/null +++ b/tests/standalone/io/process_environment_lib.dart @@ -0,0 +1,9 @@ +// 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:io'; + +void main() { + print(Platform.environment); +} diff --git a/tests/standalone/io/process_environment_test.dart b/tests/standalone/io/process_environment_test.dart new file mode 100644 index 00000000000..1a6767d048b --- /dev/null +++ b/tests/standalone/io/process_environment_test.dart @@ -0,0 +1,62 @@ +// 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 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: {fakeKey: fakeValue}); + + Map 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: {fakeKey: fakeValue}); + + Map 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 RestoreToMap(String s) { + s = s.substring(1, s.length - 1); + Map 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; +}