Skip to content

test(openemr-cmd): deeper non-worktree bats — snapshot/capsule, webroot, LDAP, prek dispatch; 4 more set-u fixes#830

Merged
bradymiller merged 2 commits into
openemr:masterfrom
bradymiller:test/openemr-cmd-nonworktree-cluster-2
Jun 24, 2026
Merged

test(openemr-cmd): deeper non-worktree bats — snapshot/capsule, webroot, LDAP, prek dispatch; 4 more set-u fixes#830
bradymiller merged 2 commits into
openemr:masterfrom
bradymiller:test/openemr-cmd-nonworktree-cluster-2

Conversation

@bradymiller

@bradymiller bradymiller commented Jun 24, 2026

Copy link
Copy Markdown
Member

Summary

Continuation of #828 + #829's non-worktree coverage. 28 new tests across 3 files plus 4 small script fixes for the same set-u bug pattern uncovered in #829 (the ev fix).

What's new (3 files, 28 tests)

File What it pins
snapshot_capsule_commands.bats bs/rs → `/root/devtools backup
webroot_swagger_ldap.bats cwb/cwo run an in-container sed against apache's openemr.conf then docker restart <id>; swtm <name> passes through to /root/devtools set-swagger-to-multisite; el/dld are plain devtools passthroughs.
prek_dispatch.bats Outer dispatch shape (docker exec -w /var/www/localhost/htdocs/openemr -i <id> sh -c <inline> sh <args>). Args pass-through for run / install-hooks / --version. Inline script contains the actionlint-docker → actionlint-system substitution + the `run

4 more set-u fixes (same pattern as the ev fix in #829)

creat_a_backup_snapshot / restore_from_a_snapshot / copy_capsule_from_docker_to_host / copy_capsule_from_host_to_docker all had local FOO="$1" (or $2) BEFORE their $# != N check. Under set -u that crashes with "unbound variable" when called with no args instead of showing the documented help. All four reorder the check ahead of the local assignments. Pinned by dedicated regression tests.

Test plan

  • Full hermetic bats suite — 290 tests, 0 failures
  • shellcheck utilities/openemr-cmd/openemr-cmd clean
  • CI: BATS openemr-cmd (ubuntu-22.04) green
  • CI: BATS openemr-cmd (macos-14) green
  • CI: ShellCheck green
  • CI: e2e jobs green (script touched — workflow filter triggers)
  • CodeRabbit review

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved argument handling for snapshot/capsule-related subcommands so missing inputs produce clean usage errors instead of set -u “unbound variable” crashes.
    • Hardened capsule copy behavior with earlier argument-count checks (including optional destination handling and consistent missing-file checks).
  • Tests
    • Added/expanded BATS coverage for prek dispatch, snapshot/capsule command CLI contracts, webroot switching, Swagger-to-multisite, and LDAP enable/disable.
  • Chores
    • Updated the openemr-cmd version to 1.0.47.

…ot, LDAP, prek dispatch; 4 more set-u fixes

28 new tests across 3 files plus 4 small script fixes for the same
set-u bug pattern uncovered in openemr#829 (ev): `local FOO="$1"` before the
`$# != 1` check crashes under set -u instead of showing the usage hint.

  - snapshot_capsule_commands.bats: bs / rs / gc / pc.
    * Happy-path dispatch (bs/rs → /root/devtools backup|restore;
      gc/pc → docker cp in/out of /snapshots/).
    * No-arg usage hints with the right exit codes (20 / 21 / 19 / 18).
    * pc with a missing file exits 15 with the documented error.
    * Fixes: creat_a_backup_snapshot, restore_from_a_snapshot,
      copy_capsule_from_docker_to_host, copy_capsule_from_host_to_docker
      all reorder the $# check ahead of `local FOO="$1"`. Same pattern
      as ensure_current_ver_with_upgrade_ver in openemr#829.
  - webroot_swagger_ldap.bats: cwb/cwo (sed in container + docker
    restart), swtm (devtools passthrough with arg), el/dld (devtools
    enable-ldap/disable-ldap).
  - prek_dispatch.bats: outer dispatch shape (docker exec -w
    /var/www/localhost/htdocs/openemr -i <id> sh -c <inline> sh <args>),
    args pass-through for run/install-hooks/--version, the inline
    script contains the actionlint-docker → actionlint-system
    substitution + 'run|install-hooks' case + 'exec pre-commit' pass-
    through, and the no-container error path.

OC_SCRIPT_FUNCS_END bumped to 1741 to match the post-fix function-defs
end. VERSION 1.0.46 → 1.0.47.

Total: 290 hermetic tests, 0 failures. shellcheck clean.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jun 24, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: d7b66c6d-ca32-47ba-abf7-d197d233891a

📥 Commits

Reviewing files that changed from the base of the PR and between 9573dd0 and fd6b4d0.

📒 Files selected for processing (2)
  • tests/bats/openemr-cmd/snapshot_capsule_commands.bats
  • tests/bats/openemr-cmd/webroot_swagger_ldap.bats
🚧 Files skipped from review as they are similar to previous changes (2)
  • tests/bats/openemr-cmd/webroot_swagger_ldap.bats
  • tests/bats/openemr-cmd/snapshot_capsule_commands.bats

📝 Walkthrough

Walkthrough

openemr-cmd now validates snapshot/capsule arguments earlier to avoid set -u crashes, bumps to version 1.0.47, adjusts the BATS sourcing cutoff, and adds three BATS suites covering snapshot/capsule, prek, and webroot/swagger/LDAP command behavior.

Changes

Unbound-variable fix and BATS test coverage

Layer / File(s) Summary
Arg-count validation fix in snapshot/capsule functions + version bump
utilities/openemr-cmd/openemr-cmd
VERSION is bumped to 1.0.47; creat_a_backup_snapshot, restore_from_a_snapshot, copy_capsule_from_docker_to_host, and copy_capsule_from_host_to_docker now validate argument count before declaring locals or referencing positional parameters.
Snapshot/capsule BATS tests + helper cutoff update
tests/bats/openemr-cmd/helpers.bash, tests/bats/openemr-cmd/snapshot_capsule_commands.bats
OC_SCRIPT_FUNCS_END is adjusted from 1744 to 1741. The new suite covers bs/backup-snapshot, rs/restore-snapshot, gc/get-capsule, and pc/put-capsule dispatch, exit codes, usage text, and docker-stub log assertions.
prek dispatch BATS tests
tests/bats/openemr-cmd/prek_dispatch.bats
The new suite validates openemr-cmd prek <args> invocation shape, argument forwarding, actionlint substitution tokens, inline-script branching for `run
Webroot, swagger-to-multisite, and LDAP BATS tests
tests/bats/openemr-cmd/webroot_swagger_ldap.bats
The new suite covers cwb/cwo webroot changes, swtm/set-swagger-to-multisite dispatch, and el/dld LDAP enable/disable dispatch.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • openemr/openemr-devops#823: Updates the same OC_SCRIPT_FUNCS_END line-cutoff constant in helpers.bash to keep oc_source_funcs() aligned with changes to openemr-cmd's function layout.
  • openemr/openemr-devops#824: Also modifies tests/bats/openemr-cmd/helpers.bash by updating the shared OC_SCRIPT_FUNCS_END line-cutoff used when sourcing function-only code from openemr-cmd.

Poem

🐇 I hopped through the args before locals could bite,
No unbound-variable ghosts in the night.
With stubs and bats, the commands all sing,
Snapshot and webroot and prek take wing.
1.0.47 bounces along with glee,
A tidy little trail for rabbit and tree 🌿

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the added non-worktree Bats coverage and the four set -u fixes in openemr-cmd.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
tests/bats/openemr-cmd/webroot_swagger_ldap.bats (1)

59-68: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

The cwo DocumentRoot assertion does not actually distinguish the openemr direction.

The logged sed command for both directions contains the literal search pattern DocumentRoot /var/www/localhost/htdocs.*@.... Your regex DocumentRoot /var/www/localhost/htdocs[^/] matches the . immediately after htdocs in that search pattern, so it also matches the cwb (blank) command, whose replacement string is .../htdocs/openemr. As written, this test would still pass even if cwo regressed to the blank sed, defeating its stated purpose ("set DocumentRoot WITHOUT /openemr suffix").

Anchor the assertion to the replacement segment instead so it fails when the wrong direction runs.

♻️ Match the replacement target, not the search pattern
-    grep -Eq 'DocumentRoot /var/www/localhost/htdocs[^/]' "${STUB_DIR}/docker.log" \
-        || { cat "${STUB_DIR}/docker.log"; fail "expected sed to set DocumentRoot WITHOUT /openemr suffix"; }
+    grep -Fq "`@DocumentRoot` /var/www/localhost/htdocs@g" "${STUB_DIR}/docker.log" \
+        || { cat "${STUB_DIR}/docker.log"; fail "expected sed replacement to set DocumentRoot WITHOUT /openemr suffix"; }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/bats/openemr-cmd/webroot_swagger_ldap.bats` around lines 59 - 68, The
cwo test assertion is matching the sed search pattern instead of the
replacement, so it cannot distinguish the openemr direction from the
blank-webroot direction. Update the check in the cwo case to inspect the
docker.log entry for the replacement target used by cwo, and anchor it to the
DocumentRoot value after sed runs so it fails if the command still points at the
/openemr path. Use the existing cwo test block and the docker.log grep assertion
to make the match direction-specific.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tests/bats/openemr-cmd/snapshot_capsule_commands.bats`:
- Around line 119-122: The temporary capsule filename creation in the snapshot
capsule test is using a GNU-specific mktemp pattern that breaks on macOS; update
the mktemp usage in the pc snapshot test setup to a BSD/macOS-compatible form
while keeping the same tmp_cap variable and oc_run flow. Locate the change in
snapshot_capsule_commands.bats around the local tmp_cap assignment and replace
the -p-based invocation with a portable mktemp pattern under BATS_TMPDIR so the
test works in the macOS matrix.

---

Nitpick comments:
In `@tests/bats/openemr-cmd/webroot_swagger_ldap.bats`:
- Around line 59-68: The cwo test assertion is matching the sed search pattern
instead of the replacement, so it cannot distinguish the openemr direction from
the blank-webroot direction. Update the check in the cwo case to inspect the
docker.log entry for the replacement target used by cwo, and anchor it to the
DocumentRoot value after sed runs so it fails if the command still points at the
/openemr path. Use the existing cwo test block and the docker.log grep assertion
to make the match direction-specific.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: c1c651d2-21e9-4ff4-81f8-4abc6678ac24

📥 Commits

Reviewing files that changed from the base of the PR and between ec028ac and 9573dd0.

📒 Files selected for processing (5)
  • tests/bats/openemr-cmd/helpers.bash
  • tests/bats/openemr-cmd/prek_dispatch.bats
  • tests/bats/openemr-cmd/snapshot_capsule_commands.bats
  • tests/bats/openemr-cmd/webroot_swagger_ldap.bats
  • utilities/openemr-cmd/openemr-cmd

Comment thread tests/bats/openemr-cmd/snapshot_capsule_commands.bats
…ion-specific cwo assertion

CodeRabbit review on openemr#830 caught two test-quality issues:

  1. snapshot_capsule_commands.bats used `mktemp -p <dir>`, which is GNU-
     only. macOS bsd-mktemp accepts the template positionally, not via
     -p. macOS CI passed (BSD mktemp likely ignored the unrecognized -p
     and the test still got a file), but the form was non-portable.
     Switch to the full-path template form: `mktemp <dir>/<template>`,
     which works on both.

  2. webroot_swagger_ldap.bats's cwo assertion matched the sed SEARCH
     pattern (which is identical for cwb and cwo: both contain
     `htdocs.*@` since `@` is non-`/`), defeating the test's purpose
     of distinguishing the openemr direction from the blank direction.
     Anchor to the sed REPLACEMENT segment instead — cwo replaces with
     `...htdocs@g`, cwb with `...htdocs/openemr@g`. Now a regression
     that swaps the directions would actually fail the test.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.

1 participant