From 4c6226e3693ab0dff33f9c562f5bda77fe42ccee Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sat, 16 May 2026 17:06:16 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AA=20Add=20isolated=20tests=20for=20g?= =?UTF-8?q?ithub-pat=20token=20selection=20functions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit adds a new integration test script `test/_global/github-tokens-functions.sh` that thoroughly exercises the `get_best_token` and `get_elevated_token` functions in `src/github-tokens/cmd/github-pat`. The tests use subshells to isolate environment variables and verify the priority and fallback logic for GITHUB_PAT, GH_TOKEN, and GITHUB_TOKEN. A new scenario `github-tokens-functions` has been added to `test/_global/scenarios.json` to include these tests in the global test suite. Co-authored-by: MiguelRodo <23501332+MiguelRodo@users.noreply.github.com> --- test/_global/github-tokens-functions.sh | 69 +++++++++++++++++++++++++ test/_global/scenarios.json | 6 +++ 2 files changed, 75 insertions(+) create mode 100644 test/_global/github-tokens-functions.sh diff --git a/test/_global/github-tokens-functions.sh b/test/_global/github-tokens-functions.sh new file mode 100644 index 0000000..b2cef86 --- /dev/null +++ b/test/_global/github-tokens-functions.sh @@ -0,0 +1,69 @@ +#!/bin/bash + +set -e + +# Optional: Import test library bundled with the devcontainer CLI +source dev-container-features-test-lib + +echo "🧪 Testing github-tokens internal functions (get_best_token, get_elevated_token)" + +# Path to the script we are testing +GITHUB_PAT_SCRIPT="/usr/local/bin/github-tokens-github-pat" + +# Function to run a test case in a subshell +run_test() { + local label="$1" + local env_setup="$2" + local func_to_test="$3" + local expected_output="$4" + + # We use a subshell to isolate the test case and prevent environment leakage + check "$label" bash -c " + $env_setup + source $GITHUB_PAT_SCRIPT > /dev/null 2>&1 + result=\$($func_to_test) + if [ \"\$result\" = \"$expected_output\" ]; then + exit 0 + else + echo \"Expected '$expected_output' but got '\$result'\" + exit 1 + fi + " +} + +# --- Tests for get_best_token --- +# Priority: GITHUB_PAT > GH_TOKEN > GITHUB_TOKEN + +run_test "get_best_token: GITHUB_PAT has highest priority" \ + "export GITHUB_PAT=pat_val; export GH_TOKEN=gh_val; export GITHUB_TOKEN=gt_val" \ + "get_best_token" "pat_val" + +run_test "get_best_token: GH_TOKEN fallback" \ + "unset GITHUB_PAT; export GH_TOKEN=gh_val; export GITHUB_TOKEN=gt_val" \ + "get_best_token" "gh_val" + +run_test "get_best_token: GITHUB_TOKEN fallback" \ + "unset GITHUB_PAT; unset GH_TOKEN; export GITHUB_TOKEN=gt_val" \ + "get_best_token" "gt_val" + +run_test "get_best_token: empty if none set" \ + "unset GITHUB_PAT; unset GH_TOKEN; unset GITHUB_TOKEN" \ + "get_best_token" "" + +# --- Tests for get_elevated_token --- +# Priority: GITHUB_PAT > GH_TOKEN (excludes GITHUB_TOKEN) + +run_test "get_elevated_token: GITHUB_PAT priority" \ + "export GITHUB_PAT=pat_val; export GH_TOKEN=gh_val; export GITHUB_TOKEN=gt_val" \ + "get_elevated_token" "pat_val" + +run_test "get_elevated_token: GH_TOKEN priority" \ + "unset GITHUB_PAT; export GH_TOKEN=gh_val; export GITHUB_TOKEN=gt_val" \ + "get_elevated_token" "gh_val" + +run_test "get_elevated_token: empty if GITHUB_TOKEN is the only one set" \ + "unset GITHUB_PAT; unset GH_TOKEN; export GITHUB_TOKEN=gt_val" \ + "get_elevated_token" "" + +# Report result +reportResults diff --git a/test/_global/scenarios.json b/test/_global/scenarios.json index 44544e6..6162888 100644 --- a/test/_global/scenarios.json +++ b/test/_global/scenarios.json @@ -57,6 +57,12 @@ "GH_TOKEN": "ghp_permissive_token" } }, + "github-tokens-functions": { + "image": "mcr.microsoft.com/devcontainers/base:ubuntu", + "features": { + "github-tokens": {} + } + }, "github-tokens-override": { "image": "mcr.microsoft.com/devcontainers/base:ubuntu", "features": {