From c618eeda3e321fd454d77c476b53a0330f2344cc Mon Sep 17 00:00:00 2001 From: smikes Date: Sat, 20 Dec 2014 05:53:48 -0700 Subject: install: don't try to --link git dependencies test sets up git repo & mock registry -- fragile test of install --link=false test of install --link=true --- lib/install.js | 4 +- test/tap/git-dependency-install-link.js | 184 ++++++++++++++++++++++++++++++++ 2 files changed, 187 insertions(+), 1 deletion(-) create mode 100644 test/tap/git-dependency-install-link.js diff --git a/lib/install.js b/lib/install.js index b79987147..acfc2aeb7 100644 --- a/lib/install.js +++ b/lib/install.js @@ -868,8 +868,10 @@ function targetResolver (where, context, deps) { function installOne (target, where, context, cb) { // the --link flag makes this a "link" command if it's at the // the top level. + var isGit = npa(target._resolved).type === "git" + if (where === npm.prefix && npm.config.get("link") - && !npm.config.get("global")) { + && !npm.config.get("global") && !isGit) { return localLink(target, where, context, cb) } installOne_(target, where, context, function (er, installedWhat) { diff --git a/test/tap/git-dependency-install-link.js b/test/tap/git-dependency-install-link.js new file mode 100644 index 000000000..2d382dd56 --- /dev/null +++ b/test/tap/git-dependency-install-link.js @@ -0,0 +1,184 @@ +var fs = require('fs') +var resolve = require('path').resolve + +var chain = require('slide').chain +var osenv = require('osenv') +var mkdirp = require('mkdirp') +var rimraf = require('rimraf') +var test = require('tap').test +var readJson = require('read-package-json') +var mr = require('npm-registry-mock') + +var npm = require('../../lib/npm.js') +var common = require('../common-tap.js') + +var pkg = resolve(__dirname, 'git-dependency-install-link') +var repo = resolve(__dirname, 'git-dependency-install-link-repo') +var cache = resolve(pkg, 'cache') + +var daemon +var daemonPID +var git +var mockRegistry + +var EXEC_OPTS = { + registry: common.registry, + cwd: pkg, + cache: cache +} + +test('setup', function (t) { + bootstrap() + setup(function (er, r) { + t.ifError(er, 'git started up successfully') + + if (!er) { + daemon = r[r.length - 2] + daemonPID = r[r.length - 1] + } + + mr({ + port: common.port + }, function (er, server) { + t.ifError(er, 'started mock registry') + mockRegistry = server + + t.end() + }) + }) +}) + +test('install from git repo [no --link]', function (t) { + process.chdir(pkg) + + common.npm(['install', '--loglevel', 'error'], EXEC_OPTS, function (err, code, stdout, stderr) { + t.ifError(err, 'npm install failed') + + t.dissimilar(stderr, /Command failed:/, 'expect git to succeed') + t.dissimilar(stderr, /version not found/, 'should not go to repository') + + readJson(resolve(pkg, 'node_modules', 'child', 'package.json'), function (err, data) { + t.ifError(err, 'error reading child package.json') + + t.equal(data && data.version, '1.0.3') + t.end() + }) + }) +}) + +test('install from git repo [with --link]', function (t) { + process.chdir(pkg) + rimraf.sync(resolve(pkg, 'node_modules')) + + common.npm(['install', '--link', '--loglevel', 'error'], EXEC_OPTS, function (err, code, stdout, stderr) { + t.ifError(err, 'npm install --link failed') + + t.dissimilar(stderr, /Command failed:/, 'expect git to succeed') + t.dissimilar(stderr, /version not found/, 'should not go to repository') + + readJson(resolve(pkg, 'node_modules', 'child', 'package.json'), function (err, data) { + t.ifError(err, 'error reading child package.json') + + t.equal(data && data.version, '1.0.3') + t.end() + }) + }) +}) + +test('clean', function (t) { + mockRegistry.close() + daemon.on('close', function () { + cleanup() + t.end() + }) + process.kill(daemonPID) +}) + +var pjParent = JSON.stringify({ + name: 'parent', + version: '1.2.3', + dependencies: { + 'child': 'git://localhost:1234/child.git' + } +}, null, 2) + '\n' + +var pjChild = JSON.stringify({ + name: 'child', + version: '1.0.3' +}, null, 2) + '\n' + +function bootstrap () { + rimraf.sync(repo) + rimraf.sync(pkg) + mkdirp.sync(pkg) + mkdirp.sync(cache) + + fs.writeFileSync(resolve(pkg, 'package.json'), pjParent) +} + +function setup (cb) { + mkdirp.sync(repo) + fs.writeFileSync(resolve(repo, 'package.json'), pjChild) + npm.load({ + link: true, + prefix: pkg, + loglevel: 'silent' + }, function () { + git = require('../../lib/utils/git.js') + + function startDaemon (cb) { + // start git server + var d = git.spawn( + [ + 'daemon', + '--verbose', + '--listen=localhost', + '--export-all', + '--base-path=.', + '--port=1234' + ], + { + cwd: pkg, + env: process.env, + stdio: ['pipe', 'pipe', 'pipe'] + } + ) + d.stderr.on('data', childFinder) + + function childFinder (c) { + var cpid = c.toString().match(/^\[(\d+)\]/) + if (cpid[1]) { + this.removeListener('data', childFinder) + cb(null, [d, cpid[1]]) + } + } + } + + var opts = { + cwd: repo, + env: process.env + } + + chain( + [ + git.chainableExec(['init'], opts), + git.chainableExec(['config', 'user.name', 'PhantomFaker'], opts), + git.chainableExec(['config', 'user.email', 'nope@not.real'], opts), + git.chainableExec(['add', 'package.json'], opts), + git.chainableExec(['commit', '-m', 'stub package'], opts), + git.chainableExec( + ['clone', '--bare', repo, 'child.git'], + { cwd: pkg, env: process.env } + ), + startDaemon + ], + cb + ) + }) +} + +function cleanup () { + process.chdir(osenv.tmpdir()) + rimraf.sync(repo) + rimraf.sync(pkg) +} -- cgit v1.2.3