This repository was archived by the owner on Oct 26, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathcli.bats
More file actions
729 lines (616 loc) · 20.7 KB
/
Copy pathcli.bats
File metadata and controls
729 lines (616 loc) · 20.7 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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
#!/usr/bin/env bats
setup_file() {
PROJECT_ROOT="$(cd "${BATS_TEST_DIRNAME}/.." && pwd)"
export PROJECT_ROOT
ORIGINAL_HOME="${HOME:-}"
export ORIGINAL_HOME
# Capture real GOCACHE before HOME is replaced with a temp dir.
# Without this, go build would use $HOME/Library/Caches/go-build inside the
# temp dir (empty), causing a full cold rebuild on every test run (~6s).
ORIGINAL_GOCACHE="$(go env GOCACHE 2>/dev/null || true)"
export ORIGINAL_GOCACHE
HOME="$(mktemp -d "${BATS_TEST_DIRNAME}/tmp-cli-home.XXXXXX")"
export HOME
mkdir -p "$HOME"
CLI_OWNS_GO_HELPERS=0
export CLI_OWNS_GO_HELPERS
if [[ -x "${MOLE_TEST_ANALYZE_BIN:-}" && -x "${MOLE_TEST_STATUS_BIN:-}" ]]; then
ANALYZE_BIN="$MOLE_TEST_ANALYZE_BIN"
STATUS_BIN="$MOLE_TEST_STATUS_BIN"
export ANALYZE_BIN STATUS_BIN
elif command -v go > /dev/null 2>&1; then
# Build Go binaries from current source for JSON tests.
# Point GOPATH/GOMODCACHE/GOCACHE at the real home so local focused runs
# can reuse caches when the full runner did not prebuild helpers.
ANALYZE_BIN="$(mktemp "${TMPDIR:-/tmp}/analyze-go.XXXXXX")"
STATUS_BIN="$(mktemp "${TMPDIR:-/tmp}/status-go.XXXXXX")"
GOPATH="${ORIGINAL_HOME}/go" GOMODCACHE="${ORIGINAL_HOME}/go/pkg/mod" \
GOCACHE="${ORIGINAL_GOCACHE}" \
go build -o "$ANALYZE_BIN" "$PROJECT_ROOT/cmd/analyze" 2>/dev/null
GOPATH="${ORIGINAL_HOME}/go" GOMODCACHE="${ORIGINAL_HOME}/go/pkg/mod" \
GOCACHE="${ORIGINAL_GOCACHE}" \
go build -o "$STATUS_BIN" "$PROJECT_ROOT/cmd/status" 2>/dev/null
CLI_OWNS_GO_HELPERS=1
export ANALYZE_BIN STATUS_BIN
fi
}
teardown_file() {
if [[ "$HOME" == "${BATS_TEST_DIRNAME}/tmp-"* ]]; then
rm -rf "$HOME/.config/mole"
rm -rf "$HOME"
fi
if [[ -n "${ORIGINAL_HOME:-}" ]]; then
export HOME="$ORIGINAL_HOME"
fi
if [[ "${CLI_OWNS_GO_HELPERS:-0}" == "1" ]]; then
rm -f "${ANALYZE_BIN:-}" "${STATUS_BIN:-}"
fi
}
create_fake_utils() {
local dir="$1"
mkdir -p "$dir"
cat >"$dir/sudo" <<'SCRIPT'
#!/usr/bin/env bash
if [[ "$1" == "-n" || "$1" == "-v" ]]; then
exit 0
fi
exec "$@"
SCRIPT
chmod +x "$dir/sudo"
cat >"$dir/bioutil" <<'SCRIPT'
#!/usr/bin/env bash
if [[ "$1" == "-r" ]]; then
echo "Touch ID: 1"
exit 0
fi
exit 0
SCRIPT
chmod +x "$dir/bioutil"
cat >"$dir/chown" <<'SCRIPT'
#!/usr/bin/env bash
exit 0
SCRIPT
chmod +x "$dir/chown"
cat >"$dir/install" <<'SCRIPT'
#!/usr/bin/env bash
args=()
skip_next=""
for arg in "$@"; do
if [[ -n "$skip_next" ]]; then skip_next=""; continue; fi
case "$arg" in -o|-g) skip_next=1 ;; *) args+=("$arg") ;; esac
done
exec /usr/bin/install "${args[@]}"
SCRIPT
chmod +x "$dir/install"
}
setup() {
# Safety: refuse to operate on a real home directory.
if [[ "$HOME" != "${BATS_TEST_DIRNAME}/tmp-"* ]]; then
printf 'FATAL: HOME is not a test temp dir: %s\n' "$HOME" >&2
return 1
fi
rm -rf "$HOME/.config/mole"
mkdir -p "$HOME/.config/mole"
}
@test "mole --help prints command overview" {
run env HOME="$HOME" "$PROJECT_ROOT/mole" --help
[ "$status" -eq 0 ]
[[ "$output" == *"mo clean"* ]] || return 1
[[ "$output" == *"mo optimize"* ]] || return 1
[[ "$output" == *"mo analyze"* ]] || return 1
[[ "$output" != *"mo optimise"* ]]
}
@test "mole --version reports script version" {
expected_version="$(grep '^VERSION=' "$PROJECT_ROOT/mole" | head -1 | sed 's/VERSION=\"\(.*\)\"/\1/')"
run env HOME="$HOME" "$PROJECT_ROOT/mole" --version
[ "$status" -eq 0 ]
[[ "$output" == *"$expected_version"* ]]
}
@test "mole --version does not hang on slow Homebrew detection" {
local fake_bin
fake_bin="$(mktemp -d "${BATS_TEST_TMPDIR}/fake-bin.XXXXXX")"
ln -s "$PROJECT_ROOT/mole" "$fake_bin/mole"
cat > "$fake_bin/brew" <<'SCRIPT'
#!/usr/bin/env bash
sleep 3
exit 1
SCRIPT
chmod +x "$fake_bin/brew"
run env HOME="$HOME" PATH="$fake_bin:$PATH" MOLE_HOMEBREW_DETECT_TIMEOUT=1 "$PROJECT_ROOT/mole" --version
[ "$status" -eq 0 ]
[[ "$output" == *"Install: Manual"* ]]
}
@test "mole --version shows nightly channel metadata" {
expected_version="$(grep '^VERSION=' "$PROJECT_ROOT/mole" | head -1 | sed 's/VERSION=\"\(.*\)\"/\1/')"
mkdir -p "$HOME/.config/mole"
cat > "$HOME/.config/mole/install_channel" <<'EOF'
CHANNEL=nightly
EOF
run env HOME="$HOME" "$PROJECT_ROOT/mole" --version
[ "$status" -eq 0 ]
[[ "$output" == *"Mole version $expected_version"* ]] || return 1
[[ "$output" == *"Channel: Nightly"* ]]
}
@test "mole unknown command returns error" {
run env HOME="$HOME" "$PROJECT_ROOT/mole" unknown-command
[ "$status" -ne 0 ]
[[ "$output" == *"Unknown command: unknown-command"* ]]
}
@test "mole --help does not list check command" {
run env HOME="$HOME" "$PROJECT_ROOT/mole" --help
[ "$status" -eq 0 ]
[[ "$output" != *"mo check"* ]]
}
@test "mole --help documents history command" {
run env HOME="$HOME" "$PROJECT_ROOT/mole" --help
[ "$status" -eq 0 ]
[[ "$output" == *"mo history"* ]]
}
@test "mole check is not a public command" {
run env HOME="$HOME" "$PROJECT_ROOT/mole" check --help
[ "$status" -ne 0 ]
[[ "$output" == *"Unknown command: check"* ]]
}
@test "mole doctor is not a public command" {
run env HOME="$HOME" "$PROJECT_ROOT/mole" doctor --help
[ "$status" -ne 0 ]
[[ "$output" == *"Unknown command: doctor"* ]]
}
@test "mole optimize --check is not a public option" {
run env HOME="$HOME" "$PROJECT_ROOT/mole" optimize --check
[ "$status" -ne 0 ]
[[ "$output" == *"Unknown optimize option: --check"* ]]
}
@test "mole uninstall --whitelist returns unsupported option error" {
run env HOME="$HOME" "$PROJECT_ROOT/mole" uninstall --whitelist
[ "$status" -ne 0 ]
[[ "$output" == *"Unknown uninstall option: --whitelist"* ]]
}
@test "main menu controls line shows the update shortcut only when an update is available" {
# The controls line is rendered only under a tty, so test the pure builder
# directly. Both the negative and positive cases run so the assertion
# cannot pass vacuously.
run /bin/bash --noprofile --norc -c "MOLE_TEST_MODE=1 MOLE_SKIP_MAIN=1 HOME=\"\$(mktemp -d)\" source '$PROJECT_ROOT/mole'; _main_menu_controls_line true false"
[ "$status" -eq 0 ] || return 1
[[ "$output" != *"U Update"* ]] || return 1
run /bin/bash --noprofile --norc -c "MOLE_TEST_MODE=1 MOLE_SKIP_MAIN=1 HOME=\"\$(mktemp -d)\" source '$PROJECT_ROOT/mole'; _main_menu_controls_line true true"
[ "$status" -eq 0 ] || return 1
[[ "$output" == *"U Update"* ]] || return 1
# TouchID setup takes precedence: no update shortcut even if one is ready.
run /bin/bash --noprofile --norc -c "MOLE_TEST_MODE=1 MOLE_SKIP_MAIN=1 HOME=\"\$(mktemp -d)\" source '$PROJECT_ROOT/mole'; _main_menu_controls_line false true"
[ "$status" -eq 0 ] || return 1
[[ "$output" == *"T TouchID"* ]] || return 1
[[ "$output" != *"U Update"* ]] || return 1
}
@test "show_main_menu keeps history out of the primary menu" {
run /bin/bash --noprofile --norc <<'EOF'
set -euo pipefail
HOME="$(mktemp -d)"
export HOME MOLE_TEST_MODE=1 MOLE_SKIP_MAIN=1
source "$PROJECT_ROOT/mole"
show_brand_banner() { printf 'banner\n'; }
show_menu_option() { printf '%s\n' "$2"; }
MAIN_MENU_BANNER=""
MAIN_MENU_UPDATE_MESSAGE=""
MAIN_MENU_SHOW_UPDATE=false
show_main_menu 1 true
EOF
[ "$status" -eq 0 ]
[[ "$output" == *"Clean Free up disk space"* ]] || return 1
[[ "$output" != *"History"* ]] || return 1
[[ "$output" != *"history"* ]]
}
@test "interactive_main_menu ignores U shortcut when update notice is hidden" {
run /bin/bash --noprofile --norc <<'EOF'
set -euo pipefail
HOME="$(mktemp -d)"
export HOME MOLE_TEST_MODE=1 MOLE_SKIP_MAIN=1
source "$PROJECT_ROOT/mole"
show_brand_banner() { :; }
show_main_menu() { :; }
hide_cursor() { :; }
show_cursor() { :; }
clear() { :; }
update_mole() { echo "UPDATE_CALLED"; }
state_file="$HOME/read_key_state"
read_key() {
if [[ ! -f "$state_file" ]]; then
: > "$state_file"
echo "UPDATE"
else
echo "QUIT"
fi
}
interactive_main_menu
EOF
[ "$status" -eq 0 ]
[[ "$output" != *"UPDATE_CALLED"* ]]
}
@test "read_update_message_cache ignores notices older than current script" {
run /bin/bash --noprofile --norc <<'EOF'
set -euo pipefail
HOME="$(mktemp -d)"
export HOME MOLE_TEST_MODE=1 MOLE_SKIP_MAIN=1
mkdir -p "$HOME/.cache/mole"
msg_cache="$HOME/.cache/mole/update_message"
printf 'Update 1.43.0 available, run mo update\n' > "$msg_cache"
touch -t 200001010000 "$msg_cache"
source "$PROJECT_ROOT/mole"
message="$(read_update_message_cache "$msg_cache")"
[[ -z "$message" ]] || exit 1
[[ ! -s "$msg_cache" ]] || exit 1
EOF
[ "$status" -eq 0 ]
}
@test "interactive_main_menu accepts U shortcut when update notice is visible" {
run /bin/bash --noprofile --norc <<'EOF'
set -euo pipefail
HOME="$(mktemp -d)"
export HOME MOLE_TEST_MODE=1 MOLE_SKIP_MAIN=1
mkdir -p "$HOME/.cache/mole"
printf 'update available\n' > "$HOME/.cache/mole/update_message"
source "$PROJECT_ROOT/mole"
show_brand_banner() { :; }
show_main_menu() { :; }
hide_cursor() { :; }
show_cursor() { :; }
clear() { :; }
update_mole() { echo "UPDATE_CALLED"; }
read_key() { echo "UPDATE"; }
interactive_main_menu
EOF
[ "$status" -eq 0 ]
[[ "$output" == *"UPDATE_CALLED"* ]]
}
@test "interactive_main_menu drains numeric shortcut Enter before launching uninstall" {
run /bin/bash --noprofile --norc <<'EOF'
set -euo pipefail
HOME="$(mktemp -d)"
export HOME MOLE_TEST_MODE=1 MOLE_SKIP_MAIN=1
source "$PROJECT_ROOT/mole"
fake_root="$HOME/fake-mole"
mkdir -p "$fake_root/bin"
cat > "$fake_root/bin/uninstall.sh" <<'SCRIPT'
#!/usr/bin/env bash
if IFS= read -r -s -n1 -t 0.1 key; then
if [[ -z "$key" ]]; then
echo "LEAK:ENTER"
else
printf 'LEAK:%s\n' "$key"
fi
else
echo "NO_LEAK"
fi
SCRIPT
chmod +x "$fake_root/bin/uninstall.sh"
SCRIPT_DIR="$fake_root"
show_brand_banner() { :; }
show_main_menu() { :; }
hide_cursor() { :; }
show_cursor() { :; }
interactive_main_menu < <(printf '2\n')
EOF
[ "$status" -eq 0 ]
[[ "$output" == *"NO_LEAK"* ]] || return 1
[[ "$output" != *"LEAK:"* ]]
}
@test "touchid status reports current configuration" {
run env HOME="$HOME" "$PROJECT_ROOT/mole" touchid status
[ "$status" -eq 0 ]
[[ "$output" == *"Touch ID"* ]]
}
@test "mo optimize command is recognized" {
run /bin/bash -c "grep -Eq '\"optimi[sz]e\"[[:space:]]*\\|[[:space:]]*\"optimi[sz]e\"' '$PROJECT_ROOT/mole'"
[ "$status" -eq 0 ]
}
@test "mo analyze binary is valid" {
if [[ -f "$PROJECT_ROOT/bin/analyze-go" ]]; then
[ -x "$PROJECT_ROOT/bin/analyze-go" ]
run file "$PROJECT_ROOT/bin/analyze-go"
[[ "$output" == *"Mach-O"* ]] || [[ "$output" == *"executable"* ]]
else
skip "analyze-go binary not built"
fi
}
@test "mo clean --debug creates debug log file" {
mkdir -p "$HOME/.config/mole"
run env HOME="$HOME" TERM="xterm-256color" MOLE_TEST_MODE=1 MO_DEBUG=1 "$PROJECT_ROOT/mole" clean --dry-run
[ "$status" -eq 0 ]
MOLE_OUTPUT="$output"
DEBUG_LOG="$HOME/Library/Logs/mole/mole_debug_session.log"
[ -f "$DEBUG_LOG" ]
run grep "Mole Debug Session" "$DEBUG_LOG"
[ "$status" -eq 0 ]
[[ "$MOLE_OUTPUT" =~ "Debug session log saved to" ]]
}
@test "mo clean without debug does not show debug log path" {
mkdir -p "$HOME/.config/mole"
run env HOME="$HOME" TERM="xterm-256color" MOLE_TEST_MODE=1 MO_DEBUG=0 "$PROJECT_ROOT/mole" clean --dry-run
[ "$status" -eq 0 ]
[[ "$output" != *"Debug session log saved to"* ]]
}
@test "mo clean --debug logs system info" {
mkdir -p "$HOME/.config/mole"
run env HOME="$HOME" TERM="xterm-256color" MOLE_TEST_MODE=1 MO_DEBUG=1 "$PROJECT_ROOT/mole" clean --dry-run
[ "$status" -eq 0 ]
DEBUG_LOG="$HOME/Library/Logs/mole/mole_debug_session.log"
run grep "User:" "$DEBUG_LOG"
[ "$status" -eq 0 ]
run grep "Architecture:" "$DEBUG_LOG"
[ "$status" -eq 0 ]
}
@test "mo clean --help includes external volume option" {
run env HOME="$HOME" "$PROJECT_ROOT/mole" clean --help
[ "$status" -eq 0 ]
[[ "$output" == *"--external PATH"* ]] || return 1
[[ "$output" == *"already-uninstalled apps"* ]]
}
@test "mo uninstall --help directs leftover-only cleanup to clean" {
run env HOME="$HOME" "$PROJECT_ROOT/mole" uninstall --help
[ "$status" -eq 0 ]
[[ "$output" == *"already gone, use mo clean"* ]]
}
@test "mo clean --external accepts canonicalized custom root" {
real_root="$(mktemp -d "$HOME/ext-real.XXXXXX")"
link_root="$HOME/ext-link"
ln -s "$real_root" "$link_root"
mkdir -p "$link_root/USB/.Trashes"
touch "$link_root/USB/.Trashes/cache.tmp"
mock_bin="$HOME/mock-bin"
mkdir -p "$mock_bin"
cat > "$mock_bin/diskutil" <<'EOF'
#!/usr/bin/env bash
exit 0
EOF
chmod +x "$mock_bin/diskutil"
run env HOME="$HOME" PATH="$mock_bin:$PATH" MOLE_EXTERNAL_VOLUMES_ROOT="$link_root" \
MOLE_TEST_NO_AUTH=1 "$PROJECT_ROOT/mole" clean --external "$link_root/USB" --dry-run
[ "$status" -eq 0 ]
[[ "$output" == *"Clean External Volume"* ]] || return 1
[[ "$output" == *"External volume cleanup"* ]]
}
@test "touchid status reflects pam file contents" {
pam_file="$HOME/pam_test"
cat >"$pam_file" <<'EOF'
auth sufficient pam_opendirectory.so
EOF
run env MOLE_PAM_SUDO_FILE="$pam_file" "$PROJECT_ROOT/bin/touchid.sh" status
[ "$status" -eq 0 ]
[[ "$output" == *"not configured"* ]] || return 1
cat >"$pam_file" <<'EOF'
auth sufficient pam_tid.so
EOF
run env MOLE_PAM_SUDO_FILE="$pam_file" "$PROJECT_ROOT/bin/touchid.sh" status
[ "$status" -eq 0 ]
[[ "$output" == *"enabled"* ]]
}
@test "enable_touchid inserts pam_tid line in pam file" {
pam_file="$HOME/pam_enable"
cat >"$pam_file" <<'EOF'
auth sufficient pam_opendirectory.so
EOF
fake_bin="$HOME/fake-bin"
create_fake_utils "$fake_bin"
run env PATH="$fake_bin:$PATH" MOLE_PAM_SUDO_FILE="$pam_file" "$PROJECT_ROOT/bin/touchid.sh" enable
[ "$status" -eq 0 ]
grep -q "pam_tid.so" "$pam_file"
[[ -f "${pam_file}.mole-backup" ]]
}
@test "disable_touchid removes pam_tid line" {
pam_file="$HOME/pam_disable"
cat >"$pam_file" <<'EOF'
auth sufficient pam_tid.so
auth sufficient pam_opendirectory.so
EOF
fake_bin="$HOME/fake-bin-disable"
create_fake_utils "$fake_bin"
run env PATH="$fake_bin:$PATH" MOLE_PAM_SUDO_FILE="$pam_file" "$PROJECT_ROOT/bin/touchid.sh" disable
[ "$status" -eq 0 ]
run grep "pam_tid.so" "$pam_file"
[ "$status" -ne 0 ]
}
@test "touchid enable --dry-run does not modify pam file" {
pam_file="$HOME/pam_enable_dry_run"
cat >"$pam_file" <<'EOF'
auth sufficient pam_opendirectory.so
EOF
run env MOLE_PAM_SUDO_FILE="$pam_file" "$PROJECT_ROOT/bin/touchid.sh" enable --dry-run
[ "$status" -eq 0 ]
[[ "$output" == *"DRY RUN MODE"* ]] || return 1
run grep "pam_tid.so" "$pam_file"
[ "$status" -ne 0 ]
}
@test "enable_touchid sets correct file permissions on pam file" {
pam_file="$HOME/pam_perms_enable"
cat >"$pam_file" <<'EOF'
auth sufficient pam_opendirectory.so
EOF
fake_bin="$HOME/fake-bin-perms-enable"
create_fake_utils "$fake_bin"
run env PATH="$fake_bin:$PATH" MOLE_PAM_SUDO_FILE="$pam_file" "$PROJECT_ROOT/bin/touchid.sh" enable
[ "$status" -eq 0 ]
grep -q "pam_tid.so" "$pam_file"
local perms
perms=$(stat -f "%Lp" "$pam_file" 2>/dev/null || stat -c "%a" "$pam_file" 2>/dev/null)
[ "$perms" = "444" ]
}
@test "disable_touchid sets correct file permissions on pam file" {
pam_file="$HOME/pam_perms_disable"
cat >"$pam_file" <<'EOF'
auth sufficient pam_tid.so
auth sufficient pam_opendirectory.so
EOF
fake_bin="$HOME/fake-bin-perms-disable"
create_fake_utils "$fake_bin"
run env PATH="$fake_bin:$PATH" MOLE_PAM_SUDO_FILE="$pam_file" "$PROJECT_ROOT/bin/touchid.sh" disable
[ "$status" -eq 0 ]
local perms
perms=$(stat -f "%Lp" "$pam_file" 2>/dev/null || stat -c "%a" "$pam_file" 2>/dev/null)
[ "$perms" = "444" ]
}
@test "enable_touchid sets correct permissions on sudo_local file" {
pam_file="$HOME/pam_perms_sudolocal"
pam_local="$(dirname "$pam_file")/sudo_local_perms"
cat >"$pam_file" <<'EOF'
# sudo: auth account password session
auth include sudo_local
auth sufficient pam_opendirectory.so
EOF
fake_bin="$HOME/fake-bin-perms-sudolocal"
create_fake_utils "$fake_bin"
run env PATH="$fake_bin:$PATH" \
MOLE_PAM_SUDO_FILE="$pam_file" \
MOLE_PAM_SUDO_LOCAL_FILE="$pam_local" \
"$PROJECT_ROOT/bin/touchid.sh" enable
[ "$status" -eq 0 ]
grep -q "pam_tid.so" "$pam_local"
local perms
perms=$(stat -f "%Lp" "$pam_local" 2>/dev/null || stat -c "%a" "$pam_local" 2>/dev/null)
[ "$perms" = "444" ]
}
# --- JSON output mode tests ---
@test "mo analyze --json outputs valid JSON with expected fields" {
if [[ ! -x "${ANALYZE_BIN:-}" ]]; then
skip "analyze binary not available (go not installed?)"
fi
run "$ANALYZE_BIN" --json /tmp
[ "$status" -eq 0 ]
# Validate it is parseable JSON
echo "$output" | python3 -c "import sys, json; json.load(sys.stdin)"
# Check required top-level keys
echo "$output" | python3 -c "
import sys, json
data = json.load(sys.stdin)
assert 'path' in data, 'missing path'
assert 'overview' in data, 'missing overview'
assert 'entries' in data, 'missing entries'
assert 'total_size' in data, 'missing total_size'
assert 'total_files' in data, 'missing total_files'
assert isinstance(data['entries'], list), 'entries is not a list'
"
}
@test "mo analyze --json entries contain required fields" {
if [[ ! -x "${ANALYZE_BIN:-}" ]]; then
skip "analyze binary not available (go not installed?)"
fi
run "$ANALYZE_BIN" --json /tmp
[ "$status" -eq 0 ]
echo "$output" | python3 -c "
import sys, json
data = json.load(sys.stdin)
assert data['overview'] is False, 'explicit path should not be overview mode'
for entry in data['entries']:
assert 'name' in entry, 'entry missing name'
assert 'path' in entry, 'entry missing path'
assert 'size' in entry, 'entry missing size'
assert 'is_dir' in entry, 'entry missing is_dir'
"
}
@test "mo analyze --json path reflects target directory" {
if [[ ! -x "${ANALYZE_BIN:-}" ]]; then
skip "analyze binary not available (go not installed?)"
fi
run "$ANALYZE_BIN" --json /tmp
[ "$status" -eq 0 ]
echo "$output" | python3 -c "
import sys, json
data = json.load(sys.stdin)
assert data['path'] == '/tmp' or data['path'] == '/private/tmp', \
f\"unexpected path: {data['path']}\"
"
}
@test "mo status --json outputs valid JSON with expected fields" {
if [[ ! -x "${STATUS_BIN:-}" ]]; then
skip "status binary not available (go not installed?)"
fi
run "$STATUS_BIN" --json
[ "$status" -eq 0 ]
# Validate it is parseable JSON
echo "$output" | python3 -c "import sys, json; json.load(sys.stdin)"
# Check required top-level keys
echo "$output" | python3 -c "
import sys, json
data = json.load(sys.stdin)
for key in ['cpu', 'memory', 'disks', 'health_score', 'host', 'uptime']:
assert key in data, f'missing key: {key}'
"
}
@test "mo status --json cpu section has expected structure" {
if [[ ! -x "${STATUS_BIN:-}" ]]; then
skip "status binary not available (go not installed?)"
fi
run "$STATUS_BIN" --json
[ "$status" -eq 0 ]
echo "$output" | python3 -c "
import sys, json
data = json.load(sys.stdin)
cpu = data['cpu']
assert 'usage' in cpu, 'cpu missing usage'
assert 'logical_cpu' in cpu, 'cpu missing logical_cpu'
assert isinstance(cpu['usage'], (int, float)), 'cpu usage is not a number'
"
}
@test "mo status --json memory section has expected structure" {
if [[ ! -x "${STATUS_BIN:-}" ]]; then
skip "status binary not available (go not installed?)"
fi
run "$STATUS_BIN" --json
[ "$status" -eq 0 ]
echo "$output" | python3 -c "
import sys, json
data = json.load(sys.stdin)
mem = data['memory']
assert 'total' in mem, 'memory missing total'
assert 'used' in mem, 'memory missing used'
assert 'used_percent' in mem, 'memory missing used_percent'
assert mem['total'] > 0, 'memory total should be positive'
"
}
@test "mo status --json piped to stdout auto-detects JSON mode" {
if [[ ! -x "${STATUS_BIN:-}" ]]; then
skip "status binary not available (go not installed?)"
fi
# When piped (not a tty), status should auto-detect and output JSON
output=$("$STATUS_BIN" 2>/dev/null)
echo "$output" | python3 -c "import sys, json; json.load(sys.stdin)"
}
@test "mo status --watch streams newline-delimited JSON" {
if [[ ! -x "${STATUS_BIN:-}" ]]; then
skip "status binary not available (go not installed?)"
fi
run python3 - "$STATUS_BIN" <<'PY'
import json
import subprocess
import sys
status_bin = sys.argv[1]
proc = subprocess.Popen(
[status_bin, "--watch", "--interval", "200ms"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
lines = []
try:
for _ in range(3):
line = proc.stdout.readline()
if not line:
raise RuntimeError("missing watch output")
snapshot = json.loads(line)
for key in ("collected_at", "cpu", "memory", "disk_io", "network", "health_score"):
if key not in snapshot:
raise RuntimeError(f"missing key: {key}")
lines.append(snapshot)
finally:
proc.terminate()
try:
proc.wait(timeout=3)
except subprocess.TimeoutExpired:
proc.kill()
proc.wait(timeout=3)
if proc.stderr.read():
raise RuntimeError("watch wrote to stderr")
print(f"watch_lines={len(lines)}")
PY
[ "$status" -eq 0 ]
[[ "$output" == *"watch_lines=3"* ]]
}