|
| 1 | +import { Buffer } from 'node:buffer' |
| 2 | +import { mkdtemp, writeFile } from 'node:fs/promises' |
| 3 | +import { tmpdir } from 'node:os' |
| 4 | +import path from 'node:path' |
| 5 | +import { expect, test, vi } from 'vitest' |
| 6 | +import { fsRemove } from '../../utils/fs.ts' |
| 7 | +import { packTarball } from './index.ts' |
| 8 | + |
| 9 | +const GZIP_MAGIC = Buffer.from([0x1f, 0x8b]) |
| 10 | + |
| 11 | +async function writeTempPackage() { |
| 12 | + const dir = await mkdtemp(path.join(tmpdir(), 'tsdown-pack-fixture-')) |
| 13 | + await Promise.all([ |
| 14 | + writeFile( |
| 15 | + path.join(dir, 'package.json'), |
| 16 | + JSON.stringify({ |
| 17 | + name: 'tsdown-pack-fixture', |
| 18 | + version: '0.0.0', |
| 19 | + files: ['index.js'], |
| 20 | + }), |
| 21 | + ), |
| 22 | + // forces `npm` as the detected package manager |
| 23 | + writeFile( |
| 24 | + path.join(dir, 'package-lock.json'), |
| 25 | + JSON.stringify({ lockfileVersion: 3 }), |
| 26 | + ), |
| 27 | + writeFile(path.join(dir, 'index.js'), 'module.exports = 1\n'), |
| 28 | + ]) |
| 29 | + return dir |
| 30 | +} |
| 31 | + |
| 32 | +test('packs a tarball', async () => { |
| 33 | + const dir = await writeTempPackage() |
| 34 | + try { |
| 35 | + const tarball = await packTarball(path.join(dir, 'package.json')) |
| 36 | + expect(tarball.subarray(0, 2)).toEqual(GZIP_MAGIC) |
| 37 | + } finally { |
| 38 | + await fsRemove(dir) |
| 39 | + } |
| 40 | +}) |
| 41 | + |
| 42 | +test('packs a tarball inside `npm publish --dry-run`', async () => { |
| 43 | + vi.stubEnv('npm_config_dry_run', 'true') |
| 44 | + const dir = await writeTempPackage() |
| 45 | + try { |
| 46 | + const tarball = await packTarball(path.join(dir, 'package.json')) |
| 47 | + expect(tarball.subarray(0, 2)).toEqual(GZIP_MAGIC) |
| 48 | + } finally { |
| 49 | + await fsRemove(dir) |
| 50 | + vi.unstubAllEnvs() |
| 51 | + } |
| 52 | +}) |
0 commit comments