notices.test.mjs 1.8 KB

123456789101112131415161718192021222324252627282930
  1. import { test } from 'node:test'
  2. import assert from 'node:assert/strict'
  3. import fs from 'node:fs'
  4. import path from 'node:path'
  5. import os from 'node:os'
  6. import { writeNotices } from '../notices.mjs'
  7. test('notices find the named owner above a nested package.json and preserve original text', () => {
  8. const root = fs.mkdtempSync(path.join(os.tmpdir(), 'scriptor-notices-'))
  9. const dependency = path.join(root, 'node_modules/example')
  10. const output = path.join(root, 'output')
  11. const input = path.join(dependency, 'dist/index.js')
  12. fs.mkdirSync(path.dirname(input), { recursive: true })
  13. fs.mkdirSync(path.join(output, 'lib'), { recursive: true })
  14. try {
  15. fs.writeFileSync(path.join(dependency, 'package.json'), JSON.stringify({ name: 'example', version: '1.0.0', license: 'MIT' }))
  16. fs.writeFileSync(path.join(dependency, 'LICENSE'), 'Original copyright and permission text')
  17. fs.writeFileSync(path.join(dependency, 'dist/package.json'), '{"type":"module"}')
  18. fs.writeFileSync(input, 'export const value = 1')
  19. const builds = [{ metafile: { inputs: { [input]: {} } } }]
  20. writeNotices(output, builds)
  21. assert.ok(fs.readFileSync(path.join(output, 'licenses/example--1.0.0.txt'), 'utf8').includes('Original copyright and permission text'))
  22. fs.writeFileSync(path.join(dependency, 'package.json'), JSON.stringify({ name: 'example', version: '1.0.0', license: 'UNLICENSED' }))
  23. assert.throws(() => writeNotices(output, builds), /Review bundled license/)
  24. } finally {
  25. const remove = directory => { for (const entry of fs.readdirSync(directory, { withFileTypes: true })) { const target = path.join(directory, entry.name); if (entry.isDirectory()) remove(target); else fs.unlinkSync(target) } fs.rmdirSync(directory) }
  26. assert.ok(path.resolve(root).startsWith(path.resolve(os.tmpdir()) + path.sep))
  27. remove(root)
  28. }
  29. })