Skip to content

test: add bats unit tests and CI job#21

Open
CheapFuck wants to merge 1 commit into
codam-coding-college:mainfrom
CheapFuck:test/bats-unit-tests
Open

test: add bats unit tests and CI job#21
CheapFuck wants to merge 1 commit into
codam-coding-college:mainfrom
CheapFuck:test/bats-unit-tests

Conversation

@CheapFuck

Copy link
Copy Markdown

Closes #11.

Adds a bats-core unit-test suite plus a CI job that gates the release, so a logic regression fails the PR/push before semantic-release cuts a new version.

Tests (tests/)

  • update_compare.bats_towel_update_compare semver logic (lt/gt/eq). This is the function that decides whether a newer release exists, so it's the most release-critical piece. Covers equal/patch/minor/major, missing-field defaults (1.2 == 1.2.0), and numeric-vs-lexical (1.10.0 > 1.9.0).
  • common_helpers.batsinside_towel, the exports/host path helpers, towel_repoquery_exportables filtering (with dnf mocked via a PATH shim), and towel_export_bin_wrapper output (wrapper is executable, contains the trampoline, host symlink created).
  • cli.bats — the documented CLI exit-code contract as real subprocesses: towel update misuse → 64, --help0, towel --version format, unknown command → 1.
  • helpers/load.bash — sources scripts into the test shell with an isolated TOWEL_DATA (under $BATS_TEST_TMPDIR), tolerating the enable/unalias shell-hardening under bats' errexit.

Production change (minimal)

To unit-test _towel_update_compare/_towel_fetch_latest_version, towel-update needed to be source-safe. Added one guard before its imperative tail:

(return 0 2>/dev/null) && return 0

Direct execution is unchanged — the dispatcher always executes subcommands ("$TOWEL_BIN_DIR/towel-$cmd" "$@"), never sources them. Verified locally: towel update with no args still exits 64, and the script lints clean (shellcheck -x + shfmt -d).

CI / release

  • New tests job: bats --print-output-on-failure tests.
  • Added tests to the release job's needs: so failing tests block semantic-release.
  • The .bats files use the bats DSL (not plain bash), so they stay out of the shellcheck/shfmt globs (which only target install + bin/*) — the existing lint jobs are unaffected.
  • Excluded tests/ from the release tarball in .releaserc.yml, and documented bats --print-output-on-failure tests in AGENTS.md.

Notes / possible follow-ups

  • bats is installed in CI via apt-get install -y bats (Ubuntu's 1.10.0). Happy to switch to a pinned bats-core download instead, to match the shfmt/distrobox pinning style — let me know your preference.
  • _towel_fetch_latest_version is intentionally left for a follow-up: it calls /usr/bin/curl by absolute path (per the repo's hardening convention), which can't be mocked offline without adding an injection seam. Can do that separately if you'd like it covered.

All 22 tests pass locally.

Add a bats-core unit-test suite plus a gating CI job so logic regressions
fail PRs/pushes before semantic-release cuts a release (the issue's goal:
prevent broken releases).

- tests/update_compare.bats: _towel_update_compare semver lt/gt/eq, incl.
  numeric-vs-lexical (1.10.0 > 1.9.0) and missing-field defaults.
- tests/common_helpers.bats: inside_towel, exports/host path helpers,
  towel_repoquery_exportables filtering (dnf mocked via PATH shim), and
  towel_export_bin_wrapper output.
- tests/cli.bats: CLI exit-code contract (towel update misuse -> 64,
  --help -> 0, towel --version format, unknown command -> 1).
- tests/helpers/load.bash: sources scripts into the test shell with an
  isolated TOWEL_DATA, tolerating the enable/unalias hardening under errexit.

To make towel-update unit-testable, add a source-safe guard
'(return 0 2>/dev/null) && return 0' before its imperative tail; direct
execution is unchanged (dispatcher always execs subcommands).

CI: new 'tests' job runs 'bats --print-output-on-failure tests' and is added
to the release job's needs. Exclude tests/ from the release tarball and note
the command in AGENTS.md.

Closes codam-coding-college#11
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.

Add unit tests

1 participant