Skip to content

feat: add bats unit tests for lib/github-common.sh#41

Merged
locus313 merged 4 commits into
mainfrom
feat/unit-tests-common-lib
Jun 27, 2026
Merged

feat: add bats unit tests for lib/github-common.sh#41
locus313 merged 4 commits into
mainfrom
feat/unit-tests-common-lib

Conversation

@locus313

Copy link
Copy Markdown
Owner

Summary

Adds 15 unit tests for the shared library functions in lib/github-common.sh — the highest-leverage target since it's sourced by every script.

Tests added (tests/test_common.bats)

Function Cases
validate_slug alphanumeric/hyphen/underscore pass; space/slash/dot/metachar fail
require_env_var unset exits 1, empty exits 1, set exits 0
require_command found exits 0, not-found exits 1
gh_api returns __404__ on 404, __422__ on 422, body on 200

No real network calls — curl is mocked per-test via a temporary binary prepended to PATH.

CI

Adds a test job to .github/workflows/ci.yml that installs bats and runs the suite on every PR.

locus313 and others added 4 commits June 27, 2026 09:40
15 tests covering the shared library's pure-logic functions and
gh_api sentinel returns — all testable without a real GitHub token.

- validate_slug: valid (alphanumeric/hyphen/underscore) and invalid
  (space/slash/dot/metachar) inputs
- require_env_var: unset, empty, and set variable cases
- require_command: found and not-found cases
- gh_api: __404__, __422__ sentinels and 200 body pass-through
  (curl is mocked per-test via a temporary PATH-prepended binary)

Also adds a 'test' job to ci.yml that installs bats and runs the suite.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…tions

test_common.bats (29 tests):
- Add err, configure_gh_auth, validate_token, validate_github_token URL
  warning, get_repo_page_count (with/without Link header), gh_api_paginate
  (404, 422, single-page)
- Upgrade _mock_curl to use universal mock_curl.sh (handles both gh_api
  stdout mode and gh_api_paginate/validate_token -o/-D file mode)

tests/mock_curl.sh:
- Universal drop-in curl mock; response data via env vars (MOCK_CURL_CODE,
  MOCK_CURL_BODY, MOCK_CURL_LINK) — no quoting issues with JSON bodies

test_script_validation.bats (62 tests):
- Every script: missing required env vars exit 1 in the correct order
- Arg parsing: --help exits 0, unknown args exit 1, recognised flags
  (--dry-run, --type) don't trigger Unknown argument errors
- Invalid enum values: --type, DEPENDABOT_REASON, SECRET_SCANNING_RESOLUTION
- github-import-repo: non-GitHub GIT_URL_PREFIX exits 1 (security guard)
- github-repo-permissions-report: missing -r exits 1

ci.yml: run bats tests/ (all bats files)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…-instructions.md

- Replaced the placeholder Testing section in AGENTS.md with full
  bats suite documentation: test files table, what to test per script,
  and the mock_curl.sh pattern with a code example
- Added step 7 (write tests) to the Adding a New Script checklist in
  AGENTS.md; renumbered subsequent steps
- Updated CI/CD bullet to mention the bats test job
- Updated Maintenance Matrix 'Add a new script' row to include
  tests/test_script_validation.bats
- Mirrored all changes in .github/copilot-instructions.md: expanded
  Adding New Scripts checklist with mandatory test step, replaced
  single-line Testing Approach with table + requirement prose

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Add Unit Tests subsection after Pre-commit Hooks: install
  instructions for macOS/Ubuntu, bats tests/ run command, test-file
  table, note that CI runs bats on every PR
- Update Contributing checklist: add 'add test section to
  test_script_validation.bats' to step 3, add step 6 'bats tests/'
  before the test-on-non-prod-org step

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@locus313 locus313 merged commit 226c1f3 into main Jun 27, 2026
3 checks passed
@locus313 locus313 deleted the feat/unit-tests-common-lib branch June 27, 2026 22:05
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