summaryrefslogtreecommitdiff
path: root/tests/cli.rs
diff options
context:
space:
mode:
authorJustin Wernick <justin@worthe-it.co.za>2023-03-01 13:57:18 +0200
committerJustin Wernick <justin@worthe-it.co.za>2023-03-01 13:57:18 +0200
commit10564554e6c6be7de32d6af4242fe1d2fbb90255 (patch)
treebf41154d8015d25c76c4090a61f9a88abe8d7847 /tests/cli.rs
parent7e9c3df2e9dc48838e4788ab2d28a5ad022517ec (diff)
Isolate test runs from each other
Diffstat (limited to 'tests/cli.rs')
-rw-r--r--tests/cli.rs68
1 files changed, 39 insertions, 29 deletions
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<TestContext> {
+ let workdir = tempfile::tempdir()?;
-fn spawn_interactive_process() -> Result<PtySession> {
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("");