Merge pull request #1973 from topecongiro/issue-1972

Echo back input from stdin when disable_all_formatting is true
This commit is contained in:
Nick Cameron 2017-09-18 18:06:49 +12:00 committed by GitHub
commit c313fb1ddc
4 changed files with 31 additions and 5 deletions

View File

@ -890,6 +890,12 @@ pub fn format_input<T: Write>(
) -> Result<(Summary, FileMap, FormatReport), (io::Error, Summary)> {
let mut summary = Summary::default();
if config.disable_all_formatting() {
// When the input is from stdin, echo back the input.
if let Input::Text(ref buf) = input {
if let Err(e) = io::stdout().write_all(buf.as_bytes()) {
return Err((e, summary));
}
}
return Ok((summary, FileMap::new(), FormatReport::new()));
}
let codemap = Rc::new(CodeMap::new(FilePathMapping::empty()));

View File

@ -0,0 +1 @@
disable_all_formatting = true

View File

@ -14,8 +14,9 @@ extern crate term;
use std::collections::HashMap;
use std::fs;
use std::io::{self, BufRead, BufReader, Read};
use std::io::{self, BufRead, BufReader, Read, Write};
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use rustfmt::*;
use rustfmt::filemap::{write_system_newlines, FileMap};
@ -157,6 +158,28 @@ fn stdin_formatting_smoke_test() {
panic!("no stdin");
}
#[test]
fn stdin_disable_all_formatting_test() {
let input = String::from("fn main() { println!(\"This should not be formatted.\"); }");
let mut child = Command::new("./target/debug/rustfmt")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.arg("--config-path=./tests/config/disable_all_formatting.toml")
.spawn()
.expect("failed to execute child");
{
let stdin = child.stdin.as_mut().expect("failed to get stdin");
stdin
.write_all(input.as_bytes())
.expect("failed to write stdin");
}
let output = child.wait_with_output().expect("failed to wait on child");
assert!(output.status.success());
assert!(output.stderr.is_empty());
assert_eq!(input, String::from_utf8(output.stdout).unwrap());
}
#[test]
fn format_lines_errors_are_reported() {
let long_identifier = String::from_utf8(vec![b'a'; 239]).unwrap();

View File

@ -1,4 +0,0 @@
// rustfmt-disable_all_formatting: true
// Don't format anything.
fn main() { println!("This should not be formatted."); }