-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathdoctor.bats
More file actions
325 lines (243 loc) · 8.71 KB
/
Copy pathdoctor.bats
File metadata and controls
325 lines (243 loc) · 8.71 KB
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#!/usr/bin/env bats
#
# Tests for `asimov doctor` — the diagnostic subcommand.
#
# Doctor exists mainly for people arriving from v0.3.0 (issue #122), where the
# failure modes are leftovers: a shadowed binary, an orphaned LaunchAgent, and a
# root-owned cache. It is read-only: it reports and prints commands, never runs
# them, and never executes another asimov binary it finds.
load test_helper
# The shared setup() from test_helper applies. With ASIMOV_TEST_LAUNCHCTL_LOADED
# unset, the mock launchctl reports nothing as loaded — the right baseline.
require_non_root() {
if [[ "${EUID:-$(id -u)}" -eq 0 ]]; then
skip "chmod-based tests are meaningless as root"
fi
}
# =============================================================================
# Plumbing
# =============================================================================
@test "doctor runs and exits 0 on a clean system" {
run_asimov doctor
[[ "$status" -eq 0 ]]
}
@test "doctor is listed in the help output" {
run_asimov --help
[[ "$status" -eq 0 ]]
[[ "$output" == *"asimov doctor"* ]]
}
@test "doctor reports the running version" {
# Derived from the script, never hardcoded: a literal here breaks on every
# release, which is exactly what it did on the bump to 0.11.0.
run_asimov --version
local version="$output"
[[ -n "$version" ]]
run_asimov doctor
[[ "$output" == *"$version"* ]]
}
@test "doctor never modifies Time Machine exclusions" {
create_project "Code/My-Project" "package.json" "node_modules"
run_asimov doctor
[[ "$(count_exclusions)" -eq 0 ]]
refute_excluded "${HOME}/Code/My-Project/node_modules"
}
@test "doctor does not create a cache" {
run_asimov doctor
[[ ! -e "${HOME}/.cache/asimov/paths" ]]
}
@test "doctor --quiet prints nothing when everything is healthy" {
run_asimov doctor --quiet
[[ "$status" -eq 0 ]]
[[ -z "$output" ]]
}
# =============================================================================
# Install / shadowed binaries
# =============================================================================
@test "doctor flags an older asimov shadowing the one that is running" {
shadow_asimov_binary "0.3.0" >/dev/null
run_asimov doctor
[[ "$status" -eq 1 ]]
[[ "$output" == *"shadow"* ]]
[[ "$output" == *"0.3.0"* ]]
}
@test "doctor reads the shadowing binary's version without executing it" {
shadow_asimov_binary "0.3.0" >/dev/null
run_asimov doctor
# The stub screams if it is ever run. v0.3.0 parses no arguments at all, so
# executing it to ask its version would start a real scan instead.
[[ "$output" != *"STUB WAS EXECUTED"* ]]
}
@test "doctor is clean when no other asimov is on PATH" {
run_asimov doctor
[[ "$output" != *"shadow"* ]]
}
# =============================================================================
# Schedule
# =============================================================================
@test "doctor reports a loaded schedule as healthy" {
write_launch_agent "com.stevegrunwell.asimov" "$(command -v bash)"
set_loaded_agents "com.stevegrunwell.asimov"
run_asimov doctor
[[ "$status" -eq 0 ]]
[[ "$output" == *"com.stevegrunwell.asimov"* ]]
[[ "$output" == *"loaded"* ]]
}
@test "doctor flags a plist whose program no longer exists" {
write_launch_agent "homebrew.mxcl.asimov" "/opt/homebrew/Cellar/asimov/0.3.0/bin/asimov"
set_loaded_agents "homebrew.mxcl.asimov"
run_asimov doctor
[[ "$status" -eq 1 ]]
[[ "$output" == *"homebrew.mxcl.asimov"* ]]
[[ "$output" == *"/opt/homebrew/Cellar/asimov/0.3.0/bin/asimov"* ]]
[[ "$output" == *"no longer exists"* ]]
}
@test "doctor flags two schedules running at once" {
write_launch_agent "com.stevegrunwell.asimov" "$(command -v bash)"
write_launch_agent "com.django23.asimov" "$(command -v bash)"
set_loaded_agents "com.stevegrunwell.asimov com.django23.asimov"
run_asimov doctor
[[ "$status" -eq 1 ]]
[[ "$output" == *"two schedules"* || "$output" == *"more than one"* ]]
}
@test "doctor prints the bootout command for an orphaned agent" {
write_launch_agent "homebrew.mxcl.asimov" "/gone/asimov"
set_loaded_agents "homebrew.mxcl.asimov"
run_asimov doctor
[[ "$output" == *"launchctl bootout"* ]]
[[ "$output" == *"homebrew.mxcl.asimov"* ]]
}
@test "doctor says so when nothing is scheduled" {
run_asimov doctor
[[ "$status" -eq 0 ]]
[[ "$output" == *"not scheduled"* ]]
}
@test "doctor treats an unloaded legacy agent as a leftover to remove" {
write_launch_agent "com.django23.asimov" "$(command -v bash)"
set_loaded_agents
run_asimov doctor
[[ "$status" -eq 1 ]]
[[ "$output" == *"leftover"* ]]
[[ "$output" == *"rm ${HOME}/Library/LaunchAgents/com.django23.asimov.plist"* ]]
# Never advise loading a label we no longer ship.
[[ "$output" != *"bootstrap"* ]]
}
@test "doctor offers to load an unloaded current agent" {
write_launch_agent "com.stevegrunwell.asimov" "$(command -v bash)"
set_loaded_agents
run_asimov doctor
[[ "$output" == *"bootstrap"* ]]
}
@test "doctor flags a plist that is installed but not loaded" {
write_launch_agent "com.stevegrunwell.asimov" "$(command -v bash)"
set_loaded_agents
run_asimov doctor
[[ "$output" == *"not loaded"* ]]
}
# =============================================================================
# Cache
# =============================================================================
@test "doctor flags an unreadable cache file and prints the reset command" {
require_non_root
mkdir -p "${HOME}/.cache/asimov"
echo "/x" > "${HOME}/.cache/asimov/excluded"
chmod 000 "${HOME}/.cache/asimov/excluded"
run_asimov doctor
chmod 644 "${HOME}/.cache/asimov/excluded"
[[ "$status" -eq 1 ]]
[[ "$output" == *"excluded"* ]]
[[ "$output" == *"rm -rf ${HOME}/.cache/asimov"* ]]
}
@test "doctor does not blame root for a cache file you own" {
require_non_root
mkdir -p "${HOME}/.cache/asimov"
echo "/x" > "${HOME}/.cache/asimov/excluded"
chmod 000 "${HOME}/.cache/asimov/excluded"
run_asimov doctor
chmod 644 "${HOME}/.cache/asimov/excluded"
[[ "$status" -eq 1 ]]
[[ "$output" == *"rm -rf ${HOME}/.cache/asimov"* ]]
# The file is owned by the current user; root never touched it.
[[ "$output" != *"as root"* ]]
}
@test "doctor flags an unwritable cache directory" {
require_non_root
mkdir -p "${HOME}/.cache/asimov"
chmod 500 "${HOME}/.cache/asimov"
run_asimov doctor
chmod 700 "${HOME}/.cache/asimov"
[[ "$status" -eq 1 ]]
[[ "$output" == *"not writable"* ]]
}
@test "doctor reports a healthy cache with its entry count" {
write_path_cache "${HOME}/a" "${HOME}/b"
run_asimov doctor
[[ "$status" -eq 0 ]]
[[ "$output" == *"2"* ]]
}
@test "doctor says when the last scan ran" {
write_path_cache "${HOME}/a"
run_asimov doctor
[[ "$output" == *"last scan"* ]]
}
@test "doctor reports no cache yet as healthy, not a problem" {
run_asimov doctor
[[ "$status" -eq 0 ]]
[[ "$output" == *"no cache yet"* ]]
}
# =============================================================================
# Config
# =============================================================================
@test "doctor reports a valid config file" {
write_config "[fixed_dirs]
enabled = true"
run_asimov doctor
[[ "$status" -eq 0 ]]
[[ "$output" == *".config/asimov/config"* ]]
}
@test "doctor flags an unknown config key" {
write_config "[fixed_dirs]
enbaled = true"
run_asimov doctor
[[ "$status" -eq 1 ]]
[[ "$output" == *"enbaled"* ]]
}
@test "doctor flags an unknown config section" {
write_config "[fixed_dris]
enabled = true"
run_asimov doctor
[[ "$status" -eq 1 ]]
[[ "$output" == *"fixed_dris"* ]]
}
@test "doctor reports the default when there is no config file" {
run_asimov doctor
[[ "$status" -eq 0 ]]
[[ "$output" == *"defaults"* ]]
}
# =============================================================================
# Time Machine
# =============================================================================
@test "doctor reports tmutil as reachable" {
run_asimov doctor
[[ "$status" -eq 0 ]]
[[ "$output" == *"Time Machine"* ]]
}
@test "doctor flags tmutil being unable to read exclusions" {
ASIMOV_TEST_TMUTIL_ISEXCLUDED_FAIL=1
export ASIMOV_TEST_TMUTIL_ISEXCLUDED_FAIL
run_asimov doctor
[[ "$status" -eq 1 ]]
[[ "$output" == *"Full Disk Access"* ]]
}
# =============================================================================
# Summary
# =============================================================================
@test "doctor summarises the problem count" {
write_launch_agent "homebrew.mxcl.asimov" "/gone/asimov"
set_loaded_agents "homebrew.mxcl.asimov"
run_asimov doctor
[[ "$output" == *"1 problem"* ]]
}
@test "doctor says everything checks out when healthy" {
run_asimov doctor
[[ "$output" == *"No problems found"* ]]
}