Closed JimLynchCodes closed 4 months ago
Hi. The extra characters are for colouring information. You can set ColorChoice::Never
to stop printing coloured output for integration tests. https://docs.rs/cli-table/latest/cli_table/struct.TableStruct.html#method.color_choice
Please let me know if it doesn't work.
thanks @devashishdxt! How can I use ColorChoice::Never
to actually set the color in my integration test though? By then hasn't the table already been constructed?
Hmmm I tried to use the cfg macros to control the color choice, but actually this doesn't even work because for integration tests it should use the #[cfg(not(test))] one... 🤔
let table = vec![
vec!["Tom".cell(), 10.cell().justify(Justify::Right)],
vec!["Jerry".cell(), 15.cell().justify(Justify::Right)],
vec!["Scooby Doo".cell(), 20.cell().justify(Justify::Right)],
]
.table()
.title(vec![
"Name".cell().bold(true),
"Age (in years)".cell().bold(true),
])
.color_choice(get_color_choice()) // <--- new thing
.bold(true);
...
#[cfg(not(test))]
fn get_color_choice() -> ColorChoice {
ColorChoice::Auto
}
#[cfg(test)]
fn get_color_choice() -> ColorChoice {
ColorChoice::Never
}
You can just use an environment variable when running integration tests and use that environment variable to disable coloured output. Alternatively, you can try to set NO_COLOR
environment variable which should ideally not output any colour.
Read more about this here: https://github.com/BurntSushi/termcolor#automatic-color-selection
Hi! I used the same code here to render a table in my terminal that looks pretty nice when I run it with
cargo run
. 👍Now I am trying to write an integration test that expects this output to be printed to the console...
Here is the code that I'm trying:
For some reason the output looks really bizarre in the test... lots of strange `{1b} things...
Is the table output not utf8? Is there some other way I should be getting the stdout text so that it's in the same format as it looks when it's printed in the console to be read by a human?
Thanks!