summaryrefslogtreecommitdiff
path: root/tests/cli.rs
blob: a4b36a9b18a76da6a552c8d105160c3089be0b80 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
use anyhow::Result;
use assert_cmd::{cargo::cargo_bin, Command};
use rexpect::session::{spawn_command, PtySession};
use std::path::Path;
use tempfile::TempDir;
use user_info::{get_user_groups, get_username};

struct TestContext {
    p: PtySession,
    workdir: TempDir,
}

fn spawn_interactive_process() -> Result<TestContext> {
    let workdir = tempfile::tempdir()?;

    let path = cargo_bin(env!("CARGO_PKG_NAME"));
    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 run_batch_command(batch_command: &str) -> Result<TestContext> {
    let workdir = tempfile::tempdir()?;

    let path = cargo_bin(env!("CARGO_PKG_NAME"));
    let mut command = std::process::Command::new(&path);
    command.current_dir(&workdir);
    command.args(["-c", batch_command]);
    let p = spawn_command(command, Some(3000))?;

    Ok(TestContext { p, workdir })
}

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 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 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 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 c = spawn_interactive_process()?;
    c.p.send_line("ls")?;
    c.p.exp_string("error: unrecognized subcommand 'ls'")?;
    expect_prompt(&mut c.p)?;
    Ok(())
}

#[test]
fn reports_error_with_nonsense_input() -> Result<()> {
    let mut c = spawn_interactive_process()?;
    c.p.send_line(" asd fg  ")?;
    c.p.exp_string("error: unrecognized subcommand 'asd'")?;
    expect_prompt(&mut c.p)?;
    Ok(())
}

fn verify_repo_exists(repo_dir: &Path) {
    Command::new("git")
        .arg("rev-list")
        .arg("--all")
        .current_dir(repo_dir)
        .assert()
        .success()
        .stdout("");
}

fn verify_repo_config_value(repo_dir: &Path, config_key: &str, config_value: Option<&str>) {
    let assert = Command::new("git")
        .args(["config", "--local", config_key])
        .current_dir(repo_dir)
        .assert();
    match config_value {
        Some(value) => {
            assert.success().stdout(format!("{}\n", value));
        }
        None => {
            assert.failure().code(1);
        }
    }
}

#[test]
fn can_init_a_new_git_repo() -> Result<()> {
    let mut c = spawn_interactive_process()?;
    let username = get_username().unwrap();
    let repo_name = "my-new-repo";
    c.p.send_line(&format!("git-init {}", repo_name))?;
    c.p.exp_string(&format!(
        "Successfully created \"git/{}/{}.git\"",
        username, repo_name
    ))?;
    expect_prompt(&mut c.p)?;

    let repo_dir = c
        .workdir
        .as_ref()
        .join("git")
        .join(username)
        .join(&format!("{}.git", repo_name));
    verify_repo_exists(&repo_dir);
    verify_repo_config_value(&repo_dir, "core.sharedrepository", None);

    Ok(())
}

#[test]
fn can_init_a_new_shared_git_repo() -> Result<()> {
    let mut c = spawn_interactive_process()?;
    let group = get_user_groups().pop().unwrap();
    let repo_name = "my-new-shared-repo";
    c.p.send_line(&format!("git-init --group {} {}", group, repo_name))?;
    c.p.exp_string(&format!(
        "Successfully created \"git/{}/{}.git\"",
        group, repo_name
    ))?;
    expect_prompt(&mut c.p)?;

    let repo_dir = c
        .workdir
        .as_ref()
        .join("git")
        .join(&group)
        .join(&format!("{}.git", repo_name));
    verify_repo_exists(&repo_dir);
    verify_repo_config_value(&repo_dir, "core.sharedrepository", Some("1"));

    Ok(())
}

#[test]
fn runs_a_single_command_and_exit_with_cli_flag() -> Result<()> {
    let username = get_username().unwrap();
    let repo_name = "another-new-repo";
    let mut c = run_batch_command(&format!("git-init {}", repo_name))?;
    c.p.exp_string(&format!(
        "Successfully created \"git/{}/{}.git\"",
        username, repo_name
    ))?;
    c.p.exp_eof()?;
    Ok(())
}

#[test]
fn allows_quotes_arguments() -> Result<()> {
    let username = get_username().unwrap();
    let mut c = spawn_interactive_process()?;
    c.p.send_line("\"git-init\" 'another-new-repo'")?;
    c.p.exp_string(&format!(
        "Successfully created \"git/{}/another-new-repo.git\"",
        username
    ))?;
    Ok(())
}

#[test]
fn errors_with_an_open_double_quote() -> Result<()> {
    let mut c = spawn_interactive_process()?;
    c.p.send_line("\"git-init 'another-new-repo'")?;
    c.p.exp_string("Incomplete input")?;
    Ok(())
}

#[test]
fn errors_with_an_open_single_quote() -> Result<()> {
    let mut c = spawn_interactive_process()?;
    c.p.send_line("'git-init 'another-new-repo'")?;
    c.p.exp_string("Incomplete input")?;
    Ok(())
}

#[test]
fn allows_single_quotes_and_spaces_inside_double_quotes() -> Result<()> {
    let username = get_username().unwrap();
    let mut c = spawn_interactive_process()?;
    c.p.send_line("git-init \"shukkie's new repo\"")?;
    c.p.exp_string(&format!(
        "Successfully created \"git/{}/shukkie's new repo.git\"",
        username
    ))?;
    Ok(())
}