|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Smoke tests for xsh-lib/git utilities. Mirrors xsh-lib/core/test.sh: plain |
| 4 | +# bash assertions, no external test framework. |
| 5 | +# |
| 6 | +# Usage: |
| 7 | +# xsh load xsh-lib/git # one-time |
| 8 | +# bash test.sh # tests the loaded copy |
| 9 | +# |
| 10 | +# Local dev iteration against an unpushed working copy: |
| 11 | +# xsh lib-dev-manager link xsh-lib/git /path/to/workspace |
| 12 | +# XSH_DEV=1 bash test.sh |
| 13 | +# |
| 14 | +# Tests that touch the network or require an authenticated `gh` are skipped |
| 15 | +# unless XSH_GIT_TEST_NETWORK=1 is set explicitly. |
| 16 | +# |
| 17 | + |
| 18 | +set -e -o pipefail |
| 19 | + |
| 20 | +# xsh's __xsh_clean unsets XSH_DEV on every RETURN trap, so a script that makes |
| 21 | +# multiple xsh calls only gets dev-mode for the first one. Capture the initial |
| 22 | +# value once and re-apply it via a wrapper. |
| 23 | +_TEST_XSH_DEV="${XSH_DEV-}" |
| 24 | +_xsh () { XSH_DEV="$_TEST_XSH_DEV" xsh "$@"; } |
| 25 | + |
| 26 | + |
| 27 | +echo "==> xsh list /" |
| 28 | +_xsh list / |
| 29 | + |
| 30 | + |
| 31 | +# ----------------------------------------------------------------------------- |
| 32 | +# git/hub/account-for-email |
| 33 | +# ----------------------------------------------------------------------------- |
| 34 | + |
| 35 | +echo "==> git/hub/account-for-email (hit on first pair)" |
| 36 | +[[ $(XSH_GIT_HUB_ACCOUNT_MAP="a@b=alice c@d=bob" \ |
| 37 | + _xsh git/hub/account-for-email a@b) == alice ]] |
| 38 | + |
| 39 | +echo "==> git/hub/account-for-email (hit on later pair)" |
| 40 | +[[ $(XSH_GIT_HUB_ACCOUNT_MAP="a@b=alice c@d=bob" \ |
| 41 | + _xsh git/hub/account-for-email c@d) == bob ]] |
| 42 | + |
| 43 | +echo "==> git/hub/account-for-email (miss returns 1)" |
| 44 | +rc=0 |
| 45 | +XSH_GIT_HUB_ACCOUNT_MAP="a@b=alice" \ |
| 46 | + _xsh git/hub/account-for-email nope@x >/dev/null 2>&1 || rc=$? |
| 47 | +[[ $rc -eq 1 ]] |
| 48 | + |
| 49 | +echo "==> git/hub/account-for-email (empty map returns 1)" |
| 50 | +rc=0 |
| 51 | +XSH_GIT_HUB_ACCOUNT_MAP="" \ |
| 52 | + _xsh git/hub/account-for-email anything@x >/dev/null 2>&1 || rc=$? |
| 53 | +[[ $rc -eq 1 ]] |
| 54 | + |
| 55 | +echo "==> git/hub/account-for-email (account value containing '=')" |
| 56 | +[[ $(XSH_GIT_HUB_ACCOUNT_MAP="weird@e=acct=with=eq" \ |
| 57 | + _xsh git/hub/account-for-email weird@e) == "acct=with=eq" ]] |
| 58 | + |
| 59 | + |
| 60 | +# ----------------------------------------------------------------------------- |
| 61 | +# git/hub/account-for-repo |
| 62 | +# ----------------------------------------------------------------------------- |
| 63 | + |
| 64 | +echo "==> git/hub/account-for-repo (derives from local repo's user.email)" |
| 65 | +tmprepo=$(mktemp -d "${TMPDIR:-/tmp}/xsh-git-test.XXXXXXXX") |
| 66 | +trap 'rm -rf "$tmprepo"' EXIT |
| 67 | +( |
| 68 | + cd "$tmprepo" |
| 69 | + git init -q |
| 70 | + git config user.email "a@b" |
| 71 | + export XSH_GIT_HUB_ACCOUNT_MAP="a@b=alice" |
| 72 | + [[ $(_xsh git/hub/account-for-repo) == alice ]] |
| 73 | +) |
| 74 | + |
| 75 | +echo "==> git/hub/account-for-repo (outside repo / no user.email returns 1)" |
| 76 | +rc=0 |
| 77 | +( |
| 78 | + cd "${TMPDIR:-/tmp}" |
| 79 | + GIT_CONFIG_NOSYSTEM=1 HOME="$tmprepo" \ |
| 80 | + _xsh git/hub/account-for-repo >/dev/null 2>&1 |
| 81 | +) || rc=$? |
| 82 | +[[ $rc -eq 1 ]] |
| 83 | + |
| 84 | +echo "==> git/hub/account-for-repo (unmapped email returns 1)" |
| 85 | +rc=0 |
| 86 | +( |
| 87 | + cd "$tmprepo" |
| 88 | + git config user.email "unknown@x" |
| 89 | + XSH_GIT_HUB_ACCOUNT_MAP="a@b=alice" \ |
| 90 | + _xsh git/hub/account-for-repo >/dev/null 2>&1 |
| 91 | +) || rc=$? |
| 92 | +[[ $rc -eq 1 ]] |
| 93 | + |
| 94 | + |
| 95 | +# ----------------------------------------------------------------------------- |
| 96 | +# git/hub/run |
| 97 | +# ----------------------------------------------------------------------------- |
| 98 | + |
| 99 | +echo "==> git/hub/run (no command after -- returns 2)" |
| 100 | +rc=0 |
| 101 | +_xsh git/hub/run -u dummy -- >/dev/null 2>&1 || rc=$? |
| 102 | +[[ $rc -eq 2 ]] |
| 103 | + |
| 104 | +echo "==> git/hub/run (unknown option returns 2)" |
| 105 | +rc=0 |
| 106 | +_xsh git/hub/run --bogus -- echo x >/dev/null 2>&1 || rc=$? |
| 107 | +[[ $rc -eq 2 ]] |
| 108 | + |
| 109 | +echo "==> git/hub/run (missing gh config dir returns 1)" |
| 110 | +rc=0 |
| 111 | +GH_CONFIG_DIR="${TMPDIR:-/tmp}/xsh-git-no-such-dir-$$" \ |
| 112 | + _xsh git/hub/run -u dummy -- echo x >/dev/null 2>&1 || rc=$? |
| 113 | +[[ $rc -eq 1 ]] |
| 114 | + |
| 115 | +# Happy path needs a logged-in gh account; opt-in only. |
| 116 | +if [[ "${XSH_GIT_TEST_NETWORK:-}" == "1" ]] && command -v gh >/dev/null 2>&1; then |
| 117 | + echo "==> git/hub/run (happy path: round-trip gh api user)" |
| 118 | + acct=$(gh auth status 2>&1 \ |
| 119 | + | awk '/Logged in to github.com account/{print $7; exit}') |
| 120 | + if [[ -n $acct ]]; then |
| 121 | + [[ $(_xsh git/hub/run -u "$acct" -- gh api user --jq .login) == "$acct" ]] |
| 122 | + else |
| 123 | + echo " (skipped: no gh account logged in)" |
| 124 | + fi |
| 125 | +else |
| 126 | + echo "==> git/hub/run (happy path skipped — set XSH_GIT_TEST_NETWORK=1 to enable)" |
| 127 | +fi |
| 128 | + |
| 129 | +echo |
| 130 | +echo "All tests passed." |
0 commit comments