Skip to content

Commit 4f22102

Browse files
committed
Testing Configurations.md snippets without temp files
1 parent 7897420 commit 4f22102

File tree

3 files changed

+27
-88
lines changed

3 files changed

+27
-88
lines changed

Cargo.lock

Lines changed: 0 additions & 54 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@ getopts = "0.2"
4545
derive-new = "0.5"
4646
cargo_metadata = "0.4"
4747

48-
[dev-dependencies]
49-
tempfile = "2.2.0"
50-
5148
[target.'cfg(unix)'.dependencies]
5249
libc = "0.2.11"
5350

tests/system.rs

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
extern crate log;
1515
extern crate regex;
1616
extern crate rustfmt_nightly as rustfmt;
17-
extern crate tempfile;
1817
extern crate term;
1918

2019
use std::collections::HashMap;
@@ -24,8 +23,6 @@ use std::iter::Peekable;
2423
use std::path::{Path, PathBuf};
2524
use std::str::Chars;
2625

27-
use tempfile::NamedTempFileOptions;
28-
2926
use rustfmt::*;
3027
use rustfmt::config::{Color, Config, ReportTactic};
3128
use rustfmt::filemap::{write_system_newlines, FileMap};
@@ -529,8 +526,8 @@ fn get_code_blocks() -> Vec<CodeBlock> {
529526

530527
let config_name_regex =
531528
regex::Regex::new(r"^## `([^`]+)`").expect("Failed creating configuration pattern");
532-
let config_value_regex =
533-
regex::Regex::new(r"^#### `([^`]+)`").expect("Failed creating configuration value pattern");
529+
let config_value_regex = regex::Regex::new(r#"^#### `"?([^`"]+)"?`"#)
530+
.expect("Failed creating configuration value pattern");
534531

535532
let mut code_blocks = Vec::new();
536533
let mut extracting_block = false;
@@ -593,34 +590,33 @@ fn check_blocks_idempotency(blocks: &Vec<CodeBlock>) {
593590
let mut failures = 0;
594591

595592
for block in blocks {
596-
let mut temp_file = NamedTempFileOptions::new().suffix(".rs").create().unwrap();
597-
let mut file_name_list = Vec::new();
598-
599-
// Write the configuration name and value at the top of the temp file.
600-
writeln!(
601-
temp_file,
602-
"// {}: {}",
603-
block.config.name, block.config.value
604-
).unwrap();
605-
606-
// Write the code block to the temp file.
607-
write!(temp_file, "{}", block.code_block).unwrap();
608-
609-
// Format the temp file.
610-
file_name_list.push(temp_file.path().to_path_buf());
611-
let files = file_name_list.iter().map(|x| x.to_owned());
612-
let (_reports, _count, fails) = check_files(files);
613-
614-
if fails != 0 {
615-
// If formatting failed, print a message with the *actual* line
616-
// number of the code block in Configurations.md. The message
617-
// printed by `check_files` will use the name and line number from
618-
// the temp file.
619-
write_message(format!(
593+
let input = Input::Text(block.code_block.clone());
594+
let mut config = Config::default();
595+
config.override_value(&block.config.name, &block.config.value);
596+
597+
let (error_summary, file_map, _report) =
598+
format_input::<io::Stdout>(input, &config, None).unwrap();
599+
600+
if error_summary.has_parsing_errors() {
601+
write_message(String::from(format!(
620602
"\u{261d}\u{1f3fd} Failed to format block starting at Line {} in Configurations.md",
621603
block.code_block_start_line
622-
));
623-
failures += fails;
604+
)));
605+
failures += 1;
606+
} else {
607+
for &(ref file_name, ref text) in &file_map {
608+
if let FileName::Custom(ref file_name) = *file_name {
609+
if file_name == "stdin" {
610+
let compare = make_diff(&block.code_block, text, DIFF_CONTEXT_SIZE);
611+
if !compare.is_empty() {
612+
let mut mismatches = HashMap::new();
613+
mismatches.insert(PathBuf::from("Configurations.md"), compare);
614+
print_mismatches(mismatches);
615+
failures += 1;
616+
}
617+
}
618+
}
619+
}
624620
}
625621
}
626622

0 commit comments

Comments
 (0)