|
| 1 | +import { chmodSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'; |
| 2 | +import { tmpdir } from 'node:os'; |
| 3 | +import { join } from 'node:path'; |
| 4 | +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; |
| 5 | +import { GhMergeGateway, type RunGh } from '../github/merge.ts'; |
| 6 | +import { GhIssueGateway } from '../github/issues.ts'; |
| 7 | +import { GhPullRequestGateway } from '../github/pull-requests.ts'; |
| 8 | +import { GhAlreadyFixedGateway } from '../github/already-fixed.ts'; |
| 9 | + |
| 10 | +// Every adapter's own runner, not an injected one: these are the runners the server uses. |
| 11 | +const defaultRunners: [string, () => RunGh][] = [ |
| 12 | + ['merge', () => new GhMergeGateway({ repository: 'owner/repo', pullRequest: 1, issue: 1 }).run], |
| 13 | + ['issues', () => new GhIssueGateway('owner/repo').run], |
| 14 | + ['pull requests', () => new GhPullRequestGateway({ repository: 'owner/repo' }).run], |
| 15 | + ['already fixed', () => new GhAlreadyFixedGateway({ repository: 'owner/repo' }).run], |
| 16 | +]; |
| 17 | + |
| 18 | +describe('default gh runners', () => { |
| 19 | + let dir = ''; |
| 20 | + let path: string | undefined; |
| 21 | + beforeAll(() => { |
| 22 | + // A `gh` first on PATH that prints the environment it was given. |
| 23 | + dir = mkdtempSync(join(tmpdir(), 'codeboost-gh-env-')); |
| 24 | + writeFileSync(join(dir, 'gh'), '#!/bin/sh\nexec env\n'); |
| 25 | + chmodSync(join(dir, 'gh'), 0o755); |
| 26 | + path = process.env.PATH; |
| 27 | + process.env.PATH = `${dir}:${path}`; |
| 28 | + process.env.CODEBOOST_UNRELATED_SECRET = 'must-not-reach-gh'; |
| 29 | + }); |
| 30 | + afterAll(() => { |
| 31 | + process.env.PATH = path; |
| 32 | + delete process.env.CODEBOOST_UNRELATED_SECRET; |
| 33 | + rmSync(dir, { recursive: true, force: true }); |
| 34 | + }); |
| 35 | + |
| 36 | + it.each(defaultRunners)('%s: passes only the gh environment', async (_name, runner) => { |
| 37 | + const lines = (await runner()(['api', 'user'])).split('\n'); |
| 38 | + expect(lines).toContain('GH_PROMPT_DISABLED=1'); |
| 39 | + expect(lines).toContain(`PATH=${dir}:${path}`); |
| 40 | + expect(lines.some(line => line.startsWith('CODEBOOST_UNRELATED_SECRET='))).toBe(false); |
| 41 | + }); |
| 42 | +}); |
0 commit comments