Repository navigation
test(ci): cover inlined workflows #87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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', | ||
| }) | ||
|
|
||
| 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 }) | ||
| } | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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', | ||
| ) | ||
| }) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.