Skip to content

Commit

Permalink
fix: make pack and exec work with git hash refs (npm#7815)
Browse files Browse the repository at this point in the history
Passing arborist constructor to pacote.manifest call because internally
when fetching git deps DirFetcher requires Arborist constructor from
GitFetcher
https://github.com/npm/pacote/blob/main/CHANGELOG.md#1400-pre3-2022-09-28

- [x] Trying to add some tests 

Fixes: npm#6723
  • Loading branch information
milaninfy authored Oct 14, 2024
1 parent 852dd8b commit 7f541e8
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/commands/pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ class Pack extends BaseCommand {
const unicode = this.npm.config.get('unicode')
const json = this.npm.config.get('json')

const Arborist = require('@npmcli/arborist')
// Get the manifests and filenames first so we can bail early on manifest
// errors before making any tarballs
const manifests = []
for (const arg of args) {
const spec = npa(arg)
const manifest = await pacote.manifest(spec, this.npm.flatOptions)
const manifest = await pacote.manifest(spec, { ...this.npm.flatOptions, Arborist })
if (!manifest._id) {
throw new Error('Invalid package, must have name and version')
}
Expand Down
Binary file added test/fixtures/git-test.tgz
Binary file not shown.
24 changes: 24 additions & 0 deletions test/lib/commands/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,27 @@ t.test('npx --no-install @npmcli/npx-test', async t => {
)
}
})

t.test('packs from git spec', async t => {
const spec = 'test/test#111111aaaaaaaabbbbbbbbccccccdddddddeeeee'
const pkgPath = path.resolve(__dirname, '../../fixtures/git-test.tgz')

const srv = MockRegistry.tnock(t, 'https://codeload.github.com')
srv.get('/test/test/tar.gz/111111aaaaaaaabbbbbbbbccccccdddddddeeeee')
.times(2)
.reply(200, await fs.readFile(pkgPath))

const { npm } = await loadMockNpm(t, {
config: {
audit: false,
yes: true,
},
})
try {
await npm.exec('exec', [spec])
const exists = await fs.stat(path.join(npm.prefix, 'npm-exec-test-success'))
t.ok(exists.isFile(), 'bin ran, creating file')
} catch (err) {
t.fail(err, "shouldn't throw")
}
})
2 changes: 1 addition & 1 deletion workspaces/libnpmexec/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const manifests = new Map()

const getManifest = async (spec, flatOptions) => {
if (!manifests.has(spec.raw)) {
const manifest = await pacote.manifest(spec, { ...flatOptions, preferOnline: true })
const manifest = await pacote.manifest(spec, { ...flatOptions, preferOnline: true, Arborist })
manifests.set(spec.raw, manifest)
}
return manifests.get(spec.raw)
Expand Down
2 changes: 1 addition & 1 deletion workspaces/libnpmpack/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async function pack (spec = 'file:.', opts = {}) {
// gets spec
spec = npa(spec)

const manifest = await pacote.manifest(spec, opts)
const manifest = await pacote.manifest(spec, { ...opts, Arborist })

const stdio = opts.foregroundScripts ? 'inherit' : 'pipe'

Expand Down
Binary file added workspaces/libnpmpack/test/fixtures/git-test.tgz
Binary file not shown.
14 changes: 14 additions & 0 deletions workspaces/libnpmpack/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const spawk = tspawk(t)

const fs = require('node:fs')
const path = require('node:path')
const { resolve } = require('node:path')
const pack = require('../lib/index.js')
const tnock = require('./fixtures/tnock.js')

Expand Down Expand Up @@ -133,6 +134,19 @@ t.test('packs from registry spec', async t => {
t.ok(tarball)
})

t.test('packs from git spec', async t => {
const spec = 'test/test#111111aaaaaaaabbbbbbbbccccccdddddddeeeee'
const pkgPath = resolve(__dirname, 'fixtures/git-test.tgz')

const srv = tnock(t, 'https://codeload.github.com')
srv.get('/test/test/tar.gz/111111aaaaaaaabbbbbbbbccccccdddddddeeeee')
.times(2)
.reply(200, fs.readFileSync(pkgPath))

const tarball = await pack(spec, { ...OPTS })
t.ok(tarball)
})

t.test('runs scripts in foreground when foregroundScripts === true', async t => {
const testDir = t.testdir({
'package.json': JSON.stringify({
Expand Down

0 comments on commit 7f541e8

Please sign in to comment.