From 2a2969aee4bffb8b1bd20bd5202f9cbbf308681f Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Tue, 26 Nov 2019 18:39:12 +0100 Subject: [PATCH] Don't write --version output directly to stdout. (#1602) Fixes #1390. --- src/parse/parser.rs | 9 ++++----- tests/version.rs | 8 ++++++-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/parse/parser.rs b/src/parse/parser.rs index 316163da..1a1f1aa7 100644 --- a/src/parse/parser.rs +++ b/src/parse/parser.rs @@ -1,7 +1,7 @@ // Std use std::cell::Cell; use std::ffi::{OsStr, OsString}; -use std::io::{self, BufWriter, Write}; +use std::io::Write; use std::iter::Peekable; use std::mem; #[cfg(all( @@ -1545,13 +1545,12 @@ where fn version_err(&self, use_long: bool) -> ClapError { debugln!("Parser::version_err: "); - let out = io::stdout(); - let mut buf_w = BufWriter::new(out.lock()); - match self.print_version(&mut buf_w, use_long) { + let mut buf = vec![]; + match self.print_version(&mut buf, use_long) { Err(e) => e, _ => ClapError { cause: String::new(), - message: String::new(), + message: String::from_utf8(buf).unwrap_or_default(), kind: ErrorKind::VersionDisplayed, info: None, }, diff --git a/tests/version.rs b/tests/version.rs index 750d608b..6a934777 100644 --- a/tests/version.rs +++ b/tests/version.rs @@ -18,7 +18,9 @@ fn version_short() { .try_get_matches_from(vec!["myprog", "-V"]); assert!(m.is_err()); - assert_eq!(m.unwrap_err().kind, ErrorKind::VersionDisplayed); + let err = m.unwrap_err(); + assert_eq!(err.kind, ErrorKind::VersionDisplayed); + assert_eq!(err.message, "test 1.3"); } #[test] @@ -30,7 +32,9 @@ fn version_long() { .try_get_matches_from(vec!["myprog", "--version"]); assert!(m.is_err()); - assert_eq!(m.unwrap_err().kind, ErrorKind::VersionDisplayed); + let err = m.unwrap_err(); + assert_eq!(err.kind, ErrorKind::VersionDisplayed); + assert_eq!(err.message, "test 1.3"); } #[test]