dart-sdk/tests/standalone/from_env_test.dart
Lasse R.H. Nielsen 5acb2132f8 Constant evaluator won't assume all dart.library.x entries exist.
The existing implementation assumed, hardcoded, that the compilation
environment had an entry for any string of the form `dart.library.`+X,
with a value of either `"true"` or `""`.
This did not match the runtime behavior of the standalone VM,
which allows non-constant access to the compilation environment.

Changed to only have entries with value `"true"` for libraries
which exist.
This changes the value of `bool.hasEnvironment` or a
`String.fromEnvironment` or `bool.fromEnvironment` with a
non-default `defaultValue`.
The existing behavior was that `bool.hasEnvironment` was always true,
and that the other constructors ignored the `defaultValue`,
so most likely such tests or `defaultValues` aren't used anyway.

Fixes #53815

Bug: https://dartbug.com/53815
Change-Id: I995bb34b5ab04b39a8a588d6a59c0027a0fe855c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/331261
Commit-Queue: Lasse Nielsen <lrn@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2024-03-23 11:18:46 +00:00

117 lines
5.6 KiB
Dart

// Copyright (c) 2023, 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.
// SharedOptions=-DtestFlag=testValue -DnumFlag=-0xfeed -DboolFlag=false
import "package:expect/expect.dart";
// Test that the `fromEnvironment`/`hasEnvironment` constructors
// agree between constant and runtime evaluation,
// both for platform-library detection entries and for normal entries.
main() {
// User string entry, const.
Expect.isTrue(const bool.hasEnvironment("testFlag"));
Expect.equals("testValue",
const String.fromEnvironment("testFlag", defaultValue: "nonce"));
// User string entry, runtime.
Expect.isTrue(bool.hasEnvironment("testFlag"));
Expect.equals(
"testValue", String.fromEnvironment("testFlag", defaultValue: "nonce"));
// User number entry, const.
Expect.isTrue(const bool.hasEnvironment("numFlag"));
Expect.equals("-0xfeed", const String.fromEnvironment("numFlag"));
Expect.equals(-0xfeed, const int.fromEnvironment("numFlag", defaultValue: 4));
// User number entry, runtime.
Expect.isTrue(bool.hasEnvironment("numFlag"));
Expect.equals("-0xfeed", String.fromEnvironment("numFlag"));
Expect.equals(-0xfeed, int.fromEnvironment("numFlag", defaultValue: 4));
// User bool entry, const.
Expect.isTrue(const bool.hasEnvironment("boolFlag"));
Expect.equals("false", const String.fromEnvironment("boolFlag"));
Expect.equals(
false, const bool.fromEnvironment("boolFlag", defaultValue: true));
// User bool entry, runtime.
Expect.isTrue(bool.hasEnvironment("boolFlag"));
Expect.equals("false", String.fromEnvironment("boolFlag"));
Expect.equals(false, bool.fromEnvironment("boolFlag", defaultValue: true));
// Missing user entry, const.
Expect.isFalse(const bool.hasEnvironment("noEntry"));
Expect.equals("", const String.fromEnvironment("noEntry"));
Expect.equals(
"nonce", const String.fromEnvironment("noEntry", defaultValue: "nonce"));
Expect.equals(0, const int.fromEnvironment("noEntry"));
Expect.equals(42, const int.fromEnvironment("noEntry", defaultValue: 42));
Expect.isFalse(const bool.fromEnvironment("noEntry"));
Expect.isTrue(const bool.fromEnvironment("noEntry", defaultValue: true));
// Missing user entry, runtime.
Expect.isFalse(bool.hasEnvironment("noEntry"));
Expect.equals("", String.fromEnvironment("noEntry"));
Expect.equals(
"nonce", String.fromEnvironment("noEntry", defaultValue: "nonce"));
Expect.equals(0, int.fromEnvironment("noEntry"));
Expect.equals(42, int.fromEnvironment("noEntry", defaultValue: 42));
Expect.isFalse(bool.fromEnvironment("noEntry"));
Expect.isTrue(bool.fromEnvironment("noEntry", defaultValue: true));
// General platform library entry, const.
Expect.isTrue(const bool.hasEnvironment("dart.library.core"));
Expect.equals("true",
const String.fromEnvironment("dart.library.core", defaultValue: "nonce"));
Expect.isTrue(
const bool.fromEnvironment("dart.library.core", defaultValue: false));
// General platform library entry, runtime.
Expect.isTrue(bool.hasEnvironment("dart.library.core"));
Expect.equals("true",
String.fromEnvironment("dart.library.core", defaultValue: "nonce"));
Expect.isTrue(bool.fromEnvironment("dart.library.core", defaultValue: false));
// Standalone VM-specific library, const.
Expect.isTrue(const bool.hasEnvironment("dart.library.io"));
Expect.equals("true",
const String.fromEnvironment("dart.library.io", defaultValue: "nonce"));
Expect.isTrue(
const bool.fromEnvironment("dart.library.io", defaultValue: false));
// Standalone VM-specific library, runtime.
Expect.isTrue(bool.hasEnvironment("dart.library.io"));
Expect.equals(
"true", String.fromEnvironment("dart.library.io", defaultValue: "nonce"));
Expect.isTrue(bool.fromEnvironment("dart.library.io", defaultValue: false));
// Web-specific library, not available here, const.
Expect.isFalse(const bool.hasEnvironment("dart.library.html"));
Expect.equals("", const String.fromEnvironment("dart.library.html"));
Expect.equals("nonce",
const String.fromEnvironment("dart.library.html", defaultValue: "nonce"));
Expect.isFalse(const bool.fromEnvironment("dart.library.html"));
Expect.isTrue(
const bool.fromEnvironment("dart.library.html", defaultValue: true));
// Web-specific library, not available here, runtime.
Expect.isFalse(bool.hasEnvironment("dart.library.html"));
Expect.equals("", String.fromEnvironment("dart.library.html"));
Expect.equals("nonce",
String.fromEnvironment("dart.library.html", defaultValue: "nonce"));
Expect.isFalse(bool.fromEnvironment("dart.library.html"));
Expect.isTrue(bool.fromEnvironment("dart.library.html", defaultValue: true));
// Non-existing library, const.
Expect.isFalse(const bool.hasEnvironment("dart.library.not"));
Expect.equals("", const String.fromEnvironment("dart.library.not"));
Expect.equals("nonce",
const String.fromEnvironment("dart.library.not", defaultValue: "nonce"));
Expect.isFalse(const bool.fromEnvironment("dart.library.not"));
Expect.isTrue(
const bool.fromEnvironment("dart.library.not", defaultValue: true));
// Non-existing library, runtime.
Expect.isFalse(bool.hasEnvironment("dart.library.not"));
Expect.equals("", String.fromEnvironment("dart.library.not"));
Expect.equals("nonce",
String.fromEnvironment("dart.library.not", defaultValue: "nonce"));
Expect.isFalse(bool.fromEnvironment("dart.library.not"));
Expect.isTrue(bool.fromEnvironment("dart.library.not", defaultValue: true));
}