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
//! Snapshot test for `mkit help` — every documented subcommand in
//! `docs/CLI.md` must appear in the help text.
//!
//! These tests also assert man-page and shell-completion coverage of
//! the documented subcommand list, so a new command can't silently ship
//! without being added to `man/mkit.1` and `completions/mkit.{bash,zsh,
//! fish}` (the drift that #219 fixed for `pack-shard`).
use std::path::PathBuf;
use std::process::Command;
fn mkit_bin() -> &'static str {
env!("CARGO_BIN_EXE_mkit")
}
/// Repository root, three levels up from this crate
/// (`rust/crates/mkit-cli`).
fn repo_root() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("..")
.join("..")
.join("..")
}
fn read_repo_file(rel: &str) -> String {
let path = repo_root().join(rel);
std::fs::read_to_string(&path)
.unwrap_or_else(|e| panic!("read {} ({}): {e}", rel, path.display()))
}
/// True iff `needle` occurs in `haystack` as a whole token: the
/// characters immediately before and after the match (if any) are not
/// alphanumeric/hyphen. Plain `.contains()` would let a short command
/// name like `"rm"` match inside an unrelated word (e.g. "perform"),
/// so this pins word-boundary coverage instead.
fn contains_word(haystack: &str, needle: &str) -> bool {
fn is_word_char(c: char) -> bool {
c.is_ascii_alphanumeric() || c == '-'
}
haystack.match_indices(needle).any(|(idx, m)| {
let before_ok = haystack[..idx]
.chars()
.next_back()
.is_none_or(|c| !is_word_char(c));
let after_ok = haystack[idx + m.len()..]
.chars()
.next()
.is_none_or(|c| !is_word_char(c));
before_ok && after_ok
})
}
/// The canonical subcommand list per `docs/CLI.md`. Keep in sync with
/// the CLI reference when adding commands.
const DOCUMENTED_SUBCOMMANDS: &[&str] = &[
"init",
"add",
"rm",
"mv",
"restore",
"reset",
"status",
"diff",
"stash",
"sparse-checkout",
"pack-shard",
"commit",
"log",
"reflog",
"blame",
"verify",
"cat",
"cat-file",
"hash",
"tree",
"ls-tree",
"ls-files",
"rev-parse",
"show",
"show-ref",
"for-each-ref",
"symbolic-ref",
"update-ref",
"ref",
"branch",
"checkout",
"clean",
"tag",
"merge",
"cherry-pick",
"revert",
"rebase",
"bisect",
"gc",
"remote",
"clone",
"fetch",
"pull",
"push",
"serve",
"mcp",
"key",
"keygen",
"config",
"self",
"version",
"worktree",
];
#[test]
fn help_lists_every_documented_subcommand() {
let output = Command::new(mkit_bin())
.arg("help")
.output()
.expect("spawn `mkit help`");
assert!(output.status.success(), "`mkit help` must exit 0");
let text = String::from_utf8(output.stdout).expect("stdout is utf-8");
for cmd in DOCUMENTED_SUBCOMMANDS {
assert!(
contains_word(&text, cmd),
"`mkit help` output is missing documented subcommand '{cmd}'"
);
}
}
#[test]
fn man_page_documents_every_subcommand() {
let man = read_repo_file("man/mkit.1");
for cmd in DOCUMENTED_SUBCOMMANDS {
// The man page enumerates subcommands as mdoc `Cm <name>`
// macros (either `.It Cm <name>` for the leading entry or
// `Cm <name>` after a comma for grouped entries like
// `.Cm pull , Cm fetch`). Match the macro plus a delimiter so a
// prefix can't satisfy a longer command name by accident.
let documented = man.lines().any(|line| {
line.match_indices(&format!("Cm {cmd}")).any(|(idx, m)| {
let after = &line[idx + m.len()..];
after.is_empty() || after.starts_with([' ', ',', '\t'])
})
});
assert!(
documented,
"man/mkit.1 is missing documented subcommand '{cmd}' (expected a `Cm {cmd}` macro)"
);
}
}
#[test]
fn completions_cover_every_subcommand() {
for (file, name) in [
("completions/mkit.bash", "bash"),
("completions/mkit.zsh", "zsh"),
("completions/mkit.fish", "fish"),
] {
let text = read_repo_file(file);
for cmd in DOCUMENTED_SUBCOMMANDS {
assert!(
contains_word(&text, cmd),
"{name} completion ({file}) is missing documented subcommand '{cmd}'"
);
}
}
}
#[test]
fn dash_dash_help_goes_to_stdout() {
let output = Command::new(mkit_bin())
.arg("--help")
.output()
.expect("spawn `mkit --help`");
assert!(output.status.success(), "`mkit --help` must exit 0");
assert!(!output.stdout.is_empty(), "stdout empty");
assert!(output.stderr.is_empty(), "stderr should be empty on --help");
}
// Unknown-subcommand exit code + exact error text: see
// tests/cmd/unknown-subcommand.trycmd (a strict superset of what a
// `unknown_subcommand_exits_usage` test asserting only the exit code
// used to check here).
/// Snapshot of `mkit --help` output. Reviewable diffs via
/// `cargo insta review`; raw assertions on a 30+ subcommand list
/// produce noisy diffs that nobody reads.
#[test]
fn help_output_snapshot() {
let output = Command::new(mkit_bin())
.arg("--help")
.output()
.expect("spawn `mkit --help`");
let stdout = String::from_utf8(output.stdout).expect("utf-8");
insta::assert_snapshot!("mkit_dash_help", stdout);
}
/// Snapshot of `mkit version`. Pins the format (key=value pairs,
/// trailing newline) so any drift in the version-emitter shows up as
/// a reviewable diff instead of a `contains("0.")` regex.
#[test]
fn version_output_snapshot() {
let output = Command::new(mkit_bin())
.arg("version")
.output()
.expect("spawn `mkit version`");
let stdout = String::from_utf8(output.stdout).expect("utf-8");
// Mask the version string so a `Cargo.toml` version bump doesn't
// re-break the snapshot — we want shape-stability, not number-
// stability.
insta::with_settings!({filters => vec![
(r"\d+\.\d+\.\d+(-[A-Za-z0-9._-]+)?", "[VERSION]"),
]}, {
insta::assert_snapshot!("mkit_version", stdout);
});
}