Skip to content

Commit

Permalink
Auto merge of #5652 - Eh2406:masquerade_as_nightly_cargo, r=alexcrichton
Browse files Browse the repository at this point in the history
skip test if not nightly; fix for #5648

closes #5648
The alternative was to use `set_var` to emulate `masquerade_as_nightly_cargo` but we worried that it would not get unset correctly. Although I think each test is its own process, so it should work.
  • Loading branch information
bors committed Jun 29, 2018
2 parents 75c0d7e + 75bb190 commit 94f7058
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/bin/cargo/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
41 changes: 39 additions & 2 deletions src/cargo/core/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -368,9 +369,45 @@ fn channel() -> String {
.unwrap_or_else(|| String::from("dev"))
}

thread_local!(
static NIGHTLY_FEATURES_ALLOWED: Cell<bool> = Cell::new(false);
static ENABLE_NIGHTLY_FEATURES: Cell<bool> = 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));
}
1 change: 1 addition & 0 deletions src/cargo/core/mod.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
3 changes: 2 additions & 1 deletion tests/testsuite/config.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion tests/testsuite/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 94f7058

Please sign in to comment.