From 131cbe0fcb0f4e6a76bba71929247d48b154ac69 Mon Sep 17 00:00:00 2001 From: Claudio Noguera Date: Sun, 7 Feb 2021 18:43:16 +0100 Subject: [PATCH] Increase coverage I already had tests that should have a bigger coverage than reported. Apparently there is an [issue with assert_cmd](https://github.com/assert-rs/assert_cmd/issues/9) that makes it not show coverage correctly for tarpaulin. So the workaround is to test as needed with assert_cmd and in the same function call one that re-runs the command executing run_app and only checks that the result is ok. --- tests/common.rs | 9 +++++ tests/test_commands.rs | 76 +++++++++++++++++------------------------- 2 files changed, 39 insertions(+), 46 deletions(-) diff --git a/tests/common.rs b/tests/common.rs index 8b13789..01cfba3 100644 --- a/tests/common.rs +++ b/tests/common.rs @@ -1 +1,10 @@ +use dinero::run_app; +pub fn test_args(args: &[&str]) { + let mut function_args: Vec<&str> = vec!["testing"]; + for arg in args { + function_args.push(arg); + } + let res = run_app(function_args.iter().map(|x| x.to_string()).collect()); + assert!(res.is_ok()); +} diff --git a/tests/test_commands.rs b/tests/test_commands.rs index 9d8d605..ed96eb4 100644 --- a/tests/test_commands.rs +++ b/tests/test_commands.rs @@ -1,28 +1,28 @@ use assert_cmd::Command; -use dinero::run_app; +use common::test_args; +mod common; #[test] fn date_filters() { - let assert_1 = Command::cargo_bin("dinero") - .unwrap() - .args(&["bal", "-f", "examples/demo.ledger"]) - .assert(); + let args1 = &["bal", "-f", "examples/demo.ledger"]; + let assert_1 = Command::cargo_bin("dinero").unwrap().args(args1).assert(); let mut output = String::from_utf8(assert_1.get_output().to_owned().stdout).unwrap(); assert_eq!(output.lines().into_iter().count(), 18); - let assert_2 = Command::cargo_bin("dinero") - .unwrap() - .args(&[ - "bal", - "-f", - "examples/demo.ledger", - "-e", - "2021-01-17", - "-b", - "2021-01-15", - "--force-color", - ]) - .assert(); + test_args(args1); + let args2 = &[ + "bal", + "-f", + "examples/demo.ledger", + "-e", + "2021-01-17", + "-b", + "2021-01-15", + "--force-color", + ]; + let assert_2 = Command::cargo_bin("dinero").unwrap().args(args2).assert(); output = String::from_utf8(assert_2.get_output().to_owned().stdout).unwrap(); assert_eq!(output.lines().into_iter().count(), 13); + + test_args(args2); } /// A test for [issue 18](https://github.com/frosklis/dinero-rs/issues/18) @@ -30,11 +30,10 @@ fn date_filters() { fn exchange() { let mut outputs = Vec::new(); for _ in 0..100 { - let assert = Command::cargo_bin("dinero") - .unwrap() - .args(&["bal", "-f", "examples/demo.ledger", "-X", "EUR"]) - .assert(); + let args = &["bal", "-f", "examples/demo.ledger", "-X", "EUR"]; + let assert = Command::cargo_bin("dinero").unwrap().args(args).assert(); outputs.push(String::from_utf8(assert.get_output().to_owned().stdout).unwrap()); + test_args(args); } for i in 1..100 { assert_eq!(outputs[i], outputs[0], "output mismatch"); @@ -48,11 +47,10 @@ fn commodity_alias() { let mut outputs = Vec::new(); let aliases = vec!["EUR", "eur"]; for alias in aliases { - let assert = Command::cargo_bin("dinero") - .unwrap() - .args(&["bal", "-f", "examples/demo.ledger", "-X", alias]) - .assert(); + let args = &["bal", "-f", "examples/demo.ledger", "-X", alias]; + let assert = Command::cargo_bin("dinero").unwrap().args(args).assert(); outputs.push(String::from_utf8(assert.get_output().to_owned().stdout).unwrap()); + test_args(args); } assert_eq!(outputs[0], outputs[1], "output mismatch"); } @@ -60,34 +58,20 @@ fn commodity_alias() { #[test] /// Check that the register report is showing virtual postings fn virtual_postings() { - let assert_1 = Command::cargo_bin("dinero") - .unwrap() - .args(&["reg", "-f", "examples/virtual_postings.ledger"]) - .assert(); + let args = &["reg", "-f", "examples/virtual_postings.ledger"]; + let assert_1 = Command::cargo_bin("dinero").unwrap().args(args).assert(); let output = String::from_utf8(assert_1.get_output().to_owned().stdout).unwrap(); assert_eq!(output.lines().into_iter().count(), 7); + test_args(args); } #[test] /// Check that the virtual postings are being filtered out fn real_filter() { - let assert_1 = Command::cargo_bin("dinero") - .unwrap() - .args(&["reg", "-f", "examples/virtual_postings.ledger", "--real"]) - .assert(); + let args = &["reg", "-f", "examples/virtual_postings.ledger", "--real"]; + let assert_1 = Command::cargo_bin("dinero").unwrap().args(args).assert(); let output = String::from_utf8(assert_1.get_output().to_owned().stdout).unwrap(); assert_eq!(output.lines().into_iter().count(), 4); - let args: Vec = vec![ - "testing", - "reg", - "-f", - "examples/virtual_postings.ledger", - "--real", - ] - .iter() - .map(|x| x.to_string()) - .collect(); - let res = run_app(args); - assert!(res.is_ok()); + test_args(args); }