1var fs = require('fs') 2var resolve = require('path').resolve 3 4var cwd = process.cwd() 5var mkdirp = require('mkdirp') 6var rimraf = require('rimraf') 7var test = require('tap').test 8 9var npm = require('../../lib/npm.js') 10var common = require('../common-tap.js') 11 12var pkg = resolve(common.pkg, 'package') 13var repos = resolve(common.pkg, 'repos') 14var subwt = resolve(repos, 'subwt') 15var topwt = resolve(repos, 'topwt') 16var suburl = 'git://localhost:' + common.gitPort + '/sub.git' 17var topurl = 'git://localhost:' + common.gitPort + '/top.git' 18 19var daemon 20var daemonPID 21var git 22 23var pjParent = JSON.stringify({ 24 name: 'parent', 25 version: '1.2.3', 26 dependencies: { 27 child: topurl 28 } 29}, null, 2) + '\n' 30 31var pjChild = JSON.stringify({ 32 name: 'child', 33 version: '1.0.3' 34}, null, 2) + '\n' 35 36test('setup', function (t) { 37 setup(function (er, r) { 38 t.ifError(er, 'git started up successfully') 39 t.end() 40 }) 41}) 42 43test('install from repo', function (t) { 44 bootstrap(t) 45 npm.commands.install('.', [], function (er) { 46 t.ifError(er, 'npm installed via git') 47 t.end() 48 }) 49}) 50 51test('has file in submodule', function (t) { 52 bootstrap(t) 53 npm.commands.install('.', [], function (er) { 54 t.ifError(er, 'npm installed via git') 55 var fooPath = resolve('node_modules', 'child', 'subpath', 'foo.txt') 56 fs.stat(fooPath, function (er) { 57 t.ifError(er, 'file in submodule exists') 58 t.end() 59 }) 60 }) 61}) 62 63test('clean', function (t) { 64 daemon.on('close', function () { 65 t.end() 66 }) 67 process.kill(daemonPID) 68}) 69 70function bootstrap (t) { 71 process.chdir(cwd) 72 rimraf.sync(pkg) 73 mkdirp.sync(pkg) 74 process.chdir(pkg) 75 fs.writeFileSync('package.json', pjParent) 76} 77 78function setup (cb) { 79 rimraf.sync(pkg) 80 rimraf.sync(repos) 81 82 mkdirp.sync(topwt) 83 fs.writeFileSync(resolve(topwt, 'package.json'), pjChild) 84 mkdirp.sync(subwt) 85 fs.writeFileSync(resolve(subwt, 'foo.txt'), 'This is provided by submodule') 86 npm.load({ registry: common.registry, loglevel: 'silent' }, function () { 87 git = require('../../lib/utils/git.js') 88 89 function startDaemon (cb) { 90 // start git server 91 var d = git.spawn( 92 [ 93 'daemon', 94 '--verbose', 95 '--listen=localhost', 96 '--export-all', 97 '--base-path=.', 98 '--reuseaddr', 99 '--port=' + common.gitPort 100 ], 101 { 102 cwd: repos, 103 env: process.env, 104 stdio: ['pipe', 'pipe', 'pipe'] 105 } 106 ) 107 d.stderr.on('data', childFinder) 108 109 function childFinder (c) { 110 var cpid = c.toString().match(/^\[(\d+)\]/) 111 if (cpid[1]) { 112 this.removeListener('data', childFinder) 113 daemon = d 114 daemonPID = cpid[1] 115 cb(null) 116 } 117 } 118 } 119 120 var env = { PATH: process.env.PATH } 121 var topopt = { cwd: topwt, env: env } 122 var reposopt = { cwd: repos, env: env } 123 common.makeGitRepo({ 124 path: subwt, 125 message: 'subwt repo: ' + subwt, 126 added: ['foo.txt'], 127 commands: [ 128 git.chainableExec(['clone', '--bare', subwt, 'sub.git'], reposopt), 129 startDaemon, 130 [common.makeGitRepo, { 131 path: topwt, 132 message: 'topwt repo: ' + topwt, 133 commands: [ 134 git.chainableExec(['submodule', 'add', suburl, 'subpath'], topopt), 135 git.chainableExec(['commit', '-m', 'added submodule'], topopt), 136 git.chainableExec(['clone', '--bare', topwt, 'top.git'], reposopt) 137 ] 138 }] 139 ] 140 }, cb) 141 }) 142} 143