Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .config/repo/vitest.local.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
91 changes: 91 additions & 0 deletions test/.github/workflows/ci-gates.test.mts
Original file line number Diff line number Diff line change
@@ -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',
})
Comment thread
jdalton marked this conversation as resolved.
Resolved

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 })
}
})
74 changes: 74 additions & 0 deletions test/scripts/ci/setup-node.test.mts
Original file line number Diff line number Diff line change
@@ -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',
)
})
})
Loading