From 10564554e6c6be7de32d6af4242fe1d2fbb90255 Mon Sep 17 00:00:00 2001 From: Justin Wernick Date: Wed, 1 Mar 2023 13:57:18 +0200 Subject: Isolate test runs from each other --- Cargo.lock | 151 +++++++++++++++++++++++++++++++++++++++++++++++++++++------ Cargo.toml | 1 + readme.org | 3 +- tests/cli.rs | 68 +++++++++++++++------------ 4 files changed, 178 insertions(+), 45 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0a5c417..76e0078 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -94,6 +94,27 @@ version = "1.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91" +[[package]] +name = "errno" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f639046355ee4f37944e44f60642c6f3a7efa3cf6b78c78a0d989a8ce6c396a1" +dependencies = [ + "errno-dragonfly", + "libc", + "winapi", +] + +[[package]] +name = "errno-dragonfly" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf" +dependencies = [ + "cc", + "libc", +] + [[package]] name = "fastrand" version = "1.9.0" @@ -146,6 +167,16 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "io-lifetimes" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1abeb7a0dd0f8181267ff8adc397075586500b81b28a73e8a0208b00fc170fb3" +dependencies = [ + "libc", + "windows-sys 0.45.0", +] + [[package]] name = "itertools" version = "0.10.5" @@ -210,6 +241,12 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "linux-raw-sys" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f051f77a7c8e6957c0696eac88f26b0117e54f52d3fc682ab19397a8812846a4" + [[package]] name = "log" version = "0.4.17" @@ -384,15 +421,6 @@ version = "0.6.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848" -[[package]] -name = "remove_dir_all" -version = "0.5.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7" -dependencies = [ - "winapi", -] - [[package]] name = "rexpect" version = "0.5.0" @@ -406,6 +434,20 @@ dependencies = [ "thiserror", ] +[[package]] +name = "rustix" +version = "0.36.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f43abb88211988493c1abb44a70efa56ff0ce98f233b7b276146f1f3f7ba9644" +dependencies = [ + "bitflags", + "errno", + "io-lifetimes", + "libc", + "linux-raw-sys", + "windows-sys 0.45.0", +] + [[package]] name = "serde" version = "1.0.152" @@ -421,6 +463,7 @@ dependencies = [ "git2", "nom", "rexpect", + "tempfile", "thiserror", ] @@ -437,16 +480,15 @@ dependencies = [ [[package]] name = "tempfile" -version = "3.3.0" +version = "3.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5cdb1ef4eaeeaddc8fbd371e5017057064af0911902ef36b39801f67cc6d79e4" +checksum = "af18f7ae1acd354b992402e9ec5864359d693cd8a79dcbef59f76891701c1e95" dependencies = [ "cfg-if", "fastrand", - "libc", "redox_syscall", - "remove_dir_all", - "winapi", + "rustix", + "windows-sys 0.42.0", ] [[package]] @@ -558,3 +600,84 @@ name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-sys" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows-sys" +version = "0.45.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e2522491fbfcd58cc84d47aeb2958948c4b8982e9a2d8a2a35bbaed431390e7" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c9864e83243fdec7fc9c5444389dcbbfd258f745e7853198f365e3c4968a608" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c8b1b673ffc16c47a9ff48570a9d85e25d265735c503681332589af6253c6c7" + +[[package]] +name = "windows_i686_gnu" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de3887528ad530ba7bdbb1faa8275ec7a1155a45ffa57c37993960277145d640" + +[[package]] +name = "windows_i686_msvc" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf4d1122317eddd6ff351aa852118a2418ad4214e6613a50e0191f7004372605" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1040f221285e17ebccbc2591ffdc2d44ee1f9186324dd3e84e99ac68d699c45" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "628bfdf232daa22b0d64fdb62b09fcc36bb01f05a3939e20ab73aaf9470d0463" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "447660ad36a13288b1db4d4248e857b510e8c3a225c822ba4fb748c0aafecffd" diff --git a/Cargo.toml b/Cargo.toml index 98d243e..1f70f9a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,3 +14,4 @@ thiserror = "1.0.38" [dev-dependencies] anyhow = "1.0.69" assert_cmd = "2.0.8" +tempfile = "3.4.0" diff --git a/readme.org b/readme.org index 1a36ac8..9288d44 100644 --- a/readme.org +++ b/readme.org @@ -17,8 +17,7 @@ Pijul. - [X] exit command - [X] git init of private repo - [X] responds to unknown commands -- [ ] Isolation of workdir between tests - - use TempDir and rexpect::session::spawn_command instead of spawn +- [X] Isolation of workdir between tests - [ ] git fetch - git receive-pack - [ ] git push diff --git a/tests/cli.rs b/tests/cli.rs index 8b8aa82..01e8f2b 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -1,12 +1,22 @@ use anyhow::Result; use assert_cmd::{cargo::cargo_bin, Command}; -use rexpect::{session::PtySession, spawn}; +use rexpect::session::{spawn_command, PtySession}; +use tempfile::TempDir; + +struct TestContext { + p: PtySession, + workdir: TempDir, +} + +fn spawn_interactive_process() -> Result { + let workdir = tempfile::tempdir()?; -fn spawn_interactive_process() -> Result { let path = cargo_bin(env!("CARGO_PKG_NAME")); - let mut process = spawn(&path.display().to_string(), Some(3000))?; - expect_prompt(&mut process)?; - Ok(process) + let mut command = std::process::Command::new(&path); + command.current_dir(&workdir); + let mut p = spawn_command(command, Some(3000))?; + expect_prompt(&mut p)?; + Ok(TestContext { p, workdir }) } fn expect_prompt(p: &mut PtySession) -> Result<()> { @@ -22,59 +32,59 @@ fn shows_a_prompt() -> Result<()> { #[test] fn does_nothing_after_receiving_whitespace_input() -> Result<()> { - let mut p = spawn_interactive_process()?; - p.send_line("")?; - expect_prompt(&mut p)?; - p.send_line(" ")?; - expect_prompt(&mut p)?; + let mut c = spawn_interactive_process()?; + c.p.send_line("")?; + expect_prompt(&mut c.p)?; + c.p.send_line(" ")?; + expect_prompt(&mut c.p)?; Ok(()) } #[test] fn quits_when_eof_is_sent() -> Result<()> { - let mut p = spawn_interactive_process()?; - p.send_control('d')?; - p.exp_eof()?; + let mut c = spawn_interactive_process()?; + c.p.send_control('d')?; + c.p.exp_eof()?; Ok(()) } #[test] fn quits_when_exit_command_is_sent() -> Result<()> { - let mut p = spawn_interactive_process()?; - p.send_line("exit")?; - p.exp_eof()?; + let mut c = spawn_interactive_process()?; + c.p.send_line("exit")?; + c.p.exp_eof()?; Ok(()) } #[test] fn reports_error_with_unsupported_shell_commands() -> Result<()> { - let mut p = spawn_interactive_process()?; - p.send_line("ls")?; - p.exp_string("Unknown input \"ls\"")?; - expect_prompt(&mut p)?; + let mut c = spawn_interactive_process()?; + c.p.send_line("ls")?; + c.p.exp_string("Unknown input \"ls\"")?; + expect_prompt(&mut c.p)?; Ok(()) } #[test] fn reports_error_with_nonsense_input() -> Result<()> { - let mut p = spawn_interactive_process()?; - p.send_line(" asd fg ")?; - p.exp_string("Unknown input \"asd fg\"")?; - expect_prompt(&mut p)?; + let mut c = spawn_interactive_process()?; + c.p.send_line(" asd fg ")?; + c.p.exp_string("Unknown input \"asd fg\"")?; + expect_prompt(&mut c.p)?; Ok(()) } #[test] fn can_init_a_new_git_repo() -> Result<()> { - let mut p = spawn_interactive_process()?; - p.send_line("git-init my-new-repo")?; - p.exp_string("Successfully created my-new-repo.git")?; - expect_prompt(&mut p)?; + let mut c = spawn_interactive_process()?; + c.p.send_line("git-init my-new-repo")?; + c.p.exp_string("Successfully created my-new-repo.git")?; + expect_prompt(&mut c.p)?; Command::new("git") .arg("rev-list") .arg("--all") - .current_dir("git/my-new-repo.git") + .current_dir(c.workdir.as_ref().join("git").join("my-new-repo.git")) .assert() .success() .stdout(""); -- cgit v1.2.3