diff --git a/src/bin/main.rs b/src/bin/main.rs index 3ad2d3088f0..34c1bc59745 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -15,7 +15,7 @@ extern crate getopts; extern crate rustfmt_nightly as rustfmt; use std::fs::File; -use std::io::{self, Read, Write}; +use std::io::{self, stdout, Read, Write}; use std::path::{Path, PathBuf}; use std::str::FromStr; use std::{env, error}; @@ -226,7 +226,7 @@ fn execute(opts: &Options) -> FmtResult { Ok(Summary::default()) } Operation::ConfigHelp => { - Config::print_docs(); + Config::print_docs(&mut stdout(), matches.opt_present("unstable-features")); Ok(Summary::default()) } Operation::ConfigOutputDefault { path } => { diff --git a/src/config/config_type.rs b/src/config/config_type.rs index 53a4fdb8e57..0c38b6d28ad 100644 --- a/src/config/config_type.rs +++ b/src/config/config_type.rs @@ -81,6 +81,7 @@ macro_rules! is_nightly_channel { macro_rules! create_config { ($($i:ident: $ty:ty, $def:expr, $stb:expr, $( $dstring:expr ),+ );+ $(;)*) => ( use std::collections::HashSet; + use std::io::Write; #[derive(Clone)] pub struct Config { @@ -359,7 +360,7 @@ macro_rules! create_config { HIDE_OPTIONS.contains(&name) } - pub fn print_docs() { + pub fn print_docs(out: &mut Write, include_unstable: bool) { use std::cmp; let max = 0; $( let max = cmp::max(max, stringify!($i).len()+1); )+ @@ -367,25 +368,29 @@ macro_rules! create_config { for _ in 0..max { space_str.push(' '); } - println!("Configuration Options:"); + writeln!(out, "Configuration Options:").unwrap(); $( - let name_raw = stringify!($i); + if $stb || include_unstable { + let name_raw = stringify!($i); - if !Config::is_hidden_option(name_raw) { - let mut name_out = String::with_capacity(max); - for _ in name_raw.len()..max-1 { - name_out.push(' ') + if !Config::is_hidden_option(name_raw) { + let mut name_out = String::with_capacity(max); + for _ in name_raw.len()..max-1 { + name_out.push(' ') + } + name_out.push_str(name_raw); + name_out.push(' '); + writeln!(out, + "{}{} Default: {:?}{}", + name_out, + <$ty>::doc_hint(), + $def, + if !$stb { " (unstable)" } else { "" }).unwrap(); + $( + writeln!(out, "{}{}", space_str, $dstring).unwrap(); + )+ + writeln!(out).unwrap(); } - name_out.push_str(name_raw); - name_out.push(' '); - println!("{}{} Default: {:?}", - name_out, - <$ty>::doc_hint(), - $def); - $( - println!("{}{}", space_str, $dstring); - )+ - println!(); } )+ } diff --git a/src/config/mod.rs b/src/config/mod.rs index 7b96c2f671e..3676aed4ba9 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -181,6 +181,31 @@ pub fn get_toml_path(dir: &Path) -> Result, Error> { #[cfg(test)] mod test { use super::Config; + use std::str; + + #[allow(dead_code)] + mod mock { + use super::super::*; + + create_config! { + // Options that are used by the generated functions + max_width: usize, 100, true, "Maximum width of each line"; + use_small_heuristics: bool, true, false, "Whether to use different formatting for items and \ + expressions if they satisfy a heuristic notion of 'small'."; + license_template_path: String, String::default(), false, "Beginning of file must match license template"; + required_version: String, env!("CARGO_PKG_VERSION").to_owned(), false, "Require a specific version of rustfmt."; + ignore: IgnoreList, IgnoreList::default(), false, "Skip formatting the specified files and directories."; + verbose: bool, false, false, "Use verbose output"; + file_lines: FileLines, FileLines::all(), false, + "Lines to format; this is not supported in rustfmt.toml, and can only be specified \ + via the --file-lines option"; + width_heuristics: WidthHeuristics, WidthHeuristics::scaled(100), false, "'small' heuristic values"; + + // Options that are used by the tests + stable_option: bool, false, true, "A stable option"; + unstable_option: bool, false, false, "An unstable option"; + } + } #[test] fn test_config_set() { @@ -218,6 +243,33 @@ mod test { assert_eq!(config.was_set().verbose(), false); } + #[test] + fn test_print_docs_exclude_unstable() { + use self::mock::Config; + + let mut output = Vec::new(); + Config::print_docs(&mut output, false); + + let s = str::from_utf8(&output).unwrap(); + + assert_eq!(s.contains("stable_option"), true); + assert_eq!(s.contains("unstable_option"), false); + assert_eq!(s.contains("(unstable)"), false); + } + + #[test] + fn test_print_docs_include_unstable() { + use self::mock::Config; + + let mut output = Vec::new(); + Config::print_docs(&mut output, true); + + let s = str::from_utf8(&output).unwrap(); + assert_eq!(s.contains("stable_option"), true); + assert_eq!(s.contains("unstable_option"), true); + assert_eq!(s.contains("(unstable)"), true); + } + // FIXME(#2183) these tests cannot be run in parallel because they use env vars // #[test] // fn test_as_not_nightly_channel() {