|
| 1 | +#!/usr/bin/env bats |
| 2 | +# ============================================================================= |
| 3 | +# tests/test_common.bats |
| 4 | +# |
| 5 | +# Unit tests for lib/github-common.sh — pure-logic functions and gh_api |
| 6 | +# sentinel returns. No real network calls are made; curl is mocked per test. |
| 7 | +# |
| 8 | +# Requirements: |
| 9 | +# - bats (https://github.com/bats-core/bats-core / apt install bats) |
| 10 | +# |
| 11 | +# Usage: |
| 12 | +# bats tests/test_common.bats |
| 13 | +# ============================================================================= |
| 14 | + |
| 15 | +LIB_PATH="${BATS_TEST_DIRNAME}/../lib/github-common.sh" |
| 16 | + |
| 17 | +# Per-test mock binary directory; prepend to PATH in subshells that need mocking |
| 18 | +MOCK_BIN="" |
| 19 | + |
| 20 | +setup() { |
| 21 | + MOCK_BIN="$(mktemp -d)" |
| 22 | +} |
| 23 | + |
| 24 | +teardown() { |
| 25 | + rm -rf "$MOCK_BIN" |
| 26 | +} |
| 27 | + |
| 28 | +# Write a mock curl that outputs <body>\n<status_code> and ignores all arguments. |
| 29 | +# gh_api captures: body=$(curl -s -w "\n%{http_code}" ...) then splits on last line. |
| 30 | +# Body and status are passed via env vars to avoid quoting issues with JSON content. |
| 31 | +_mock_curl() { |
| 32 | + local code="$1" body="${2:-}" |
| 33 | + export MOCK_CURL_CODE="$code" |
| 34 | + export MOCK_CURL_BODY="$body" |
| 35 | + printf '#!/bin/sh\nprintf "%%s\\n%%s" "$MOCK_CURL_BODY" "$MOCK_CURL_CODE"\n' \ |
| 36 | + > "$MOCK_BIN/curl" |
| 37 | + chmod +x "$MOCK_BIN/curl" |
| 38 | +} |
| 39 | + |
| 40 | +# ─── validate_slug ──────────────────────────────────────────────────────────── |
| 41 | + |
| 42 | +@test "validate_slug: alphanumeric slug passes" { |
| 43 | + run bash -c "GITHUB_TOKEN=x source '${LIB_PATH}' 2>/dev/null; validate_slug 'myrepo123' 'repo'" |
| 44 | + [ "$status" -eq 0 ] |
| 45 | +} |
| 46 | + |
| 47 | +@test "validate_slug: slug with hyphens passes" { |
| 48 | + run bash -c "GITHUB_TOKEN=x source '${LIB_PATH}' 2>/dev/null; validate_slug 'my-org-repo' 'repo'" |
| 49 | + [ "$status" -eq 0 ] |
| 50 | +} |
| 51 | + |
| 52 | +@test "validate_slug: slug with underscores passes" { |
| 53 | + run bash -c "GITHUB_TOKEN=x source '${LIB_PATH}' 2>/dev/null; validate_slug 'my_repo_name' 'repo'" |
| 54 | + [ "$status" -eq 0 ] |
| 55 | +} |
| 56 | + |
| 57 | +@test "validate_slug: space in slug exits 1" { |
| 58 | + run bash -c "GITHUB_TOKEN=x source '${LIB_PATH}' 2>/dev/null; validate_slug 'my repo' 'repo'" |
| 59 | + [ "$status" -eq 1 ] |
| 60 | +} |
| 61 | + |
| 62 | +@test "validate_slug: slash in slug exits 1" { |
| 63 | + run bash -c "GITHUB_TOKEN=x source '${LIB_PATH}' 2>/dev/null; validate_slug 'org/repo' 'repo'" |
| 64 | + [ "$status" -eq 1 ] |
| 65 | +} |
| 66 | + |
| 67 | +@test "validate_slug: dot in slug exits 1" { |
| 68 | + run bash -c "GITHUB_TOKEN=x source '${LIB_PATH}' 2>/dev/null; validate_slug 'my.repo' 'repo'" |
| 69 | + [ "$status" -eq 1 ] |
| 70 | +} |
| 71 | + |
| 72 | +@test "validate_slug: shell metachar in slug exits 1" { |
| 73 | + run bash -c "GITHUB_TOKEN=x source '${LIB_PATH}' 2>/dev/null; validate_slug 'repo\$(evil)' 'repo'" |
| 74 | + [ "$status" -eq 1 ] |
| 75 | +} |
| 76 | + |
| 77 | +# ─── require_env_var ────────────────────────────────────────────────────────── |
| 78 | + |
| 79 | +@test "require_env_var: exits 1 when variable is unset" { |
| 80 | + run bash -c "GITHUB_TOKEN=x source '${LIB_PATH}' 2>/dev/null; unset MY_VAR; require_env_var MY_VAR" |
| 81 | + [ "$status" -eq 1 ] |
| 82 | +} |
| 83 | + |
| 84 | +@test "require_env_var: exits 1 when variable is empty string" { |
| 85 | + run bash -c "GITHUB_TOKEN=x source '${LIB_PATH}' 2>/dev/null; MY_VAR=''; require_env_var MY_VAR" |
| 86 | + [ "$status" -eq 1 ] |
| 87 | +} |
| 88 | + |
| 89 | +@test "require_env_var: exits 0 when variable has a value" { |
| 90 | + run bash -c "GITHUB_TOKEN=x source '${LIB_PATH}' 2>/dev/null; MY_VAR=hello; require_env_var MY_VAR" |
| 91 | + [ "$status" -eq 0 ] |
| 92 | +} |
| 93 | + |
| 94 | +# ─── require_command ────────────────────────────────────────────────────────── |
| 95 | + |
| 96 | +@test "require_command: exits 0 for an existing command" { |
| 97 | + run bash -c "GITHUB_TOKEN=x source '${LIB_PATH}' 2>/dev/null; require_command bash" |
| 98 | + [ "$status" -eq 0 ] |
| 99 | +} |
| 100 | + |
| 101 | +@test "require_command: exits 1 for a command that does not exist" { |
| 102 | + run bash -c "GITHUB_TOKEN=x source '${LIB_PATH}' 2>/dev/null; require_command __no_such_cmd__" |
| 103 | + [ "$status" -eq 1 ] |
| 104 | +} |
| 105 | + |
| 106 | +# ─── gh_api sentinels ───────────────────────────────────────────────────────── |
| 107 | + |
| 108 | +@test "gh_api: returns __404__ on HTTP 404" { |
| 109 | + _mock_curl 404 |
| 110 | + # Expand ${MOCK_BIN} and ${PATH} now so the subshell inherits full system PATH |
| 111 | + run bash -c "export PATH='${MOCK_BIN}:${PATH}'; export GITHUB_TOKEN=fake; export API_URL_PREFIX=https://api.github.com; source '${LIB_PATH}' 2>/dev/null; gh_api '/orgs/nonexistent'" |
| 112 | + [ "$status" -eq 0 ] |
| 113 | + [ "$output" = "__404__" ] |
| 114 | +} |
| 115 | + |
| 116 | +@test "gh_api: returns __422__ on HTTP 422" { |
| 117 | + _mock_curl 422 |
| 118 | + run bash -c "export PATH='${MOCK_BIN}:${PATH}'; export GITHUB_TOKEN=fake; export API_URL_PREFIX=https://api.github.com; source '${LIB_PATH}' 2>/dev/null; gh_api '/orgs/bad-request'" |
| 119 | + [ "$status" -eq 0 ] |
| 120 | + [ "$output" = "__422__" ] |
| 121 | +} |
| 122 | + |
| 123 | +@test "gh_api: returns body on HTTP 200" { |
| 124 | + _mock_curl 200 '{"login":"test-org"}' |
| 125 | + run bash -c "export PATH='${MOCK_BIN}:${PATH}'; export GITHUB_TOKEN=fake; export API_URL_PREFIX=https://api.github.com; source '${LIB_PATH}' 2>/dev/null; gh_api '/orgs/test-org'" |
| 126 | + [ "$status" -eq 0 ] |
| 127 | + [ "$output" = '{"login":"test-org"}' ] |
| 128 | +} |
0 commit comments