publint-all.spec.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
  2. import { tmpdir } from 'node:os'
  3. import { join } from 'node:path'
  4. import { fileURLToPath } from 'node:url'
  5. import { spawnSync } from 'node:child_process'
  6. import { afterEach, describe, expect, it } from 'vitest'
  7. const repositoryRoot = fileURLToPath(new URL('..', import.meta.url))
  8. const runner = fileURLToPath(new URL('./publint-all.ts', import.meta.url))
  9. const roots: string[] = []
  10. afterEach(() => {
  11. for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true })
  12. })
  13. function fixture(exportPath = './lib/index.js'): string {
  14. const root = mkdtempSync(join(tmpdir(), 'dsh-publint-all-'))
  15. roots.push(root)
  16. const packageDir = join(root, 'packages/core/probe')
  17. mkdirSync(join(packageDir, 'lib'), { recursive: true })
  18. writeFileSync(join(packageDir, 'package.json'), `${JSON.stringify({
  19. name: '@deepseek-ai/dsh-probe',
  20. version: '0.0.1',
  21. type: 'module',
  22. license: 'MIT',
  23. engines: { node: '>=22.19' },
  24. sideEffects: false,
  25. files: ['lib'],
  26. exports: { '.': { default: exportPath } },
  27. }, null, 2)}\n`)
  28. writeFileSync(join(packageDir, 'README.md'), '# Probe\n')
  29. writeFileSync(join(packageDir, 'lib/index.js'), 'export const probe = true\n')
  30. writeFileSync(join(packageDir, 'unpublished.js'), 'export const hidden = true\n')
  31. return root
  32. }
  33. function run(root: string) {
  34. return spawnSync(process.execPath, [
  35. '--import', 'tsx', runner,
  36. '--packages-root', root,
  37. ], {
  38. cwd: repositoryRoot,
  39. encoding: 'utf8',
  40. timeout: 5_000,
  41. })
  42. }
  43. describe('publint package runner', () => {
  44. it('lints recursively declared files from an in-memory publication view', () => {
  45. const result = run(fixture())
  46. expect(result.status, result.stderr).toBe(0)
  47. expect(result.stdout).toContain('linting 1 package(s)')
  48. expect(result.stdout).toContain('All good!')
  49. })
  50. it('rejects an export that exists in the workspace but is not published', () => {
  51. const result = run(fixture('./unpublished.js'))
  52. expect(result.status).toBe(1)
  53. expect(result.stdout).toContain('unpublished.js')
  54. })
  55. })