From 5abc1ebc0d7d00ef31d1832b2baab7d44797a7d6 Mon Sep 17 00:00:00 2001 From: rolv Date: Wed, 13 Nov 2024 13:07:12 +0000 Subject: [PATCH] tests(cli): add basic integration tests for the CLI --- Cargo.lock | 139 +++++++++++++++++++++++++++++++++++++++++++++++ cli/Cargo.toml | 4 ++ cli/tests/cli.rs | 117 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 260 insertions(+) create mode 100644 cli/tests/cli.rs diff --git a/Cargo.lock b/Cargo.lock index 0be20d9..138decc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,15 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + [[package]] name = "android-tzdata" version = "0.1.1" @@ -87,6 +96,22 @@ version = "1.0.91" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c042108f3ed77fd83760a5fd79b53be043192bb3b9dba91d8c574c0ada7850c8" +[[package]] +name = "assert_cmd" +version = "2.0.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc1835b7f27878de8525dc71410b5a31cdcc5f230aed5ba5df968e09c201b23d" +dependencies = [ + "anstyle", + "bstr", + "doc-comment", + "libc", + "predicates", + "predicates-core", + "predicates-tree", + "wait-timeout", +] + [[package]] name = "atomic-waker" version = "1.1.2" @@ -126,6 +151,17 @@ version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" +[[package]] +name = "bstr" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40723b8fb387abc38f4f4a37c09073622e41dd12327033091ef8950659e6dc0c" +dependencies = [ + "memchr", + "regex-automata", + "serde", +] + [[package]] name = "bumpalo" version = "3.16.0" @@ -270,6 +306,12 @@ version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8" +[[package]] +name = "difflib" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6184e33543162437515c2e2b48714794e37845ec9851711914eec9d308f6ebe8" + [[package]] name = "displaydoc" version = "0.2.5" @@ -281,6 +323,12 @@ dependencies = [ "syn", ] +[[package]] +name = "doc-comment" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" + [[package]] name = "encoding_rs" version = "0.8.35" @@ -324,6 +372,15 @@ version = "2.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e8c02a5121d4ea3eb16a80748c74f5549a5665e4c21333c6098f283870fbdea6" +[[package]] +name = "float-cmp" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98de4bbd547a563b716d8dfa9aad1cb19bfab00f4fa09a6a4ed21dbcf44ce9c4" +dependencies = [ + "num-traits", +] + [[package]] name = "fnv" version = "1.0.7" @@ -359,12 +416,14 @@ name = "frankfurter_cli" version = "0.0.0" dependencies = [ "anyhow", + "assert_cmd", "chrono", "clap", "comfy-table", "enum_dispatch", "is-terminal", "lib_frankfurter", + "predicates", "serde_json", "strum", "termcolor", @@ -893,6 +952,12 @@ dependencies = [ "tempfile", ] +[[package]] +name = "normalize-line-endings" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61807f77802ff30975e01f4f071c8ba10c022052f98b3294119f3e615d13e5be" + [[package]] name = "num-traits" version = "0.2.19" @@ -1008,6 +1073,36 @@ version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2" +[[package]] +name = "predicates" +version = "3.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e9086cc7640c29a356d1a29fd134380bee9d8f79a17410aa76e7ad295f42c97" +dependencies = [ + "anstyle", + "difflib", + "float-cmp", + "normalize-line-endings", + "predicates-core", + "regex", +] + +[[package]] +name = "predicates-core" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae8177bee8e75d6846599c6b9ff679ed51e882816914eec639944d7c9aa11931" + +[[package]] +name = "predicates-tree" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41b740d195ed3166cd147c8047ec98db0e22ec019eb8eeb76d343b795304fb13" +dependencies = [ + "predicates-core", + "termtree", +] + [[package]] name = "pretty_assertions" version = "1.4.1" @@ -1045,6 +1140,35 @@ dependencies = [ "bitflags", ] +[[package]] +name = "regex" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" + [[package]] name = "reqwest" version = "0.12.9" @@ -1407,6 +1531,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "termtree" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3369f5ac52d5eb6ab48c6b4ffdc8efbcad6b89c765749064ba298f2c68a16a76" + [[package]] name = "thiserror" version = "1.0.65" @@ -1583,6 +1713,15 @@ version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" +[[package]] +name = "wait-timeout" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f200f5b12eb75f8c1ed65abd4b2db8a6e1b138a20de009dacee265a2498f3f6" +dependencies = [ + "libc", +] + [[package]] name = "want" version = "0.3.1" diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 2c22ea8..5026828 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -26,3 +26,7 @@ termcolor = { version = "1.4.1" } is-terminal = { version = "0.4.13" } comfy-table = "7.1.1" anyhow = "1.0.91" + +[dev-dependencies] +assert_cmd = "2.0.16" +predicates = "3.1.2" diff --git a/cli/tests/cli.rs b/cli/tests/cli.rs new file mode 100644 index 0000000..a7dae5e --- /dev/null +++ b/cli/tests/cli.rs @@ -0,0 +1,117 @@ +use assert_cmd::Command; +use predicates::{ + prelude::PredicateBooleanExt, + str::{contains, ends_with, is_match, starts_with}, +}; + +fn get_cmd() -> Command { + let mut cmd = Command::cargo_bin("frs").unwrap(); + cmd.arg("--url=http://localhost:8080"); + cmd +} + +#[test] +fn test_currencies_basic() { + get_cmd() + .arg("currencies") + .assert() + .stdout( + contains("AUD") + .and(contains("USD")) + .and(contains("GBP")) + .and(contains("United States Dollar")) + .and(is_match("\\d").unwrap().not()), + ) + .success(); +} + +#[test] +fn test_currencies_json() { + get_cmd() + .arg("currencies") + .arg("--json") + .assert() + .stdout( + starts_with("{") + .and(ends_with("}\n")) + .and(contains("\"EUR\": \"Euro\"")), + ) + .success(); +} + +#[test] +fn test_currencies_raw() { + get_cmd() + .arg("currencies") + .arg("--raw") + .assert() + .stdout(starts_with("AUD\tAustralian Dollar").and(contains("EUR\tEuro"))) + .success(); +} + +#[test] +fn test_convert_basic() { + get_cmd() + .arg("convert") + .assert() + .stdout( + contains("AUD") + .and(contains("USD")) + .and(contains("GBP")) + .and(is_match("\\d").unwrap()), + ) + .success(); +} + +#[test] +fn test_convert_targets() { + get_cmd() + .args(["convert", "USD", "EUR,GBP"]) + .assert() + .stdout( + contains("EUR") + .and(contains("GBP")) + .and(is_match("\\d").unwrap()) + .and(contains("USD").not()), + ) + .success(); +} + +#[test] +fn test_convert_amount() { + get_cmd() + .args(["convert", "-a", "1000", "--json"]) + .assert() + .stdout(contains("1000").and(is_match("\\d").unwrap())) + .success(); +} + +#[test] +fn test_period_basic() { + get_cmd() + .args(["period", "EUR", "2024-10-10"]) + .assert() + .stdout( + contains("2024-10-10") + .and(contains("2024-10-11")) + .and(contains("2024-11-05")) + .and(contains("AUD")) + .and(contains("USD")) + .and(contains("GBP")), + ) + .success(); +} + +#[test] +fn test_period_end_date() { + get_cmd() + .args(["period", "EUR", "2020-5-12", "2020-5-13"]) + .assert() + .stdout( + contains("2020-05-12") + .and(contains("2020-05-13")) + .and(contains("2024-05-11").not()) + .and(contains("2024-05-14").not()), + ) + .success(); +}