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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
//! Doctor command tests
//!
//! Tests verify:
//! - DoctorArgs struct defaults
//! - Tool detection (finds cargo on dev machine)
//! - JSON output structure
//! - Install mode error handling
//! - Text output formatting
use assert_cmd::prelude::*;
use predicates::prelude::*;
use std::process::Command;
/// Get the path to the test binary
fn tldr_cmd() -> Command {
Command::new(assert_cmd::cargo::cargo_bin!("tldr"))
}
// =============================================================================
// DoctorArgs Struct Tests
// =============================================================================
#[test]
fn test_doctor_args_default() {
// Running `tldr doctor` without --install should run check mode (no install arg)
let mut cmd = tldr_cmd();
cmd.args(["doctor", "--help"])
.assert()
.success()
.stdout(predicate::str::contains("--install"))
.stdout(predicate::str::contains("diagnostic tools"));
}
// =============================================================================
// Check Mode Tests
// =============================================================================
#[test]
fn test_doctor_check_finds_cargo() {
// cargo should be found on any dev machine running these tests
let mut cmd = tldr_cmd();
let output = cmd.args(["doctor", "-f", "json", "-q"]).output().unwrap();
assert!(output.status.success(), "doctor command should succeed");
let json: serde_json::Value =
serde_json::from_slice(&output.stdout).expect("doctor output should be valid JSON");
// Rust entry should exist and have type_checker info
let rust = json.get("rust").expect("should have rust entry");
let type_checker = rust.get("type_checker").expect("should have type_checker");
assert_eq!(
type_checker.get("name").and_then(|v| v.as_str()),
Some("cargo"),
"rust type_checker should be cargo"
);
assert_eq!(
type_checker.get("installed").and_then(|v| v.as_bool()),
Some(true),
"cargo should be installed on dev machine"
);
assert!(
type_checker.get("path").and_then(|v| v.as_str()).is_some(),
"cargo path should be present"
);
}
#[test]
fn test_doctor_json_structure() {
// Verify JSON output has correct shape for all supported languages
let mut cmd = tldr_cmd();
let output = cmd.args(["doctor", "-f", "json", "-q"]).output().unwrap();
assert!(output.status.success());
let json: serde_json::Value =
serde_json::from_slice(&output.stdout).expect("doctor output should be valid JSON");
// Check that it's an object
assert!(json.is_object(), "output should be a JSON object");
// Verify expected languages are present
let expected_langs = [
"python",
"typescript",
"javascript",
"go",
"rust",
"java",
"c",
"cpp",
"ruby",
"php",
"kotlin",
"swift",
"csharp",
"scala",
"elixir",
"lua",
];
for lang in expected_langs {
let lang_entry = json
.get(lang)
.unwrap_or_else(|| panic!("should have {} entry", lang));
// Each language should have type_checker and linter keys
assert!(
lang_entry.get("type_checker").is_some(),
"{} should have type_checker key",
lang
);
assert!(
lang_entry.get("linter").is_some(),
"{} should have linter key",
lang
);
// If type_checker is not null, it should have required fields
if let Some(tc) = lang_entry.get("type_checker") {
if !tc.is_null() {
assert!(
tc.get("name").is_some(),
"{} type_checker should have name",
lang
);
assert!(
tc.get("installed").is_some(),
"{} type_checker should have installed",
lang
);
assert!(
tc.get("path").is_some(),
"{} type_checker should have path",
lang
);
assert!(
tc.get("install").is_some(),
"{} type_checker should have install",
lang
);
}
}
// If linter is not null, it should have required fields
if let Some(linter) = lang_entry.get("linter") {
if !linter.is_null() {
assert!(
linter.get("name").is_some(),
"{} linter should have name",
lang
);
assert!(
linter.get("installed").is_some(),
"{} linter should have installed",
lang
);
assert!(
linter.get("path").is_some(),
"{} linter should have path",
lang
);
assert!(
linter.get("install").is_some(),
"{} linter should have install",
lang
);
}
}
}
}
// =============================================================================
// Install Mode Tests
// =============================================================================
#[test]
fn test_doctor_install_invalid_lang() {
// Trying to install for unknown language should error
let mut cmd = tldr_cmd();
cmd.args(["doctor", "--install", "cobol", "-q"])
.assert()
.failure()
.stderr(predicate::str::contains("cobol").or(predicate::str::contains("unknown")));
}
#[test]
fn test_doctor_install_help_shows_option() {
let mut cmd = tldr_cmd();
cmd.args(["doctor", "--help"])
.assert()
.success()
.stdout(predicate::str::contains("install"));
}
// =============================================================================
// Text Output Format Tests
// =============================================================================
#[test]
fn test_doctor_text_output_format() {
// Text output should have headers and status markers
let mut cmd = tldr_cmd();
cmd.args(["doctor", "-f", "text"])
.assert()
.success()
// Should have title/header
.stdout(predicate::str::contains("Diagnostics").or(predicate::str::contains("Doctor")))
// Should show some language sections
.stdout(predicate::str::contains("Python").or(predicate::str::contains("python")))
.stdout(predicate::str::contains("Rust").or(predicate::str::contains("rust")))
// Should have status indicators
.stdout(
predicate::str::contains("[OK]")
.or(predicate::str::contains("[X]"))
.or(predicate::str::contains("installed")),
);
}
#[test]
fn test_doctor_default_format_produces_output() {
// Running without -f flag should produce output (JSON is the default for all commands)
let mut cmd = tldr_cmd();
let output = cmd.args(["doctor"]).output().unwrap();
assert!(output.status.success());
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(!stdout.is_empty(), "doctor should produce output");
}