Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make pack and exec work with git hash refs #7815

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not the biggest fan of having this tgz file in fixtures, but if I weigh it against the effort required to build it during tests it makes sense.


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')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we're already requiring node:path in line 9.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll correct it as chore in next pr whenever I get chance.

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
Loading