From 29c6ad5f6af39b24bde315f9687807af0d318c39 Mon Sep 17 00:00:00 2001 From: Julio Rincon Date: Fri, 8 Feb 2019 07:54:48 +1100 Subject: [PATCH] tests: untrimmed stdout assertion (fix #1235) --- src/cat/cat.rs | 4 ++- src/join/join.rs | 15 +++++--- tests/common/util.rs | 6 ++-- .../join/semicolon_separated.expected | 1 - .../sort/numeric_floats_with_nan.expected | 2 +- .../fixtures/sort/numeric_floats_with_nan.txt | 2 +- tests/test_basename.rs | 12 +++---- tests/test_cat.rs | 10 +++--- tests/test_dirname.rs | 10 +++--- tests/test_echo.rs | 6 ++-- tests/test_join.rs | 14 ++++++-- tests/test_numfmt.rs | 34 +++++++++---------- tests/test_pinky.rs | 2 +- tests/test_pwd.rs | 2 +- tests/test_readlink.rs | 6 ++-- tests/test_realpath.rs | 6 ++-- 16 files changed, 73 insertions(+), 59 deletions(-) mode change 100644 => 100755 src/cat/cat.rs mode change 100644 => 100755 src/join/join.rs mode change 100644 => 100755 tests/common/util.rs diff --git a/src/cat/cat.rs b/src/cat/cat.rs old mode 100644 new mode 100755 index 5885a7c0f..37762fb8e --- a/src/cat/cat.rs +++ b/src/cat/cat.rs @@ -419,6 +419,7 @@ fn write_to_end(in_buf: &[u8], writer: &mut W) -> usize { } fn write_tab_to_end(mut in_buf: &[u8], writer: &mut W) -> usize { + let mut count = 0; loop { match in_buf .iter() @@ -427,10 +428,11 @@ fn write_tab_to_end(mut in_buf: &[u8], writer: &mut W) -> usize { Some(p) => { writer.write_all(&in_buf[..p]).unwrap(); if in_buf[p] == b'\n' { - return p + 1; + return count + p + 1; } else { writer.write_all(b"^I").unwrap(); in_buf = &in_buf[p + 1..]; + count += p + 1; } } None => { diff --git a/src/join/join.rs b/src/join/join.rs old mode 100644 new mode 100755 index 2f26de014..6d2ccb7ed --- a/src/join/join.rs +++ b/src/join/join.rs @@ -106,12 +106,17 @@ impl<'a> Repr<'a> { } /// Print each field except the one at the index. - fn print_fields(&self, line: &Line, index: usize, max_fields: usize) { - for i in 0..min(max_fields, line.fields.len()) { + fn print_fields(&self, line: &Line, index: usize, max_fields: Option) { + for i in 0..min(max_fields.unwrap_or(usize::max_value()), line.fields.len()) { if i != index { print!("{}{}", self.separator, line.fields[i]); } } + if let Some(n) = max_fields { + for _ in line.fields.len()..n { + print!("{}", self.separator) + } + } } /// Print each field or the empty filler if the field is not set. @@ -232,7 +237,7 @@ struct State<'a> { print_unpaired: bool, lines: Lines>, seq: Vec, - max_fields: usize, + max_fields: Option, line_num: usize, has_failed: bool, } @@ -261,7 +266,7 @@ impl<'a> State<'a> { print_unpaired: print_unpaired == file_num, lines: f.lines(), seq: Vec::new(), - max_fields: usize::max_value(), + max_fields: None, line_num: 0, has_failed: false, } @@ -363,7 +368,7 @@ impl<'a> State<'a> { fn initialize(&mut self, read_sep: Sep, autoformat: bool) { if let Some(line) = self.read_line(read_sep) { if autoformat { - self.max_fields = line.fields.len(); + self.max_fields = Some(line.fields.len()); } self.seq.push(line); diff --git a/tests/common/util.rs b/tests/common/util.rs old mode 100644 new mode 100755 index 1a0e4205e..9bf212cc6 --- a/tests/common/util.rs +++ b/tests/common/util.rs @@ -89,12 +89,12 @@ impl CmdResult { } /// asserts that the command resulted in stdout stream output that equals the - /// passed in value, when both are trimmed of trailing whitespace + /// passed in value, trailing whitespace are kept to force strict comparison (#1235) /// stdout_only is a better choice unless stderr may or will be non-empty pub fn stdout_is>(&self, msg: T) -> Box<&CmdResult> { assert_eq!( - String::from(msg.as_ref()).trim_right(), - self.stdout.trim_right() + String::from(msg.as_ref()), + self.stdout ); Box::new(self) } diff --git a/tests/fixtures/join/semicolon_separated.expected b/tests/fixtures/join/semicolon_separated.expected index ad97ecf88..21d4f24b1 100644 --- a/tests/fixtures/join/semicolon_separated.expected +++ b/tests/fixtures/join/semicolon_separated.expected @@ -1,4 +1,3 @@ 2 ;b;x 3; c; y 4 ;d;z - diff --git a/tests/fixtures/sort/numeric_floats_with_nan.expected b/tests/fixtures/sort/numeric_floats_with_nan.expected index d4bdc6bd9..33cb481d0 100644 --- a/tests/fixtures/sort/numeric_floats_with_nan.expected +++ b/tests/fixtures/sort/numeric_floats_with_nan.expected @@ -1,3 +1,3 @@ NaN .02 -.03 \ No newline at end of file +.03 diff --git a/tests/fixtures/sort/numeric_floats_with_nan.txt b/tests/fixtures/sort/numeric_floats_with_nan.txt index d40d01c62..d3b7c2bc3 100644 --- a/tests/fixtures/sort/numeric_floats_with_nan.txt +++ b/tests/fixtures/sort/numeric_floats_with_nan.txt @@ -1,3 +1,3 @@ .03 .02 -NaN \ No newline at end of file +NaN diff --git a/tests/test_basename.rs b/tests/test_basename.rs index c72f2a856..0402d2b40 100644 --- a/tests/test_basename.rs +++ b/tests/test_basename.rs @@ -4,23 +4,23 @@ use common::util::*; #[test] fn test_directory() { new_ucmd!().args(&["/root/alpha/beta/gamma/delta/epsilon/omega/"]) - .succeeds().stdout_only("omega"); + .succeeds().stdout_only("omega\n"); } #[test] fn test_file() { - new_ucmd!().args(&["/etc/passwd"]).succeeds().stdout_only("passwd"); + new_ucmd!().args(&["/etc/passwd"]).succeeds().stdout_only("passwd\n"); } #[test] fn test_remove_suffix() { new_ucmd!().args(&["/usr/local/bin/reallylongexecutable.exe", ".exe"]) - .succeeds().stdout_only("reallylongexecutable"); + .succeeds().stdout_only("reallylongexecutable\n"); } #[test] fn test_dont_remove_suffix() { - new_ucmd!().args(&["/foo/bar/baz", "baz"]).succeeds().stdout_only( "baz"); + new_ucmd!().args(&["/foo/bar/baz", "baz"]).succeeds().stdout_only( "baz\n"); } #[test] @@ -28,7 +28,7 @@ fn test_multiple_param() { for multiple_param in vec!["-a", "--multiple"] { let path = "/foo/bar/baz"; new_ucmd!().args(&[multiple_param, path, path]) - .succeeds().stdout_only("baz\nbaz"); + .succeeds().stdout_only("baz\nbaz\n"); } } @@ -38,7 +38,7 @@ fn test_suffix_param() { let path = "/foo/bar/baz.exe"; new_ucmd!() .args(&[suffix_param, ".exe", path, path]) - .succeeds().stdout_only("baz\nbaz"); + .succeeds().stdout_only("baz\nbaz\n"); } } diff --git a/tests/test_cat.rs b/tests/test_cat.rs index 027bfddd1..51e7e8278 100644 --- a/tests/test_cat.rs +++ b/tests/test_cat.rs @@ -38,7 +38,7 @@ fn test_stdin_show_nonprinting() { .args(&[same_param]) .pipe_in("\t\0\n") .succeeds() - .stdout_only("\t^@"); + .stdout_only("\t^@\n"); } } @@ -49,7 +49,7 @@ fn test_stdin_show_tabs() { .args(&[same_param]) .pipe_in("\t\0\n") .succeeds() - .stdout_only("^I\0"); + .stdout_only("^I\0\n"); } } @@ -59,9 +59,9 @@ fn test_stdin_show_ends() { for same_param in vec!["-E", "--show-ends"] { new_ucmd!() .args(&[same_param,"-"]) - .pipe_in("\t\0\n") + .pipe_in("\t\0\n\t") .succeeds() - .stdout_only("\t\0$"); + .stdout_only("\t\0$\n\t"); } } @@ -72,7 +72,7 @@ fn test_stdin_show_all() { .args(&[same_param]) .pipe_in("\t\0\n") .succeeds() - .stdout_only("^I^@$"); + .stdout_only("^I^@$\n"); } } diff --git a/tests/test_dirname.rs b/tests/test_dirname.rs index 4fa772c32..0875bd8fb 100644 --- a/tests/test_dirname.rs +++ b/tests/test_dirname.rs @@ -4,26 +4,26 @@ use common::util::*; #[test] fn test_path_with_trailing_slashes() { new_ucmd!().arg("/root/alpha/beta/gamma/delta/epsilon/omega//") - .run().stdout_is("/root/alpha/beta/gamma/delta/epsilon"); + .run().stdout_is("/root/alpha/beta/gamma/delta/epsilon\n"); } #[test] fn test_path_without_trailing_slashes() { new_ucmd!().arg("/root/alpha/beta/gamma/delta/epsilon/omega") - .run().stdout_is("/root/alpha/beta/gamma/delta/epsilon"); + .run().stdout_is("/root/alpha/beta/gamma/delta/epsilon\n"); } #[test] fn test_root() { - new_ucmd!().arg("/").run().stdout_is("/"); + new_ucmd!().arg("/").run().stdout_is("/\n"); } #[test] fn test_pwd() { - new_ucmd!().arg(".").run().stdout_is("."); + new_ucmd!().arg(".").run().stdout_is(".\n"); } #[test] fn test_empty() { - new_ucmd!().arg("").run().stdout_is("."); + new_ucmd!().arg("").run().stdout_is(".\n"); } diff --git a/tests/test_echo.rs b/tests/test_echo.rs index 1c9e7417f..2f25f95f2 100644 --- a/tests/test_echo.rs +++ b/tests/test_echo.rs @@ -45,12 +45,12 @@ fn test_escape_form_feed() { #[test] fn test_escape_hex() { - new_ucmd!().args(&["-e", "\\x41"]).succeeds().stdout_only("A"); + new_ucmd!().args(&["-e", "\\x41"]).succeeds().stdout_only("A\n"); } #[test] fn test_escape_newline() { - new_ucmd!().args(&["-e", "\\na"]).succeeds().stdout_only("\na"); + new_ucmd!().args(&["-e", "\\na"]).succeeds().stdout_only("\na\n"); } #[test] @@ -60,7 +60,7 @@ fn test_escape_no_further_output() { #[test] fn test_escape_octal() { - new_ucmd!().args(&["-e", "\\0100"]).succeeds().stdout_only("@"); + new_ucmd!().args(&["-e", "\\0100"]).succeeds().stdout_only("@\n"); } #[test] diff --git a/tests/test_join.rs b/tests/test_join.rs index 8756d6af0..2c1a73bad 100644 --- a/tests/test_join.rs +++ b/tests/test_join.rs @@ -124,7 +124,7 @@ fn new_line_separated() { .arg("-t") .arg("") .pipe_in("1 a\n1 b\n8 h\n") - .succeeds().stdout_only("1 a\n8 h"); + .succeeds().stdout_only("1 a\n8 h\n"); } #[test] @@ -174,6 +174,14 @@ fn autoformat() { .arg("-o") .arg("auto") .succeeds().stdout_only_fixture("autoformat.expected"); + + new_ucmd!() + .arg("-") + .arg("fields_2.txt") + .arg("-o") + .arg("auto") + .pipe_in("1 x y z\n2 p") + .succeeds().stdout_only("1 x y z a\n2 p b\n"); } #[test] @@ -246,11 +254,11 @@ fn single_file_with_header() { .arg("capitalized.txt") .arg("empty.txt") .arg("--header") - .succeeds().stdout_is("A 1"); + .succeeds().stdout_is("A 1\n"); new_ucmd!() .arg("empty.txt") .arg("capitalized.txt") .arg("--header") - .succeeds().stdout_is("A 1"); + .succeeds().stdout_is("A 1\n"); } diff --git a/tests/test_numfmt.rs b/tests/test_numfmt.rs index 56f5a0a95..c8b734970 100644 --- a/tests/test_numfmt.rs +++ b/tests/test_numfmt.rs @@ -6,7 +6,7 @@ fn test_from_si() { .args(&["--from=si"]) .pipe_in("1000\n1.1M\n0.1G") .run() - .stdout_is("1000\n1100000\n100000000"); + .stdout_is("1000\n1100000\n100000000\n"); } #[test] @@ -15,7 +15,7 @@ fn test_from_iec() { .args(&["--from=iec"]) .pipe_in("1024\n1.1M\n0.1G") .run() - .stdout_is("1024\n1153434\n107374182"); + .stdout_is("1024\n1153434\n107374182\n"); } #[test] @@ -24,7 +24,7 @@ fn test_from_iec_i() { .args(&["--from=iec-i"]) .pipe_in("1024\n1.1Mi\n0.1Gi") .run() - .stdout_is("1024\n1153434\n107374182"); + .stdout_is("1024\n1153434\n107374182\n"); } #[test] @@ -33,7 +33,7 @@ fn test_from_auto() { .args(&["--from=auto"]) .pipe_in("1K\n1Ki") .run() - .stdout_is("1000\n1024"); + .stdout_is("1000\n1024\n"); } #[test] @@ -42,7 +42,7 @@ fn test_to_si() { .args(&["--to=si"]) .pipe_in("1000\n1100000\n100000000") .run() - .stdout_is("1.0K\n1.1M\n100.0M"); + .stdout_is("1.0K\n1.1M\n100.0M\n"); } #[test] @@ -51,7 +51,7 @@ fn test_to_iec() { .args(&["--to=iec"]) .pipe_in("1024\n1153434\n107374182") .run() - .stdout_is("1.0K\n1.1M\n102.4M"); + .stdout_is("1.0K\n1.1M\n102.4M\n"); } #[test] @@ -60,7 +60,7 @@ fn test_to_iec_i() { .args(&["--to=iec-i"]) .pipe_in("1024\n1153434\n107374182") .run() - .stdout_is("1.0Ki\n1.1Mi\n102.4Mi"); + .stdout_is("1.0Ki\n1.1Mi\n102.4Mi\n"); } #[test] @@ -68,7 +68,7 @@ fn test_input_from_free_arguments() { new_ucmd!() .args(&["--from=si", "1K", "1.1M", "0.1G"]) .run() - .stdout_is("1000\n1100000\n100000000"); + .stdout_is("1000\n1100000\n100000000\n"); } #[test] @@ -77,7 +77,7 @@ fn test_padding() { .args(&["--from=si", "--padding=8"]) .pipe_in("1K\n1.1M\n0.1G") .run() - .stdout_is(" 1000\n 1100000\n100000000"); + .stdout_is(" 1000\n 1100000\n100000000\n"); } #[test] @@ -86,7 +86,7 @@ fn test_negative_padding() { .args(&["--from=si", "--padding=-8"]) .pipe_in("1K\n1.1M\n0.1G") .run() - .stdout_is("1000 \n1100000 \n100000000"); + .stdout_is("1000 \n1100000 \n100000000\n"); } #[test] @@ -95,7 +95,7 @@ fn test_header() { .args(&["--from=si", "--header=2"]) .pipe_in("header\nheader2\n1K\n1.1M\n0.1G") .run() - .stdout_is("header\nheader2\n1000\n1100000\n100000000"); + .stdout_is("header\nheader2\n1000\n1100000\n100000000\n"); } #[test] @@ -104,7 +104,7 @@ fn test_header_default() { .args(&["--from=si", "--header"]) .pipe_in("header\n1K\n1.1M\n0.1G") .run() - .stdout_is("header\n1000\n1100000\n100000000"); + .stdout_is("header\n1000\n1100000\n100000000\n"); } #[test] @@ -113,12 +113,12 @@ fn test_negative() { .args(&["--from=si"]) .pipe_in("-1000\n-1.1M\n-0.1G") .run() - .stdout_is("-1000\n-1100000\n-100000000"); + .stdout_is("-1000\n-1100000\n-100000000\n"); new_ucmd!() .args(&["--to=iec-i"]) .pipe_in("-1024\n-1153434\n-107374182") .run() - .stdout_is("-1.0Ki\n-1.1Mi\n-102.4Mi"); + .stdout_is("-1.0Ki\n-1.1Mi\n-102.4Mi\n"); } #[test] @@ -126,7 +126,7 @@ fn test_no_op() { new_ucmd!() .pipe_in("1024\n1234567") .run() - .stdout_is("1024\n1234567"); + .stdout_is("1024\n1234567\n"); } #[test] @@ -135,7 +135,7 @@ fn test_normalize() { .args(&["--from=si", "--to=si"]) .pipe_in("10000000K\n0.001K") .run() - .stdout_is("10.0G\n1"); + .stdout_is("10.0G\n1\n"); } #[test] @@ -143,5 +143,5 @@ fn test_si_to_iec() { new_ucmd!() .args(&["--from=si", "--to=iec", "15334263563K"]) .run() - .stdout_is("13.9T"); + .stdout_is("13.9T\n"); } diff --git a/tests/test_pinky.rs b/tests/test_pinky.rs index 0821a40ba..377abe74d 100644 --- a/tests/test_pinky.rs +++ b/tests/test_pinky.rs @@ -23,7 +23,7 @@ fn test_long_format() { new_ucmd!() .arg("-l").arg(ulogin) .run() - .stdout_is(format!("Login name: {:<28}In real life: {}\nDirectory: {:<29}Shell: {}\n", + .stdout_is(format!("Login name: {:<28}In real life: {}\nDirectory: {:<29}Shell: {}\n\n", ulogin, real_name, pw.user_dir(), pw.user_shell())); new_ucmd!() diff --git a/tests/test_pwd.rs b/tests/test_pwd.rs index e003d1c36..c17ac27d4 100644 --- a/tests/test_pwd.rs +++ b/tests/test_pwd.rs @@ -4,5 +4,5 @@ use common::util::*; #[test] fn test_default() { let (at, mut ucmd) = at_and_ucmd!(); - ucmd.run().stdout_is(at.root_dir_resolved()); + ucmd.run().stdout_is(at.root_dir_resolved() + "\n"); } diff --git a/tests/test_readlink.rs b/tests/test_readlink.rs index 000c15584..e8088dcd6 100644 --- a/tests/test_readlink.rs +++ b/tests/test_readlink.rs @@ -9,7 +9,7 @@ fn test_canonicalize() { ucmd.arg("-f") .arg(".") .run() - .stdout_is(at.root_dir_resolved()); + .stdout_is(at.root_dir_resolved() + "\n"); } #[test] @@ -18,7 +18,7 @@ fn test_canonicalize_existing() { ucmd.arg("-e") .arg(".") .run() - .stdout_is(at.root_dir_resolved()); + .stdout_is(at.root_dir_resolved() + "\n"); } #[test] @@ -28,7 +28,7 @@ fn test_canonicalize_missing() { ucmd.arg("-m") .arg(GIBBERISH) .run() - .stdout_is(expected); + .stdout_is(expected + "\n"); } #[test] diff --git a/tests/test_realpath.rs b/tests/test_realpath.rs index d0fb18f61..efe263a54 100644 --- a/tests/test_realpath.rs +++ b/tests/test_realpath.rs @@ -4,7 +4,7 @@ use common::util::*; #[test] fn test_current_directory() { let (at, mut ucmd) = at_and_ucmd!(); - ucmd.arg(".").run().stdout_is(at.root_dir_resolved()); + ucmd.arg(".").run().stdout_is(at.root_dir_resolved() + "\n"); } #[test] @@ -12,12 +12,12 @@ fn test_long_redirection_to_current_dir() { let (at, mut ucmd) = at_and_ucmd!(); // Create a 256-character path to current directory let dir = path_concat!(".", ..128); - ucmd.arg(dir).run().stdout_is(at.root_dir_resolved()); + ucmd.arg(dir).run().stdout_is(at.root_dir_resolved() + "\n"); } #[test] fn test_long_redirection_to_root() { // Create a 255-character path to root let dir = path_concat!("..", ..85); - new_ucmd!().arg(dir).run().stdout_is(get_root_path()); + new_ucmd!().arg(dir).run().stdout_is(get_root_path().to_owned() + "\n"); }