OverviewFilesCommitsIssuesDocsReleasesPeers

pgh / test / publish-ignore.test.js

62 lines · javascript ref:
import test from 'brittle'
import fs from '#node:fs'
import path from '#node:path'
import os from '#node:os'

import { computeLockfile } from '../lib/publish.js'
import { walkTree } from 'hyper-overlay/tree'
import { createMatcher } from 'hyper-overlay/ignore'

function mktemp (prefix) {
  return fs.mkdtempSync(path.join(os.tmpdir(), prefix + '-'))
}

test('publishIgnore — excludes patterns from rootTree hash', async (t) => {
  const dir = mktemp('pgh-pubignore')
  fs.mkdirSync(path.join(dir, 'src'))
  fs.mkdirSync(path.join(dir, 'captures'))
  fs.writeFileSync(path.join(dir, 'src', 'index.js'), 'console.log(1)')
  fs.writeFileSync(path.join(dir, 'captures', 'big.bin'), Buffer.alloc(100000))
  fs.writeFileSync(path.join(dir, 'README.md'), '# app')
  fs.writeFileSync(path.join(dir, 'package.json'), JSON.stringify({
    name: 'test-app', version: '1.0.0',
    pgh: { runtime: 'node', publishIgnore: ['captures', 'captures/**', '*.bin'] }
  }))

  const lockfile = await computeLockfile(dir)

  // Re-walk with the same exclusions — should match lockfile hash
  const matcher = createMatcher(['captures', 'captures/**', '*.bin'], { noDenyList: true })
  const trees = walkTree(dir, {
    skip: new Set(['.DS_Store', 'Thumbs.db', '.pgh-lock.json', '.git', '.pear-git', '.gitignore', 'package-lock.json', 'node_modules']),
    matcher
  })
  t.is(lockfile.rootTreeHash, trees.get('').hash, 'rootTreeHash matches re-walk with same exclusions')

  // Without publishIgnore, hash would differ
  const trees2 = walkTree(dir, {
    skip: new Set(['.DS_Store', 'Thumbs.db', '.pgh-lock.json', '.git', '.pear-git', '.gitignore', 'package-lock.json', 'node_modules'])
  })
  t.absent(lockfile.rootTreeHash === trees2.get('').hash, 'rootTreeHash differs from walk WITHOUT publishIgnore')

  // captures/ should not appear in root tree entries
  const rootEntries = trees.get('').entries.map(e => e.name)
  t.absent(rootEntries.includes('captures'), 'captures/ excluded from root')

  fs.rmSync(dir, { recursive: true, force: true })
})

test('publishIgnore — no patterns = no behavior change', async (t) => {
  const dir = mktemp('pgh-pubignore-noop')
  fs.writeFileSync(path.join(dir, 'index.js'), 'x')
  fs.writeFileSync(path.join(dir, 'package.json'), JSON.stringify({
    name: 'test-app', version: '1.0.0', pgh: { runtime: 'node' }
  }))
  const lockfile = await computeLockfile(dir)
  const trees = walkTree(dir, {
    skip: new Set(['.DS_Store', 'Thumbs.db', '.pgh-lock.json', '.git', '.pear-git', '.gitignore', 'package-lock.json', 'node_modules'])
  })
  t.is(lockfile.rootTreeHash, trees.get('').hash, 'identical to plain walk when no publishIgnore configured')
  fs.rmSync(dir, { recursive: true, force: true })
})