Skip to content
This repository has been archived by the owner on Aug 11, 2022. It is now read-only.

Commit

Permalink
pkglock: add from field back into git dependencies (#20384)
Browse files Browse the repository at this point in the history
PR-URL: #20384
Credit: @zkat
Reviewed-By: @iarna
  • Loading branch information
zkat committed Apr 20, 2018
1 parent 833046e commit 820f74a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
3 changes: 3 additions & 0 deletions lib/shrinkwrap.js
Expand Up @@ -113,6 +113,9 @@ function shrinkwrapDeps (deps, top, tree, seen) {
var pkginfo = deps[moduleName(child)] = {}
var requested = child.package._requested || getRequested(child) || {}
pkginfo.version = childVersion(top, child, requested)
if (requested.type === 'git' && child.package._from) {
pkginfo.from = child.package._from
}
if (child.fromBundle || child.isInLink) {
pkginfo.bundled = true
} else {
Expand Down
23 changes: 18 additions & 5 deletions test/tap/install-shrinkwrapped-git.js
@@ -1,3 +1,5 @@
'use strict'

var fs = require('fs')
var path = require('path')
var resolve = path.resolve
Expand Down Expand Up @@ -47,20 +49,30 @@ test('shrinkwrapped git dependency got updated', function (t) {
t.comment('test for https://github.com/npm/npm/issues/12718')

// Prepare the child package git repo with two commits
prepareChildAndGetRefs(function (refs) {
prepareChildAndGetRefs(function (err, refs) {
if (err) { throw err }
chain([
// Install & shrinkwrap child package's first commit
[npm.commands.install, ['git://localhost:1234/child.git#' + refs[0]]],
// Backup node_modules with the first commit
[fs.rename, parentNodeModulesPath, outdatedNodeModulesPath],
// Install & shrinkwrap child package's second commit
[npm.commands.install, ['git://localhost:1234/child.git#' + refs[1]]],
// Install & shrinkwrap child package's latest commit
[npm.commands.install, ['git://localhost:1234/child.git#' + refs[1].substr(0, 8)]],
// Restore node_modules with the first commit
[rimraf, parentNodeModulesPath],
[fs.rename, outdatedNodeModulesPath, parentNodeModulesPath],
// Update node_modules
[npm.commands.install, []]
], function () {
const pkglock = require(path.join(parentPath, 'package-lock.json'))
t.similar(pkglock, {
dependencies: {
child: {
version: `git://localhost:1234/child.git#${refs[1]}`,
from: `git://localhost:1234/child.git#${refs[1].substr(0, 8)}`
}
}
}, 'version and from fields are correct in git-based pkglock dep')
var childPackageJSON = require(path.join(parentNodeModulesPath, 'child', 'package.json'))
t.equal(
childPackageJSON._resolved,
Expand Down Expand Up @@ -114,12 +126,13 @@ function prepareChildAndGetRefs (cb) {
git.chainableExec(['add', 'README.md'], opts),
git.chainableExec(['commit', '-m', 'Add README'], opts),
git.chainableExec(['log', '--pretty=format:"%H"', '-2'], opts)
], function () {
], function (err) {
if (err) { return cb(err) }
var gitLogStdout = arguments[arguments.length - 1]
var refs = gitLogStdout[gitLogStdout.length - 1].split('\n').map(function (ref) {
return ref.match(/^"(.+)"$/)[1]
}).reverse() // Reverse refs order: last, first -> first, last
cb(refs)
cb(null, refs)
})
}

Expand Down

0 comments on commit 820f74a

Please sign in to comment.