detect-pr-changes.test.ts 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. import { describe, expect, test } from 'bun:test'
  2. import { mkdtemp, mkdir, readFile, rm, writeFile } from 'node:fs/promises'
  3. import { tmpdir } from 'node:os'
  4. import { join } from 'node:path'
  5. import {
  6. classifyChangedPaths,
  7. formatGitHubOutput,
  8. parseChangedPaths,
  9. } from './detect-pr-changes'
  10. const NO_CHANGES = {
  11. 'has-evals': false,
  12. 'has-docs': false,
  13. 'has-workflows': false,
  14. 'has-packages': false,
  15. }
  16. const SCRIPT_PATH = join(import.meta.dir, 'detect-pr-changes.ts')
  17. const WORKFLOW_PATH = join(import.meta.dir, '..', '..', '.github', 'workflows', 'pr-checks.yml')
  18. const PACKAGES_WORKFLOW_PATH = join(import.meta.dir, '..', '..', '.github', 'workflows', 'packages-checks.yml')
  19. type CliResult = {
  20. exitCode: number
  21. stderr: string
  22. stdout: string
  23. }
  24. async function runCli(input: string, outputPath?: string): Promise<CliResult> {
  25. const env = { ...process.env }
  26. if (outputPath === undefined) delete env.GITHUB_OUTPUT
  27. else env.GITHUB_OUTPUT = outputPath
  28. const subprocess = Bun.spawn([process.execPath, 'run', SCRIPT_PATH], {
  29. env,
  30. stderr: 'pipe',
  31. stdin: new Blob([input]),
  32. stdout: 'pipe',
  33. })
  34. const [exitCode, stderr, stdout] = await Promise.all([
  35. subprocess.exited,
  36. new Response(subprocess.stderr).text(),
  37. new Response(subprocess.stdout).text(),
  38. ])
  39. return { exitCode, stderr, stdout }
  40. }
  41. async function withTempDir<T>(run: (directory: string) => Promise<T>): Promise<T> {
  42. const directory = await mkdtemp(join(tmpdir(), 'detect-pr-changes-'))
  43. try {
  44. return await run(directory)
  45. } finally {
  46. await rm(directory, { force: true, recursive: true })
  47. }
  48. }
  49. describe('classifyChangedPaths', () => {
  50. test('detects eval changes', () => {
  51. expect(classifyChangedPaths(['evals/framework/src/index.ts'])).toEqual({
  52. ...NO_CHANGES,
  53. 'has-evals': true,
  54. })
  55. })
  56. test('detects docs changes', () => {
  57. expect(classifyChangedPaths(['docs/maintenance/guide.md'])).toEqual({
  58. ...NO_CHANGES,
  59. 'has-docs': true,
  60. })
  61. })
  62. test('detects workflow changes', () => {
  63. expect(classifyChangedPaths(['.github/workflows/pr-checks.yml'])).toEqual({
  64. ...NO_CHANGES,
  65. 'has-workflows': true,
  66. })
  67. })
  68. test('detects packages changes', () => {
  69. expect(classifyChangedPaths(['packages/cli/src/index.ts'])).toEqual({
  70. ...NO_CHANGES,
  71. 'has-packages': true,
  72. })
  73. expect(classifyChangedPaths(['packages/compatibility-layer/package.json'])).toEqual({
  74. ...NO_CHANGES,
  75. 'has-packages': true,
  76. })
  77. expect(classifyChangedPaths(['packages/plugin-abilities/src/index.ts'])).toEqual({
  78. ...NO_CHANGES,
  79. 'has-packages': true,
  80. })
  81. })
  82. test('detects mixed changes', () => {
  83. expect(
  84. classifyChangedPaths([
  85. 'evals/framework/package.json',
  86. 'docs/README.md',
  87. '.github/workflows/release.yml',
  88. 'packages/cli/package.json',
  89. 'src/index.ts',
  90. ]),
  91. ).toEqual({
  92. 'has-evals': true,
  93. 'has-docs': true,
  94. 'has-workflows': true,
  95. 'has-packages': true,
  96. })
  97. })
  98. test('returns false for every category when paths do not match', () => {
  99. expect(classifyChangedPaths(['README.md', 'scripts/check.ts'])).toEqual(NO_CHANGES)
  100. })
  101. test('returns false for every category when input is empty', () => {
  102. expect(classifyChangedPaths([])).toEqual(NO_CHANGES)
  103. })
  104. test('rejects near-prefix paths', () => {
  105. expect(
  106. classifyChangedPaths([
  107. 'evals-old/test.ts',
  108. 'docs.md',
  109. '.github/workflows-old/check.yml',
  110. 'nested/evals/test.ts',
  111. 'packages-old/cli/index.ts',
  112. 'packages.json',
  113. 'nested/packages/cli/index.ts',
  114. ]),
  115. ).toEqual(NO_CHANGES)
  116. })
  117. })
  118. describe('parseChangedPaths', () => {
  119. test('preserves path identity and removes empty NUL records', () => {
  120. expect(parseChangedPaths(' evals/test.ts \0\0docs/雪\n$HOME; file.md\0')).toEqual([
  121. ' evals/test.ts ',
  122. 'docs/雪\n$HOME; file.md',
  123. ])
  124. })
  125. })
  126. describe('formatGitHubOutput', () => {
  127. test('formats newline-delimited GitHub outputs', () => {
  128. expect(
  129. formatGitHubOutput({
  130. 'has-evals': true,
  131. 'has-docs': false,
  132. 'has-workflows': true,
  133. 'has-packages': false,
  134. }),
  135. ).toBe('has-evals=true\nhas-docs=false\nhas-workflows=true\nhas-packages=false\n')
  136. })
  137. })
  138. describe('CLI', () => {
  139. test('appends outputs to GITHUB_OUTPUT', async () => {
  140. await withTempDir(async (directory) => {
  141. const outputPath = join(directory, 'github-output')
  142. await writeFile(outputPath, 'existing=value\n')
  143. const result = await runCli('evals/test.ts\0', outputPath)
  144. expect(result).toEqual({ exitCode: 0, stderr: '', stdout: '' })
  145. expect(await readFile(outputPath, 'utf8')).toBe(
  146. 'existing=value\nhas-evals=true\nhas-docs=false\nhas-workflows=false\nhas-packages=false\n',
  147. )
  148. })
  149. })
  150. test('writes false outputs for empty stdin', async () => {
  151. await withTempDir(async (directory) => {
  152. const outputPath = join(directory, 'github-output')
  153. const result = await runCli('', outputPath)
  154. expect(result.exitCode).toBe(0)
  155. expect(await readFile(outputPath, 'utf8')).toBe(
  156. 'has-evals=false\nhas-docs=false\nhas-workflows=false\nhas-packages=false\n',
  157. )
  158. })
  159. })
  160. test('fails clearly when GITHUB_OUTPUT is missing', async () => {
  161. const result = await runCli('evals/test.ts\0')
  162. expect(result.exitCode).not.toBe(0)
  163. expect(result.stderr).toContain('GITHUB_OUTPUT is required but was not set')
  164. })
  165. test('writes an error to stderr and exits nonzero for an unwritable target', async () => {
  166. await withTempDir(async (directory) => {
  167. const outputPath = join(directory, 'output-directory')
  168. await mkdir(outputPath)
  169. const result = await runCli('docs/guide.md\0', outputPath)
  170. expect(result.exitCode).not.toBe(0)
  171. expect(result.stderr).toContain('Unable to append change detection outputs to GITHUB_OUTPUT')
  172. })
  173. })
  174. test('handles NUL-delimited Unicode and metacharacter filenames without changing identity', async () => {
  175. await withTempDir(async (directory) => {
  176. const outputPath = join(directory, 'github-output')
  177. const paths = [
  178. 'evals/ leading and trailing .ts ',
  179. 'docs/雪\n$HOME;$(touch never).md',
  180. '.github/workflows/[check]& weird.yml',
  181. 'packages/cli/src/ spaced 雪.ts',
  182. ]
  183. const result = await runCli(`${paths.join('\0')}\0`, outputPath)
  184. expect(result.exitCode).toBe(0)
  185. expect(await readFile(outputPath, 'utf8')).toBe(
  186. 'has-evals=true\nhas-docs=true\nhas-workflows=true\nhas-packages=true\n',
  187. )
  188. })
  189. })
  190. test('does not trim leading whitespace into a matching path', async () => {
  191. await withTempDir(async (directory) => {
  192. const outputPath = join(directory, 'github-output')
  193. const result = await runCli(' evals/not-under-evals.ts\0', outputPath)
  194. expect(result.exitCode).toBe(0)
  195. expect(await readFile(outputPath, 'utf8')).toContain('has-evals=false\n')
  196. })
  197. })
  198. })
  199. describe('PR checks workflow contract', () => {
  200. test('uses the NUL-delimited detector and exact output names', async () => {
  201. const workflow = await readFile(WORKFLOW_PATH, 'utf8')
  202. expect(workflow).toContain('git diff --name-only -z')
  203. expect(workflow).toMatch(/git diff --name-only -z[^\n]*\|[\s\S]*bun run scripts\/validation\/detect-pr-changes\.ts/)
  204. expect(workflow).toContain('has-evals: ${{ steps.filter.outputs.has-evals }}')
  205. expect(workflow).toContain('has-docs: ${{ steps.filter.outputs.has-docs }}')
  206. expect(workflow).toContain('has-workflows: ${{ steps.filter.outputs.has-workflows }}')
  207. expect(workflow).not.toMatch(/steps\.filter\.outputs\.(evals|docs|workflows)(?:\s|})/)
  208. expect(workflow).toContain('oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6')
  209. expect(workflow).toContain('bun-version: 1.3.14')
  210. })
  211. test('requires successful change detection before reporting overall success', async () => {
  212. const workflow = await readFile(WORKFLOW_PATH, 'utf8')
  213. const overallStatus = workflow.slice(workflow.indexOf('# Overall status'))
  214. expect(overallStatus).toContain('needs.check-changes.result }}" == "success"')
  215. expect(overallStatus).toContain('needs.check-changes.outputs.has-evals }}" != "true"')
  216. expect(overallStatus).toContain('exit 1')
  217. })
  218. })
  219. describe('Packages checks workflow contract', () => {
  220. test('uses the NUL-delimited detector and the has-packages output', async () => {
  221. const workflow = await readFile(PACKAGES_WORKFLOW_PATH, 'utf8')
  222. expect(workflow).toContain('git diff --name-only -z')
  223. expect(workflow).toMatch(/git diff --name-only -z[^\n]*\|[\s\S]*bun run scripts\/validation\/detect-pr-changes\.ts/)
  224. expect(workflow).toContain('has-packages: ${{ steps.filter.outputs.has-packages }}')
  225. expect(workflow).toContain('oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6')
  226. expect(workflow).toContain('bun-version: 1.3.14')
  227. })
  228. test('gates every package check job on the has-packages flag', async () => {
  229. const workflow = await readFile(PACKAGES_WORKFLOW_PATH, 'utf8')
  230. expect(workflow).toContain('cli-checks:')
  231. expect(workflow).toContain('compatibility-layer-checks:')
  232. expect(workflow).toContain('plugin-abilities-checks:')
  233. const gates = workflow.match(/needs\.check-changes\.outputs\.has-packages == 'true'/g) ?? []
  234. expect(gates.length).toBeGreaterThanOrEqual(3)
  235. })
  236. test('triggers on packages/** pull request changes', async () => {
  237. const workflow = await readFile(PACKAGES_WORKFLOW_PATH, 'utf8')
  238. expect(workflow).toContain('pull_request:')
  239. expect(workflow).toContain("- 'packages/**'")
  240. })
  241. })