From 75bb1906641ac63534c853b3cba81493f0ed3fc9 Mon Sep 17 00:00:00 2001 From: Eh2406 Date: Fri, 29 Jun 2018 15:21:05 -0400 Subject: [PATCH] default tests to stable and override where needed --- src/bin/cargo/main.rs | 1 + src/cargo/core/features.rs | 41 ++++++++++++++++++++++++++++++++++++-- src/cargo/core/mod.rs | 1 + tests/testsuite/config.rs | 3 ++- tests/testsuite/resolve.rs | 3 ++- 5 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/bin/cargo/main.rs b/src/bin/cargo/main.rs index 7d7c7db6e4c..e5338e833d6 100644 --- a/src/bin/cargo/main.rs +++ b/src/bin/cargo/main.rs @@ -25,6 +25,7 @@ mod commands; fn main() { env_logger::init(); + cargo::core::maybe_allow_nightly_features(); let mut config = match Config::default() { Ok(cfg) => cfg, diff --git a/src/cargo/core/features.rs b/src/cargo/core/features.rs index 8802b38cb00..d04f1b2ee68 100644 --- a/src/cargo/core/features.rs +++ b/src/cargo/core/features.rs @@ -45,6 +45,7 @@ //! this writing, a very new feature in Cargo. If the process differs from this //! we'll be sure to update this documentation! +use std::cell::Cell; use std::env; use std::fmt; use std::str::FromStr; @@ -368,9 +369,45 @@ fn channel() -> String { .unwrap_or_else(|| String::from("dev")) } +thread_local!( + static NIGHTLY_FEATURES_ALLOWED: Cell = Cell::new(false); + static ENABLE_NIGHTLY_FEATURES: Cell = Cell::new(false); +); + +/// This is a little complicated. +/// This should return false if: +/// - this is an artifact of the rustc distribution process for "stable" or for "beta" +/// - this is an `#[test]` that does not opt in with `enable_nightly_features` +/// - this is a integration test that uses `ProcessBuilder` +/// that does not opt in with `masquerade_as_nightly_cargo` +/// This should return true if: +/// - this is an artifact of the rustc distribution process for "nightly" +/// - this is being used in the rustc distribution process internally +/// - this is a cargo executable that was built from source +/// - this is an `#[test]` that called `enable_nightly_features` +/// - this is a integration test that uses `ProcessBuilder` +/// that called `masquerade_as_nightly_cargo` fn nightly_features_allowed() -> bool { - match &channel()[..] { - "nightly" | "dev" => true, + if ENABLE_NIGHTLY_FEATURES.with(|c| c.get()) { + return true + } + match &channel()[..] { + "nightly" | "dev" => NIGHTLY_FEATURES_ALLOWED.with(|c| c.get()), _ => false, } } + +/// Allows nightly features to be enabled for this thread, but only if the +/// development channel is nightly or dev. +/// +/// Used by cargo main to ensure that a cargo build from source has nightly features +pub fn maybe_allow_nightly_features() { + NIGHTLY_FEATURES_ALLOWED.with(|c| c.set(true)); +} + +/// Forcibly enables nightly features for this thread. +/// +/// Used by tests to allow the use of nightly features. +pub fn enable_nightly_features() { + ENABLE_NIGHTLY_FEATURES.with(|c| c.set(true)); +} diff --git a/src/cargo/core/mod.rs b/src/cargo/core/mod.rs index 67578c8223c..18b3b7c5abd 100644 --- a/src/cargo/core/mod.rs +++ b/src/cargo/core/mod.rs @@ -1,5 +1,6 @@ pub use self::dependency::Dependency; pub use self::features::{CliUnstable, Edition, Feature, Features}; +pub use self::features::{maybe_allow_nightly_features, enable_nightly_features}; pub use self::manifest::{EitherManifest, VirtualManifest}; pub use self::manifest::{LibKind, Manifest, Target, TargetKind}; pub use self::package::{Package, PackageSet}; diff --git a/tests/testsuite/config.rs b/tests/testsuite/config.rs index f9d33e8c4cb..0c8d0bf6e18 100644 --- a/tests/testsuite/config.rs +++ b/tests/testsuite/config.rs @@ -1,4 +1,4 @@ -use cargo::core::Shell; +use cargo::core::{Shell, enable_nightly_features}; use cargo::util::config::{self, Config}; use cargo::util::toml::{self, VecStringOrBool as VSOB}; use cargo::CargoError; @@ -45,6 +45,7 @@ fn write_config(config: &str) { } fn new_config(env: &[(&str, &str)]) -> Config { + enable_nightly_features(); // -Z advanced-env let output = Box::new(fs::File::create(paths::root().join("shell.out")).unwrap()); let shell = Shell::from_write(output); let cwd = paths::root(); diff --git a/tests/testsuite/resolve.rs b/tests/testsuite/resolve.rs index 4ba61b32191..7b2be0d441c 100644 --- a/tests/testsuite/resolve.rs +++ b/tests/testsuite/resolve.rs @@ -4,7 +4,7 @@ use hamcrest::{assert_that, contains, is_not}; use cargo::core::source::{GitReference, SourceId}; use cargo::core::dependency::Kind::{self, Development}; -use cargo::core::{Dependency, PackageId, Registry, Summary}; +use cargo::core::{Dependency, PackageId, Registry, Summary, enable_nightly_features}; use cargo::util::{CargoResult, Config, ToUrl}; use cargo::core::resolver::{self, Method}; @@ -342,6 +342,7 @@ fn test_resolving_maximum_version_with_transitive_deps() { #[test] fn test_resolving_minimum_version_with_transitive_deps() { + enable_nightly_features(); // -Z minimal-versions // When the minimal-versions config option is specified then the lowest // possible version of a package should be selected. "util 1.0.0" can't be // selected because of the requirements of "bar", so the minimum version