11// Plain GitHub CLI helper tests cover wrapper-safe gh execution for maintainer scripts.
22import { spawnSync } from "node:child_process" ;
3- import { chmodSync , mkdirSync , mkdtempSync , readFileSync , rmSync , writeFileSync } from "node:fs" ;
4- import { tmpdir } from "node:os" ;
3+ import { chmodSync , mkdirSync , readFileSync , writeFileSync } from "node:fs" ;
54import path from "node:path" ;
65import { afterEach , describe , expect , it } from "vitest" ;
76import {
@@ -13,18 +12,12 @@ import {
1312 PLAIN_GH_SYSTEM_CANDIDATES ,
1413 resolvePlainGhBin ,
1514} from "../../scripts/lib/plain-gh.mjs" ;
15+ import { useAutoCleanupTempDirTracker } from "../helpers/temp-dir.js" ;
1616
17- const tempDirs : string [ ] = [ ] ;
18-
19- afterEach ( ( ) => {
20- for ( const dir of tempDirs . splice ( 0 ) ) {
21- rmSync ( dir , { recursive : true , force : true } ) ;
22- }
23- } ) ;
17+ const tempDirs = useAutoCleanupTempDirTracker ( afterEach ) ;
2418
2519function makeFakeGh ( ) : string {
26- const dir = mkdtempSync ( path . join ( tmpdir ( ) , "plain-gh-" ) ) ;
27- tempDirs . push ( dir ) ;
20+ const dir = tempDirs . make ( "plain-gh-" ) ;
2821 const binDir = path . join ( dir , "bin" ) ;
2922 mkdirSync ( binDir ) ;
3023 const ghPath = path . join ( binDir , "gh" ) ;
@@ -47,8 +40,7 @@ printf 'GH_TOKEN_SET=%s\\n' "\${GH_TOKEN:+1}"
4740}
4841
4942function makeLargeFakeGh ( ) : string {
50- const dir = mkdtempSync ( path . join ( tmpdir ( ) , "plain-gh-large-" ) ) ;
51- tempDirs . push ( dir ) ;
43+ const dir = tempDirs . make ( "plain-gh-large-" ) ;
5244 const ghPath = path . join ( dir , "gh" ) ;
5345 writeFileSync (
5446 ghPath ,
@@ -61,6 +53,44 @@ process.stdout.write("x".repeat(bytes));
6153 return ghPath ;
6254}
6355
56+ function makeCredentialForwardingGh ( ) {
57+ const dir = tempDirs . make ( "plain-gh-auth-forward-" ) ;
58+ const binDir = path . join ( dir , "bin" ) ;
59+ const calls = path . join ( dir , "calls.log" ) ;
60+ const realGh = path . join ( dir , "real-gh" ) ;
61+ mkdirSync ( binDir ) ;
62+ writeFileSync (
63+ path . join ( binDir , "gh" ) ,
64+ `#!/bin/sh
65+ printf 'path:%s\\n' "$*" >> "$PLAIN_GH_FAKE_CALLS"
66+ if [ "$1 $2" = "auth token" ]; then
67+ printf 'forwarded-test-token\\n'
68+ exit 0
69+ fi
70+ exit 9
71+ ` ,
72+ ) ;
73+ writeFileSync (
74+ realGh ,
75+ `#!/bin/sh
76+ printf 'plain:%s\\n' "$*" >> "$PLAIN_GH_FAKE_CALLS"
77+ case "$PLAIN_GH_EXPECTED_TOKEN_ENV" in
78+ GH_TOKEN) token="\${GH_TOKEN-}"; other_token="\${GH_ENTERPRISE_TOKEN-}" ;;
79+ GH_ENTERPRISE_TOKEN) token="\${GH_ENTERPRISE_TOKEN-}"; other_token="\${GH_TOKEN-}" ;;
80+ *) echo 'unexpected token environment' >&2; exit 7 ;;
81+ esac
82+ if [ "$token" != "forwarded-test-token" ] || [ -n "$other_token" ]; then
83+ echo 'missing forwarded credentials' >&2
84+ exit 8
85+ fi
86+ printf 'authenticated plain gh\\n'
87+ ` ,
88+ ) ;
89+ chmodSync ( path . join ( binDir , "gh" ) , 0o755 ) ;
90+ chmodSync ( realGh , 0o755 ) ;
91+ return { binDir, calls, realGh } ;
92+ }
93+
6494describe ( "plain gh helpers" , ( ) => {
6595 it ( "prefers OPENCLAW_GH_BIN over PATH shims" , ( ) => {
6696 const ghPath = makeFakeGh ( ) ;
@@ -203,44 +233,59 @@ describe("plain gh helpers", () => {
203233 expect ( readFileSync ( outputPath , "utf8" ) ) . toContain ( "COLORTERM_SET=" ) ;
204234 } ) ;
205235
206- it ( "bridges PATH-shim credentials to the selected plain gh" , ( ) => {
207- const ghPath = makeFakeGh ( ) ;
208- const shimDir = mkdtempSync ( path . join ( tmpdir ( ) , "plain-gh-auth-shim-" ) ) ;
209- tempDirs . push ( shimDir ) ;
210- const shimPath = path . join ( shimDir , "gh" ) ;
211- writeFileSync (
212- shimPath ,
213- `#!/usr/bin/env bash
214- if [ "$*" = "auth token" ]; then
215- printf 'bridged-test-token\\n'
216- exit 0
217- fi
218- exit 2
219- ` ,
220- ) ;
221- chmodSync ( shimPath , 0o755 ) ;
222- const script = [
223- "set -euo pipefail" ,
224- "source scripts/lib/plain-gh.sh" ,
225- `OPENCLAW_GH_BIN=${ JSON . stringify ( ghPath ) } ` ,
226- "export OPENCLAW_GH_BIN" ,
227- "gh_plain api rate_limit" ,
228- ] . join ( "\n" ) ;
236+ it . each ( [
237+ {
238+ host : undefined ,
239+ name : "github.com" ,
240+ tokenArgs : "auth token" ,
241+ tokenEnv : "GH_TOKEN" ,
242+ } ,
243+ {
244+ host : "github.example.com" ,
245+ name : "an Enterprise host" ,
246+ tokenArgs : "auth token --hostname github.example.com" ,
247+ tokenEnv : "GH_ENTERPRISE_TOKEN" ,
248+ } ,
249+ ] ) ( "forwards $name credentials from a PATH wrapper to the plain CLI" , ( testCase ) => {
250+ const fixture = makeCredentialForwardingGh ( ) ;
251+ const env : NodeJS . ProcessEnv = {
252+ ...process . env ,
253+ OPENCLAW_GH_BIN : fixture . realGh ,
254+ PATH : `${ fixture . binDir } ${ path . delimiter } ${ process . env . PATH ?? "" } ` ,
255+ PLAIN_GH_EXPECTED_TOKEN_ENV : testCase . tokenEnv ,
256+ PLAIN_GH_FAKE_CALLS : fixture . calls ,
257+ } ;
258+ if ( testCase . host ) {
259+ env . GH_HOST = testCase . host ;
260+ } else {
261+ delete env . GH_HOST ;
262+ }
263+ for ( const name of [
264+ "GH_TOKEN" ,
265+ "GITHUB_TOKEN" ,
266+ "GH_ENTERPRISE_TOKEN" ,
267+ "GITHUB_ENTERPRISE_TOKEN" ,
268+ ] ) {
269+ delete env [ name ] ;
270+ }
229271
230- const result = spawnSync ( "bash" , [ "-c" , script ] , {
272+ expect ( execPlainGh ( [ "api" , "user" ] , { encoding : "utf8" , env } ) ) . toBe (
273+ "authenticated plain gh\n" ,
274+ ) ;
275+ const shell = spawnSync ( "bash" , [ "-c" , "source scripts/lib/plain-gh.sh; gh_plain api user" ] , {
276+ cwd : process . cwd ( ) ,
231277 encoding : "utf8" ,
232- env : {
233- ...process . env ,
234- GH_TOKEN : "" ,
235- GITHUB_TOKEN : "" ,
236- PATH : `${ shimDir } ${ path . delimiter } /usr/bin:/bin` ,
237- } ,
278+ env,
238279 } ) ;
239280
240- expect ( result . status , result . stderr ) . toBe ( 0 ) ;
241- expect ( result . stdout ) . toContain ( "argv=api rate_limit" ) ;
242- expect ( result . stdout ) . toContain ( "GH_TOKEN_SET=1" ) ;
243- expect ( result . stdout ) . not . toContain ( "bridged-test-token" ) ;
281+ expect ( shell . status , shell . stderr ) . toBe ( 0 ) ;
282+ expect ( shell . stdout ) . toBe ( "authenticated plain gh\n" ) ;
283+ expect ( readFileSync ( fixture . calls , "utf8" ) . trim ( ) . split ( "\n" ) ) . toEqual ( [
284+ `path:${ testCase . tokenArgs } ` ,
285+ "plain:api user" ,
286+ `path:${ testCase . tokenArgs } ` ,
287+ "plain:api user" ,
288+ ] ) ;
244289 } ) ;
245290
246291 it ( "captures large gh payloads by default" , ( ) => {
@@ -251,6 +296,7 @@ exit 2
251296 encoding : "utf8" ,
252297 env : {
253298 ...process . env ,
299+ GH_TOKEN : "large-payload-test-token" ,
254300 OPENCLAW_GH_BIN : ghPath ,
255301 PLAIN_GH_FAKE_BYTES : String ( bytes ) ,
256302 } ,
0 commit comments