Skip to content

Echo back input from stdin when disable_all_formatting is true #1973

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,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()));
Expand Down
1 change: 1 addition & 0 deletions tests/config/disable_all_formatting.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
disable_all_formatting = true
25 changes: 24 additions & 1 deletion tests/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 0 additions & 4 deletions tests/target/disable_all_formatting.rs

This file was deleted.