From fd22c27c47a71cff2f92e61b11b3344ed9e50d25 Mon Sep 17 00:00:00 2001 From: Ruben Schmidmeister Date: Fri, 17 May 2019 15:58:01 +0200 Subject: [PATCH 1/3] Allow tests to be run on nightly only --- src/test/mod.rs | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/src/test/mod.rs b/src/test/mod.rs index 23f6a69b8d1..101a8c07fd9 100644 --- a/src/test/mod.rs +++ b/src/test/mod.rs @@ -259,9 +259,9 @@ fn assert_output(source: &Path, expected_filename: &Path) { #[test] fn idempotence_tests() { run_test_with(&TestSetting::default(), || { - match option_env!("CFG_RELEASE_CHANNEL") { - None | Some("nightly") => {} - _ => return, // these tests require nightly + // these tests require nightly + if !is_nightly() { + return; } // Get all files in the tests/target directory. let files = get_test_files(Path::new("tests/target"), true); @@ -277,9 +277,9 @@ fn idempotence_tests() { // no warnings are emitted. #[test] fn self_tests() { - match option_env!("CFG_RELEASE_CHANNEL") { - None | Some("nightly") => {} - _ => return, // Issue-3443: these tests require nightly + // Issue-3443: these tests require nightly + if !is_nightly() { + return; } let mut files = get_test_files(Path::new("tests"), false); let bin_directories = vec!["cargo-fmt", "git-rustfmt", "bin", "format-diff"]; @@ -313,6 +313,11 @@ fn self_tests() { ); } +fn is_nightly() -> bool { + let release_channel = option_env!("CFG_RELEASE_CHANNEL"); + release_channel.is_none() || release_channel == Some("nightly") +} + #[test] fn stdin_formatting_smoke_test() { let input = Input::Text("fn main () {}".to_owned()); @@ -426,6 +431,16 @@ fn check_files(files: Vec, opt_config: &Option) -> (Vec Config { }; for (key, val) in &sig_comments { - if key != "target" && key != "config" { + if key != "target" && key != "config" && key != "unstable" { config.override_value(key, val); if config.is_default(key) { warn!("Default value {} used explicitly for {}", val, key); From 4745cec7f3a016d48c86c67bb6b41b3d434cd28e Mon Sep 17 00:00:00 2001 From: Ruben Schmidmeister Date: Fri, 17 May 2019 16:07:49 +0200 Subject: [PATCH 2/3] Re-use nightly channel macro --- src/config/config_type.rs | 1 + src/test/mod.rs | 12 ++++-------- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/config/config_type.rs b/src/config/config_type.rs index d14969418ad..26cd672f83a 100644 --- a/src/config/config_type.rs +++ b/src/config/config_type.rs @@ -60,6 +60,7 @@ fn doc_hint() -> String { /// If we're being built by cargo (e.g., `cargo +nightly install rustfmt-nightly`), /// `CFG_RELEASE_CHANNEL` is not set. As we only support being built against the /// nightly compiler when installed from crates.io, default to nightly mode. +#[macro_export] macro_rules! is_nightly_channel { () => { option_env!("CFG_RELEASE_CHANNEL").map_or(true, |c| c == "nightly" || c == "dev") diff --git a/src/test/mod.rs b/src/test/mod.rs index 101a8c07fd9..d3e18130977 100644 --- a/src/test/mod.rs +++ b/src/test/mod.rs @@ -14,6 +14,7 @@ use crate::rustfmt_diff::{make_diff, print_diff, DiffLine, Mismatch, ModifiedChunk, OutputWriter}; use crate::source_file; use crate::{FormatReport, FormatReportFormatterBuilder, Input, Session}; +use crate::is_nightly_channel; const DIFF_CONTEXT_SIZE: usize = 3; const CONFIGURATIONS_FILE_NAME: &str = "Configurations.md"; @@ -260,7 +261,7 @@ fn assert_output(source: &Path, expected_filename: &Path) { fn idempotence_tests() { run_test_with(&TestSetting::default(), || { // these tests require nightly - if !is_nightly() { + if !is_nightly_channel!() { return; } // Get all files in the tests/target directory. @@ -278,7 +279,7 @@ fn idempotence_tests() { #[test] fn self_tests() { // Issue-3443: these tests require nightly - if !is_nightly() { + if !is_nightly_channel!() { return; } let mut files = get_test_files(Path::new("tests"), false); @@ -313,11 +314,6 @@ fn self_tests() { ); } -fn is_nightly() -> bool { - let release_channel = option_env!("CFG_RELEASE_CHANNEL"); - release_channel.is_none() || release_channel == Some("nightly") -} - #[test] fn stdin_formatting_smoke_test() { let input = Input::Text("fn main () {}".to_owned()); @@ -432,7 +428,7 @@ fn check_files(files: Vec, opt_config: &Option) -> (Vec Date: Fri, 17 May 2019 16:13:46 +0200 Subject: [PATCH 3/3] Move macro to separate module --- src/config/config_type.rs | 19 +------------------ src/lib.rs | 3 +++ src/release_channel.rs | 16 ++++++++++++++++ src/test/mod.rs | 2 +- 4 files changed, 21 insertions(+), 19 deletions(-) create mode 100644 src/release_channel.rs diff --git a/src/config/config_type.rs b/src/config/config_type.rs index 26cd672f83a..326d81b2113 100644 --- a/src/config/config_type.rs +++ b/src/config/config_type.rs @@ -50,23 +50,6 @@ fn doc_hint() -> String { } } -/// Checks if we're in a nightly build. -/// -/// The environment variable `CFG_RELEASE_CHANNEL` is set during the rustc bootstrap -/// to "stable", "beta", or "nightly" depending on what toolchain is being built. -/// If we are being built as part of the stable or beta toolchains, we want -/// to disable unstable configuration options. -/// -/// If we're being built by cargo (e.g., `cargo +nightly install rustfmt-nightly`), -/// `CFG_RELEASE_CHANNEL` is not set. As we only support being built against the -/// nightly compiler when installed from crates.io, default to nightly mode. -#[macro_export] -macro_rules! is_nightly_channel { - () => { - option_env!("CFG_RELEASE_CHANNEL").map_or(true, |c| c == "nightly" || c == "dev") - }; -} - macro_rules! create_config { ($($i:ident: $ty:ty, $def:expr, $stb:expr, $( $dstring:expr ),+ );+ $(;)*) => ( #[cfg(test)] @@ -160,7 +143,7 @@ fn fill_from_parsed_config(mut self, parsed: PartialConfig, dir: &Path) -> Confi self.$i.1 = true; self.$i.2 = val; } else { - if is_nightly_channel!() { + if crate::is_nightly_channel!() { self.$i.1 = true; self.$i.2 = val; } else { diff --git a/src/lib.rs b/src/lib.rs index efd88357088..88605079b7e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,6 +40,9 @@ #[macro_use] mod utils; +#[macro_use] +mod release_channel; + mod attr; mod chains; pub(crate) mod checkstyle; diff --git a/src/release_channel.rs b/src/release_channel.rs new file mode 100644 index 00000000000..948247b3c97 --- /dev/null +++ b/src/release_channel.rs @@ -0,0 +1,16 @@ +/// Checks if we're in a nightly build. +/// +/// The environment variable `CFG_RELEASE_CHANNEL` is set during the rustc bootstrap +/// to "stable", "beta", or "nightly" depending on what toolchain is being built. +/// If we are being built as part of the stable or beta toolchains, we want +/// to disable unstable configuration options. +/// +/// If we're being built by cargo (e.g., `cargo +nightly install rustfmt-nightly`), +/// `CFG_RELEASE_CHANNEL` is not set. As we only support being built against the +/// nightly compiler when installed from crates.io, default to nightly mode. +#[macro_export] +macro_rules! is_nightly_channel { + () => { + option_env!("CFG_RELEASE_CHANNEL").map_or(true, |c| c == "nightly" || c == "dev") + }; +} diff --git a/src/test/mod.rs b/src/test/mod.rs index d3e18130977..68fd90a01cf 100644 --- a/src/test/mod.rs +++ b/src/test/mod.rs @@ -11,10 +11,10 @@ use crate::config::{Color, Config, EmitMode, FileName, NewlineStyle, ReportTactic}; use crate::formatting::{ReportedErrors, SourceFile}; +use crate::is_nightly_channel; use crate::rustfmt_diff::{make_diff, print_diff, DiffLine, Mismatch, ModifiedChunk, OutputWriter}; use crate::source_file; use crate::{FormatReport, FormatReportFormatterBuilder, Input, Session}; -use crate::is_nightly_channel; const DIFF_CONTEXT_SIZE: usize = 3; const CONFIGURATIONS_FILE_NAME: &str = "Configurations.md";