Skip to content

fix(clean): avoid arithmetic error when no unavailable simulators exist (#742)#743

Merged
tw93 merged 2 commits intotw93:mainfrom
sebastianbreguel:fix/742-simctl-grep-count
Apr 15, 2026
Merged

fix(clean): avoid arithmetic error when no unavailable simulators exist (#742)#743
tw93 merged 2 commits intotw93:mainfrom
sebastianbreguel:fix/742-simctl-grep-count

Conversation

@sebastianbreguel
Copy link
Copy Markdown
Contributor

Fixes #742. Regression from #714.

grep -c exits 1 on zero matches, so the || echo \"0\" fallback fires even when grep already printed "0", producing "0\n0" and breaking the [[ -gt ]] comparison in clean_xcode_tools.

Switches to the awk '/pattern/ { count++ } END { print count+0 }' pattern already used in lib/clean/dev.sh:778,818 — exit-code independent, always emits a single integer. Keeps a numeric sanity regex as a belt-and-suspenders guard.

Changes

  • lib/clean/app_caches.sh: replace grep -cE ... || echo "0" with command awk counter + numeric validation.
  • tests/clean_app_caches.bats: add two regression tests (zero unavailable sims + populated DRY_RUN path).

Verification

bats tests/clean_app_caches.bats -f "simulator|Xcode"
ok 1 clean_xcode_tools skips derived data when Xcode running
ok 2 clean_xcode_tools cleans documentation caches when Xcode is not running
ok 3 clean_xcode_tools handles zero unavailable simulators without syntax error
ok 4 clean_xcode_tools reports unavailable simulators when present

Credit to @yetval for identifying the exact root cause in the issue thread.

…st (tw93#742)

Regression from tw93#714. `grep -c` exits 1 on zero matches, so the
`|| echo "0"` fallback fires even when grep already printed "0",
producing "0\n0" and breaking the `[[ -gt ]]` comparison.

Switches to the `awk '{count++} END {print count+0}'` pattern already
used in lib/clean/dev.sh — exit-code independent, always emits a single
integer. Adds regression tests for the zero-sim and populated-sim
DRY_RUN paths.
@sebastianbreguel sebastianbreguel requested a review from tw93 as a code owner April 14, 2026 21:15
The help text in lib/core/help.sh documents the --permanent flag
with a string containing "rm -rf", triggering a false positive in
the security check. Add "echo " to the grep exclusion filter.
@tw93 tw93 merged commit 5a00a9c into tw93:main Apr 15, 2026
9 checks passed
@tw93
Copy link
Copy Markdown
Owner

tw93 commented Apr 15, 2026

@sebastianbreguel thanks for fixing the simulator count arithmetic error, merged and will be in the next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] app_caches.sh: line 79: [[: 0 0: syntax error in expression (error token is "0")

2 participants