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
#![allow(
    clippy::unwrap_used,
    clippy::panic,
    clippy::indexing_slicing,
    clippy::arithmetic_side_effects
)]

mod support;

use std::ffi::OsStr;
use std::os::unix::ffi::OsStrExt as _;

use support::Sandbox;

#[test]
fn an_unknown_command_prints_the_command_list() {
    let s = Sandbox::new();
    for args in [&[][..], &["nope"][..]] {
        let out = s.run_in(&s.repo, args);
        assert!(!out.status.success());
        assert_eq!(out.stdout, b"");
        assert_eq!(String::from_utf8(out.stderr).unwrap(), jeans::USAGE);
    }
}

#[test]
fn commands_name_their_missing_arguments() {
    let s = Sandbox::new();
    assert_eq!(s.jeans_fails(["show"]), "jeans: show needs TASK");
    assert_eq!(
        s.jeans_fails(["init", "--goal", "x"]),
        "jeans: init needs TITLE"
    );
    assert_eq!(
        s.jeans_fails(["describe", &s.goal]),
        "jeans: describe needs -m MESSAGE"
    );
    assert_eq!(
        s.jeans_fails(["tree", "--depth"]),
        "jeans: missing argument: --depth"
    );
}

#[test]
fn arguments_must_be_utf8() {
    let s = Sandbox::new();
    let out = s.run_in(&s.repo, [OsStr::new("show"), OsStr::from_bytes(b"\xff")]);
    assert!(!out.status.success());
    assert!(
        String::from_utf8(out.stderr)
            .unwrap()
            .contains("isn't UTF-8")
    );
}

#[test]
fn guide_prints_the_embedded_guide() {
    let s = Sandbox::new();
    for name in ["stakeholder", "developer"] {
        let out = s.run_in(&s.repo, ["guide", name]);
        assert!(out.status.success());
        assert_eq!(
            String::from_utf8(out.stdout).unwrap(),
            jeans::guide(name).unwrap()
        );
    }
}

#[test]
fn guide_rejects_an_unknown_name() {
    let s = Sandbox::new();
    for args in [&["guide"][..], &["guide", "nobody"][..]] {
        assert_eq!(
            s.jeans_fails(args),
            "jeans: guide needs stakeholder or developer"
        );
    }
}