Skip to content

Commit

Permalink
fix: reify git dependencies that have workspaces (#103)
Browse files Browse the repository at this point in the history
* fix: reify git dependencies that have workspaces

* chore: add tests for workspaces prepare

When finding workspaces data in a package, pacote git fetcher should
extract contents and run prepare scripts as part of that, this test
makes sure that is happening.

Co-authored-by: Ruy Adorno <ruyadorno@hotmail.com>
  • Loading branch information
nlf and ruyadorno committed Feb 16, 2022
1 parent 25eeb97 commit 08348fa
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/git.js
Expand Up @@ -154,12 +154,12 @@ class GitFetcher extends Fetcher {
return this[_readPackageJson](dir + '/package.json').then(mani => {
// no need if we aren't going to do any preparation.
const scripts = mani.scripts
if (!scripts || !(
if (!mani.workspaces && (!scripts || !(
scripts.postinstall ||
scripts.build ||
scripts.preinstall ||
scripts.install ||
scripts.prepare)) {
scripts.prepare))) {
return
}

Expand Down
43 changes: 43 additions & 0 deletions test/git.js
Expand Up @@ -52,6 +52,7 @@ ghi.localhostssh = {
const remote = `git://localhost:${gitPort}/repo`
const remoteHosted = `git://127.0.0.1:${gitPort}/repo`
const submodsRemote = `git://localhost:${gitPort}/submodule-repo`
const workspacesRemote = `git://localhost:${gitPort}/workspaces-repo`

const GitFetcher = require('../lib/git.js')
const t = require('tap')
Expand Down Expand Up @@ -284,6 +285,36 @@ t.test('setup', { bail: true }, t => {
.then(() => git('commit', '-m', 'package'))
})

t.test('create a repo with workspaces', t => {
const repo = resolve(me, 'workspaces-repo')
const wsfolder = resolve(me, 'workspaces-repo/a')
const git = (...cmd) => spawnGit(cmd, { cwd: repo })
const write = (f, c) => fs.writeFileSync(`${repo}/${f}`, c)
mkdirp.sync(wsfolder)
return git('init')
.then(() => git('config', 'user.name', 'pacotedev'))
.then(() => git('config', 'user.email', 'i+pacotedev@github.com'))
.then(() => git('config', 'tag.gpgSign', 'false'))
.then(() => git('config', 'commit.gpgSign', 'false'))
.then(() => git('config', 'tag.forceSignAnnotated', 'false'))
.then(() => write('package.json', JSON.stringify({
name: 'workspaces-root',
version: '1.2.3',
workspaces: ['a'],
})))
.then(() => git('add', 'package.json'))
.then(() => git('commit', '-m', 'package'))
.then(() => write('a/package.json', JSON.stringify({
name: 'a',
version: '1.0.0',
scripts: {
prepare: 'touch foo',
},
})))
.then(() => git('add', 'a/package.json'))
.then(() => git('commit', '-m', 'a/package.json'))
})

t.test('hosted service', t => {
const s = http.createServer((req, res) => {
res.setHeader('connection', 'close')
Expand Down Expand Up @@ -668,3 +699,15 @@ t.test('missing branch name throws pathspec error', async (t) => {
}, domain)
}
})

t.test('simple repo with workspaces', async t => {
const ws = new GitFetcher(workspacesRemote, { cache })
const extract = resolve(me, 'extract-workspaces')
await ws.extract(extract)
// the file ./a/foo does not exist in the original repo
// and should have been created during prepare phase
t.ok(
fs.statSync(me + '/extract-workspaces/a/foo'),
'should run prepare phase when finding workspaces'
)
})

0 comments on commit 08348fa

Please sign in to comment.