From 66039975acfee66c291927b6d47bf937a8fa4422 Mon Sep 17 00:00:00 2001 From: jdalton Date: Wed, 2 Dec 2026 09:56:28 -0500 Subject: [PATCH] test(ci): cover inlined workflows --- .config/repo/vitest.local.mts | 6 +- test/.github/workflows/ci-gates.test.mts | 91 ++++++++++++++++++++++++ test/scripts/ci/setup-node.test.mts | 74 +++++++++++++++++++ 3 files changed, 170 insertions(+), 1 deletion(-) create mode 100644 test/.github/workflows/ci-gates.test.mts create mode 100644 test/scripts/ci/setup-node.test.mts diff --git a/.config/repo/vitest.local.mts b/.config/repo/vitest.local.mts index 72dae9ca..d76abacf 100644 --- a/.config/repo/vitest.local.mts +++ b/.config/repo/vitest.local.mts @@ -11,7 +11,11 @@ export default defineConfig({ }, test: { setupFiles: [path.join(root, 'test/repo/vitest.setup.mts')], - include: ['test/repo/unit/**/*.test.mts'], + include: [ + 'test/repo/unit/**/*.test.mts', + 'test/scripts/**/*.test.mts', + 'test/.github/workflows/ci-gates.test.mts', + ], testTimeout: 15_000, hookTimeout: 15_000, pool: 'forks', diff --git a/test/.github/workflows/ci-gates.test.mts b/test/.github/workflows/ci-gates.test.mts new file mode 100644 index 00000000..f17e70ea --- /dev/null +++ b/test/.github/workflows/ci-gates.test.mts @@ -0,0 +1,91 @@ +import { execFileSync } from 'node:child_process' +import { mkdir, mkdtemp, readFile, rm } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import path from 'node:path' +import { pathToFileURL } from 'node:url' + +import { expect, it } from 'vitest' +import { parse } from 'yaml' + +interface Workflow { + jobs: Record< + string, + { steps: Array<{ name?: string; run?: string; uses?: string }> } + > +} + +const source = new URL( + '../../../.github/workflows/ci-gates.yml', + import.meta.url, +) + +function git(cwd: string, ...args: string[]): string { + return execFileSync('git', args, { + cwd, + encoding: 'utf8', + stdio: ['ignore', 'pipe', 'pipe'], + }).trim() +} + +it('checks out the full CI event revision and tags without persisting credentials', async () => { + const { jobs } = parse(await readFile(source, 'utf8')) as Workflow + const check = jobs['check'].steps.find(step => step.name === 'Checkout') + const test = jobs['test'].steps.find(step => step.name === 'Checkout') + expect(check?.run).toBeTruthy() + expect(check?.run).not.toBe(test?.run) + + const root = await mkdtemp(path.join(tmpdir(), 'vscode-ci-checkout-test-')) + try { + const remote = path.join(root, 'fixture.git') + const repository = path.join(root, 'repository') + const checkout = path.join(root, 'checkout') + await mkdir(repository) + await mkdir(checkout) + git(root, 'init', '--bare', '-q', '--initial-branch=main', remote) + git(repository, 'init', '-q', '-b', 'main') + git(repository, 'config', 'user.name', 'CI fixture') + git(repository, 'config', 'user.email', 'fixture@example.invalid') + git(repository, 'config', 'commit.gpgsign', 'false') + git(repository, 'commit', '--allow-empty', '-qm', 'tagged parent') + git(repository, 'tag', 'v1.0.0') + git(repository, 'switch', '-c', 'event') + git(repository, 'commit', '--allow-empty', '-qm', 'event revision') + const sha = git(repository, 'rev-parse', 'HEAD') + git(repository, 'switch', 'main') + git(repository, 'commit', '--allow-empty', '-qm', 'newer main revision') + git(repository, 'remote', 'add', 'origin', remote) + git(repository, 'push', '-q', 'origin', 'main', '--tags') + // GitHub advertises the event revision through a pull request ref. + git(repository, 'push', '-q', 'origin', `${sha}:refs/pull/1/merge`) + + const bash = + process.platform === 'win32' + ? path.join( + process.env['ProgramFiles'] ?? 'C:\\Program Files', + 'Git', + 'bin', + 'bash.exe', + ) + : 'bash' + execFileSync(bash, ['-c', check!.run!], { + cwd: checkout, + env: { + ...process.env, + CHECKOUT_TOKEN: 'fixture-token', + GITHUB_SERVER_URL: pathToFileURL(root).href, + GITHUB_REPOSITORY: 'fixture', + GITHUB_SHA: sha, + }, + stdio: 'pipe', + }) + + expect(git(checkout, 'rev-parse', 'HEAD')).toBe(sha) + expect(git(checkout, 'rev-list', '--count', 'HEAD')).toBe('2') + expect(git(checkout, 'tag', '-l')).toBe('v1.0.0') + expect( + await readFile(path.join(checkout, '.git', 'config'), 'utf8'), + ).not.toMatch(/extraheader|fixture-token/iu) + } finally { + await rm(root, { recursive: true, force: true }) + } +}) diff --git a/test/scripts/ci/setup-node.test.mts b/test/scripts/ci/setup-node.test.mts new file mode 100644 index 00000000..e23e8e8d --- /dev/null +++ b/test/scripts/ci/setup-node.test.mts @@ -0,0 +1,74 @@ +import { execFileSync } from 'node:child_process' +import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises' +import os from 'node:os' +import path from 'node:path' +import process from 'node:process' +import { afterEach, describe, expect, it } from 'vitest' + +const script = path.resolve('scripts/ci/setup-node.mjs') +const version = process.version.slice(1) +const temporaryDirectories: string[] = [] + +async function fixture() { + const root = await mkdtemp(path.join(os.tmpdir(), 'socket-setup-node-test-')) + temporaryDirectories.push(root) + const cwd = path.join(root, 'workspace') + const runnerTemp = path.join(root, 'runner-temp') + const githubPath = path.join(root, 'github-path') + await mkdir(cwd) + await writeFile(path.join(cwd, '.node-version'), `${version}\n`) + return { cwd, githubPath, root, runnerTemp } +} + +afterEach(async () => { + await Promise.all( + temporaryDirectories + .splice(0) + .map(directory => rm(directory, { force: true, recursive: true })), + ) +}) + +describe('scripts/ci/setup-node.mjs', () => { + it('uses the exact running Node installation without downloading', async () => { + const { cwd, githubPath, runnerTemp } = await fixture() + const output = execFileSync(process.execPath, [script], { + cwd, + encoding: 'utf8', + env: { + ...process.env, + GITHUB_PATH: githubPath, + RUNNER_TEMP: runnerTemp, + }, + }) + + expect(output).toContain(`ready with Node.js ${version}`) + expect(await readFile(githubPath, 'utf8')).toBe( + `${path.dirname(process.execPath)}${os.EOL}`, + ) + }) + + it('rejects a non exact version pin before attempting network access', async () => { + const { cwd, githubPath, runnerTemp } = await fixture() + await writeFile(path.join(cwd, '.node-version'), 'v26\n') + let error: { stderr?: Buffer; status?: number } + try { + execFileSync(process.execPath, [script], { + cwd, + encoding: 'utf8', + env: { + ...process.env, + GITHUB_PATH: githubPath, + RUNNER_TEMP: runnerTemp, + }, + }) + throw new Error('expected setup-node to reject the pin') + } catch (caught) { + error = caught as { stderr?: Buffer; status?: number } + } + + expect(error.status).toBe(1) + expect(error.stderr?.toString()).toContain( + '.node-version must contain an exact X.Y.Z version', + ) + }) +})