From dabf940bafef49392466a679a3ad46ec6fb36f81 Mon Sep 17 00:00:00 2001 From: cmdoret Date: Sun, 29 Oct 2023 23:38:43 +0100 Subject: [PATCH] test(cli): integration tests --- Cargo.lock | 167 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 4 ++ src/cli.rs | 2 +- src/main.rs | 43 ++++++++++++++ 4 files changed, 215 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 60cd9e5..b57806a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -59,6 +59,33 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "assert_cmd" +version = "2.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88903cb14723e4d4003335bb7f8a14f27691649105346a0f0957466c096adfe6" +dependencies = [ + "anstyle", + "bstr", + "doc-comment", + "predicates", + "predicates-core", + "predicates-tree", + "wait-timeout", +] + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "bitflags" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07" + [[package]] name = "block-buffer" version = "0.10.4" @@ -68,6 +95,17 @@ dependencies = [ "generic-array", ] +[[package]] +name = "bstr" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c79ad7fb2dd38f3dabd76b09c6a5a20c038fc0213ef1e9afd30eb777f120f019" +dependencies = [ + "memchr", + "regex-automata", + "serde", +] + [[package]] name = "cfg-if" version = "1.0.0" @@ -139,6 +177,12 @@ dependencies = [ "typenum", ] +[[package]] +name = "difflib" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6184e33543162437515c2e2b48714794e37845ec9851711914eec9d308f6ebe8" + [[package]] name = "digest" version = "0.10.7" @@ -149,6 +193,34 @@ dependencies = [ "crypto-common", ] +[[package]] +name = "doc-comment" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" + +[[package]] +name = "either" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" + +[[package]] +name = "errno" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3e13f66a2f95e32a39eaa81f6b95d42878ca0e1db0c7543723dfe12557e860" +dependencies = [ + "libc", + "windows-sys", +] + +[[package]] +name = "fastrand" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5" + [[package]] name = "generic-array" version = "0.14.7" @@ -165,6 +237,15 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" +[[package]] +name = "itertools" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1c173a5686ce8bfa551b3563d0c2170bf24ca44da99c7ca4bfdab5418c3fe57" +dependencies = [ + "either", +] + [[package]] name = "lazy_static" version = "1.4.0" @@ -177,6 +258,12 @@ version = "0.2.149" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a08173bc88b7955d1b3145aa561539096c421ac8debde8cbc3612ec635fee29b" +[[package]] +name = "linux-raw-sys" +version = "0.4.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da2479e8c062e40bf0066ffa0bc823de0a9368974af99c9f6df941d2c231e03f" + [[package]] name = "memchr" version = "2.6.4" @@ -201,6 +288,34 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bb175ec8981211357b7b379869c2f8d555881c55ea62311428ec0de46d89bd5c" +[[package]] +name = "predicates" +version = "3.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6dfc28575c2e3f19cb3c73b93af36460ae898d426eba6fc15b9bd2a5220758a0" +dependencies = [ + "anstyle", + "difflib", + "itertools", + "predicates-core", +] + +[[package]] +name = "predicates-core" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b794032607612e7abeb4db69adb4e33590fa6cf1149e95fd7cb00e634b92f174" + +[[package]] +name = "predicates-tree" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "368ba315fb8c5052ab692e68a0eefec6ec57b23a36959c14496f0b0df2c0cecf" +dependencies = [ + "predicates-core", + "termtree", +] + [[package]] name = "proc-macro2" version = "1.0.66" @@ -232,8 +347,19 @@ dependencies = [ name = "rdfpipe-rs" version = "0.1.0" dependencies = [ + "assert_cmd", "clap", "sophia", + "tempfile", +] + +[[package]] +name = "redox_syscall" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa" +dependencies = [ + "bitflags 1.3.2", ] [[package]] @@ -300,6 +426,19 @@ dependencies = [ "rio_api", ] +[[package]] +name = "rustix" +version = "0.38.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b426b0506e5d50a7d8dafcf2e81471400deb602392c7dd110815afb4eaf02a3" +dependencies = [ + "bitflags 2.4.1", + "errno", + "libc", + "linux-raw-sys", + "windows-sys", +] + [[package]] name = "serde" version = "1.0.189" @@ -487,6 +626,25 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "tempfile" +version = "3.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ef1adac450ad7f4b3c28589471ade84f25f731a7a0fe30d71dfa9f60fd808e5" +dependencies = [ + "cfg-if", + "fastrand", + "redox_syscall", + "rustix", + "windows-sys", +] + +[[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.50" @@ -531,6 +689,15 @@ version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" +[[package]] +name = "wait-timeout" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f200f5b12eb75f8c1ed65abd4b2db8a6e1b138a20de009dacee265a2498f3f6" +dependencies = [ + "libc", +] + [[package]] name = "windows-sys" version = "0.48.0" diff --git a/Cargo.toml b/Cargo.toml index 93c6b20..d731041 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,3 +8,7 @@ edition = "2021" [dependencies] clap = { version = "4.4.2", features = ["derive"] } sophia = { version = "0.8.0-alpha.3", features = ["xml"] } + +[dev-dependencies] +assert_cmd = "2.0.12" +tempfile = "3.8.1" diff --git a/src/cli.rs b/src/cli.rs index ef0af19..160da46 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -6,7 +6,7 @@ use std::str::FromStr; // This lets clap automate validation of // RDF formats from the command line -#[derive(Clone, Debug, ValueEnum)] +#[derive(Clone, Debug, PartialEq, ValueEnum)] pub enum GraphFormat { #[clap(alias = "ttl")] Turtle, diff --git a/src/main.rs b/src/main.rs index 447ac64..c87a4e9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -76,3 +76,46 @@ fn main() -> Result<(), Box> { RdfSerializer::serialize(output, output_format, parser.graph)?; Ok(()) } + +#[cfg(test)] +mod tests { + use super::*; + use assert_cmd::Command; + use std::fs::File; + use std::io::Write; + use tempfile::tempdir; + + #[test] + fn test_format_from_path() { + assert_eq!(format_from_path("file.ttl"), Some(GraphFormat::Turtle)); + assert_eq!(format_from_path("file.nt"), Some(GraphFormat::NTriples)); + assert_eq!(format_from_path("file.rdf"), Some(GraphFormat::RdfXml)); + assert_eq!(format_from_path("file.unknown"), None); + assert_eq!(format_from_path("file"), None); + } + + #[test] + fn test_main() -> Result<(), Box> { + let dir = tempdir()?; + let input_file = dir.path().join("input.ttl"); + + let mut input = File::create(&input_file)?; + writeln!( + input, + " \"object\" ." + )?; + + let mut cmd = Command::cargo_bin("rdfpipe-rs").unwrap(); + let status = cmd + .arg("-i") + .arg("turtle") + .arg("-o") + .arg("rdf-xml") + .arg(&input_file) + .assert(); + + status.success(); + + Ok(()) + } +}