Skip to content

Test failure output for headr is unfortunate #3

@carols10cents

Description

@carols10cents

Putting this as an issue on this repo rather than commenting on the book because this problem isn't actually anything to do with the book text; it only exists in this repo.

I realize that because headr in chapter 3 is demonstrating how splitting UTF-8 codepoints when requesting a number of bytes results in invalid UTF-8, you can't match on Rust strings and you have to use predicate::eq(&expected.as_bytes() as &[u8]). Buuut it makes trying to figure out why a test is failing rather frustrating :(

For example:

---- multiple_files_c1 stdout ----
thread 'multiple_files_c1' panicked at 'Unexpected stdout, failed var == [61, 61, 62, 32, 46, 47, 116, 101, 115, 116, 115, 47, 105, 110, 112, 117, 116, 115, 47, 101, 109, 112, 116, 121, 46, 116, 120, 116, 32, 60, 61, 61, 10, 10, 61, 61, 62, 32, 46, 47, 116, 101, 115, 116, 115, 47, 105, 110, 112, 117, 116, 115, 47, 111, 110, 101, 46, 116, 120, 116, 32, 60, 61, 61, 10, 239, 191, 189, 10, 61, 61, 62, 32, 46, 47, 116, 101, 115, 116, 115, 47, 105, 110, 112, 117, 116, 115, 47, 116, 119, 111, 46, 116, 120, 116, 32, 60, 61, 61, 10, 84, 10, 61, 61, 62, 32, 46, 47, 116, 101, 115, 116, 115, 47, 105, 110, 112, 117, 116, 115, 47, 116, 104, 114, 101, 101, 46, 116, 120, 116, 32, 60, 61, 61, 10, 84]

command=`"/Users/carolnichols/rust/hands-on/carols-solutions/headr2/target/debug/headr" "./tests/inputs/empty.txt" "./tests/inputs/one.txt" "./tests/inputs/two.txt" "./tests/inputs/three.txt" "-c" "1"`
code=0
stdout=```"Öne line, four words.\nTwo lines.\nFour words.\nThree\r\nlines,\r\nfour words.\n"```
stderr=```""```
', /Users/carolnichols/.cargo/registry/src/github.com-1ecc6299db9ec823/assert_cmd-1.0.8/src/assert.rs:124:9

Or a different one where I am printing the invalid UTF-8 correctly (but messed up the newlines, but it's hard to tell that from this message):

---- multiple_files_c1 stdout ----
thread 'multiple_files_c1' panicked at 'Unexpected stdout, failed var == [61, 61, 62, 32, 46, 47, 116, 101, 115, 116, 115, 47, 105, 110, 112, 117, 116, 115, 47, 101, 109, 112, 116, 121, 46, 116, 120, 116, 32, 60, 61, 61, 10, 10, 61, 61, 62, 32, 46, 47, 116, 101, 115, 116, 115, 47, 105, 110, 112, 117, 116, 115, 47, 111, 110, 101, 46, 116, 120, 116, 32, 60, 61, 61, 10, 239, 191, 189, 10, 61, 61, 62, 32, 46, 47, 116, 101, 115, 116, 115, 47, 105, 110, 112, 117, 116, 115, 47, 116, 119, 111, 46, 116, 120, 116, 32, 60, 61, 61, 10, 84, 10, 61, 61, 62, 32, 46, 47, 116, 101, 115, 116, 115, 47, 105, 110, 112, 117, 116, 115, 47, 116, 104, 114, 101, 101, 46, 116, 120, 116, 32, 60, 61, 61, 10, 84]

command=`"/Users/carolnichols/rust/hands-on/carols-solutions/headr2/target/debug/headr" "./tests/inputs/empty.txt" "./tests/inputs/one.txt" "./tests/inputs/two.txt" "./tests/inputs/three.txt" "-c" "1"`
code=0
stdout=```"==> ./tests/inputs/empty.txt <==\n\n\n==> ./tests/inputs/one.txt <==\n\n�\n==> ./tests/inputs/two.txt <==\n\nT\n==> ./tests/inputs/three.txt <==\n\nT"```
stderr=```""```
', /Users/carolnichols/.cargo/registry/src/github.com-1ecc6299db9ec823/assert_cmd-1.0.8/src/assert.rs:124:9

The problem is that it prints the expected bytes but prints the actual stdout as string.

I wanted to send this as a PR rather than an issue, but I haven't found a great solution and wanted to brainstorm some ideas with you...

  • In the test run function, use try_stdout instead of stdout and, if the result is_err, do some custom printing instead of, or in addition to, what assert_cmd prints
  • Call this an upstream bug and file it with assert_cmd/predicate
  • Do the assertion some other way, losing all the nice stuff that comes with assert_cmd
  • Something else???? I thought I had other ideas but they are no longer in my brain.

What do you think?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions