summaryrefslogtreecommitdiff
path: root/tests/cli.rs
blob: fcf38e8ad2f47ebcb0f82c8aaa72bc6f617d0b0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use anyhow::Result;
use assert_cmd::cargo::cargo_bin;
use rexpect::{session::PtySession, spawn};

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)
}

fn expect_prompt(p: &mut PtySession) -> Result<()> {
    p.exp_string("> ")?;
    Ok(())
}

#[test]
fn shows_a_prompt() -> Result<()> {
    spawn_interactive_process()?;
    Ok(())
}

#[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)?;
    Ok(())
}

#[test]
fn quits_when_eof_is_sent() -> Result<()> {
    let mut p = spawn_interactive_process()?;
    p.send_control('d')?;
    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()?;
    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)?;
    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)?;
    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)?;
    // TODO: assert that the repo is actually there?
    Ok(())
}