From 1b8ce60054306b6ab1e7ec50938557cef9be54f6 Mon Sep 17 00:00:00 2001 From: sharkdp Date: Sat, 18 Apr 2020 11:37:12 +0200 Subject: [PATCH] Pass stdin as a generic BufRead, fix stdin tests --- src/assets.rs | 17 ++++++++++++++--- src/controller.rs | 4 +--- src/inputfile.rs | 4 ++-- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/assets.rs b/src/assets.rs index 2dfcebfb..ccac5029 100644 --- a/src/assets.rs +++ b/src/assets.rs @@ -271,7 +271,7 @@ mod tests { let syntax = self.assets.get_syntax( None, input_file, - &mut input_file.get_reader(&io::stdin()).unwrap(), + &mut input_file.get_reader(io::stdin().lock()).unwrap(), &self.syntax_mapping, ); @@ -281,6 +281,17 @@ mod tests { fn syntax_for_file(&self, file_name: &str) -> String { self.syntax_for_file_with_content(file_name, "") } + + fn syntax_for_stdin_with_content(&self, file_name: &str, content: &[u8]) -> String { + let input_file = InputFile::StdIn(Some(OsStr::new(file_name))); + let syntax = self.assets.get_syntax( + None, + input_file, + &mut input_file.get_reader(content).unwrap(), + &self.syntax_mapping, + ); + syntax.name.clone() + } } #[test] @@ -353,10 +364,10 @@ mod tests { let test = SyntaxDetectionTest::new(); // from file extension - assert_eq!(test.syntax_for_file_with_content("test.cpp", ""), "C++"); + assert_eq!(test.syntax_for_stdin_with_content("test.cpp", b"a"), "C++"); // from first line (fallback) assert_eq!( - test.syntax_for_file_with_content("my_script", "#!/bin/bash"), + test.syntax_for_stdin_with_content("my_script", b"#!/bin/bash"), "Bourne Again Shell (bash)" ); } diff --git a/src/controller.rs b/src/controller.rs index 8c0b96e6..11d4d3a3 100644 --- a/src/controller.rs +++ b/src/controller.rs @@ -56,10 +56,8 @@ impl<'b> Controller<'b> { let writer = output_type.handle()?; let mut no_errors: bool = true; - let stdin = io::stdin(); - for input_file in self.config.files.iter() { - match input_file.get_reader(&stdin) { + match input_file.get_reader(io::stdin().lock()) { Err(error) => { handle_error(&error); no_errors = false; diff --git a/src/inputfile.rs b/src/inputfile.rs index d7dd731b..737d3ea3 100644 --- a/src/inputfile.rs +++ b/src/inputfile.rs @@ -86,9 +86,9 @@ pub enum InputFile<'a> { } impl<'a> InputFile<'a> { - pub(crate) fn get_reader(&self, stdin: &'a io::Stdin) -> Result { + pub(crate) fn get_reader(&self, stdin: R) -> Result { match self { - InputFile::StdIn(_) => Ok(InputFileReader::new(stdin.lock())), + InputFile::StdIn(_) => Ok(InputFileReader::new(stdin)), InputFile::Ordinary(ofile) => { let file = File::open(ofile.path) .map_err(|e| format!("'{}': {}", ofile.path.to_string_lossy(), e))?;