Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 421ed94

Browse files
authored
Merge pull request rust-lang#3558 from bash/unstable-tests
Add option to run a test only on nightly
2 parents e6d616e + 8b57668 commit 421ed94

File tree

4 files changed

+38
-24
lines changed

4 files changed

+38
-24
lines changed

src/config/config_type.rs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,6 @@ impl ConfigType for IgnoreList {
5050
}
5151
}
5252

53-
/// Checks if we're in a nightly build.
54-
///
55-
/// The environment variable `CFG_RELEASE_CHANNEL` is set during the rustc bootstrap
56-
/// to "stable", "beta", or "nightly" depending on what toolchain is being built.
57-
/// If we are being built as part of the stable or beta toolchains, we want
58-
/// to disable unstable configuration options.
59-
///
60-
/// If we're being built by cargo (e.g., `cargo +nightly install rustfmt-nightly`),
61-
/// `CFG_RELEASE_CHANNEL` is not set. As we only support being built against the
62-
/// nightly compiler when installed from crates.io, default to nightly mode.
63-
macro_rules! is_nightly_channel {
64-
() => {
65-
option_env!("CFG_RELEASE_CHANNEL").map_or(true, |c| c == "nightly" || c == "dev")
66-
};
67-
}
68-
6953
macro_rules! create_config {
7054
($($i:ident: $ty:ty, $def:expr, $stb:expr, $( $dstring:expr ),+ );+ $(;)*) => (
7155
#[cfg(test)]
@@ -159,7 +143,7 @@ macro_rules! create_config {
159143
self.$i.1 = true;
160144
self.$i.2 = val;
161145
} else {
162-
if is_nightly_channel!() {
146+
if crate::is_nightly_channel!() {
163147
self.$i.1 = true;
164148
self.$i.2 = val;
165149
} else {

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ pub use crate::rustfmt_diff::{ModifiedChunk, ModifiedLines};
4040
#[macro_use]
4141
mod utils;
4242

43+
#[macro_use]
44+
mod release_channel;
45+
4346
mod attr;
4447
mod chains;
4548
pub(crate) mod checkstyle;

src/release_channel.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/// Checks if we're in a nightly build.
2+
///
3+
/// The environment variable `CFG_RELEASE_CHANNEL` is set during the rustc bootstrap
4+
/// to "stable", "beta", or "nightly" depending on what toolchain is being built.
5+
/// If we are being built as part of the stable or beta toolchains, we want
6+
/// to disable unstable configuration options.
7+
///
8+
/// If we're being built by cargo (e.g., `cargo +nightly install rustfmt-nightly`),
9+
/// `CFG_RELEASE_CHANNEL` is not set. As we only support being built against the
10+
/// nightly compiler when installed from crates.io, default to nightly mode.
11+
#[macro_export]
12+
macro_rules! is_nightly_channel {
13+
() => {
14+
option_env!("CFG_RELEASE_CHANNEL").map_or(true, |c| c == "nightly" || c == "dev")
15+
};
16+
}

src/test/mod.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use std::thread;
1111

1212
use crate::config::{Color, Config, EmitMode, FileName, NewlineStyle, ReportTactic};
1313
use crate::formatting::{ReportedErrors, SourceFile};
14+
use crate::is_nightly_channel;
1415
use crate::rustfmt_diff::{make_diff, print_diff, DiffLine, Mismatch, ModifiedChunk, OutputWriter};
1516
use crate::source_file;
1617
use crate::{FormatReport, FormatReportFormatterBuilder, Input, Session};
@@ -259,9 +260,9 @@ fn assert_output(source: &Path, expected_filename: &Path) {
259260
#[test]
260261
fn idempotence_tests() {
261262
run_test_with(&TestSetting::default(), || {
262-
match option_env!("CFG_RELEASE_CHANNEL") {
263-
None | Some("nightly") => {}
264-
_ => return, // these tests require nightly
263+
// these tests require nightly
264+
if !is_nightly_channel!() {
265+
return;
265266
}
266267
// Get all files in the tests/target directory.
267268
let files = get_test_files(Path::new("tests/target"), true);
@@ -277,9 +278,9 @@ fn idempotence_tests() {
277278
// no warnings are emitted.
278279
#[test]
279280
fn self_tests() {
280-
match option_env!("CFG_RELEASE_CHANNEL") {
281-
None | Some("nightly") => {}
282-
_ => return, // Issue-3443: these tests require nightly
281+
// Issue-3443: these tests require nightly
282+
if !is_nightly_channel!() {
283+
return;
283284
}
284285
let mut files = get_test_files(Path::new("tests"), false);
285286
let bin_directories = vec!["cargo-fmt", "git-rustfmt", "bin", "format-diff"];
@@ -426,6 +427,16 @@ fn check_files(files: Vec<PathBuf>, opt_config: &Option<PathBuf>) -> (Vec<Format
426427
let mut reports = vec![];
427428

428429
for file_name in files {
430+
let sig_comments = read_significant_comments(&file_name);
431+
if sig_comments.contains_key("unstable") && !is_nightly_channel!() {
432+
debug!(
433+
"Skipping '{}' because it requires unstable \
434+
features which are only available on nightly...",
435+
file_name.display()
436+
);
437+
continue;
438+
}
439+
429440
debug!("Testing '{}'...", file_name.display());
430441

431442
match idempotent_check(&file_name, &opt_config) {
@@ -485,7 +496,7 @@ fn read_config(filename: &Path) -> Config {
485496
};
486497

487498
for (key, val) in &sig_comments {
488-
if key != "target" && key != "config" {
499+
if key != "target" && key != "config" && key != "unstable" {
489500
config.override_value(key, val);
490501
if config.is_default(key) {
491502
warn!("Default value {} used explicitly for {}", val, key);

0 commit comments

Comments
 (0)