From dda82383e57250182f07c99f2cbfcec5228b0ae8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kat=20March=C3=A1n?= Date: Tue, 7 Nov 2017 12:38:36 -0800 Subject: [PATCH] test: add baseline git manifest test --- appveyor.yml | 2 ++ test/git.manifest.js | 53 +++++++++++++++++++++++++++++++ test/util/git.js | 75 ++++++++++++++++++++++++++++++-------------- 3 files changed, 106 insertions(+), 24 deletions(-) create mode 100644 test/git.manifest.js diff --git a/appveyor.yml b/appveyor.yml index 71afe26..b5ea8ce 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -14,6 +14,8 @@ install: - npm install test_script: + - git config --global user.email "test@example.com" + - git config --global user.name "AppVeyor Test User" - npm test matrix: diff --git a/test/git.manifest.js b/test/git.manifest.js new file mode 100644 index 0000000..4defd0a --- /dev/null +++ b/test/git.manifest.js @@ -0,0 +1,53 @@ +'use strict' + +const BB = require('bluebird') + +const gitMock = require('./util/git.js') +const npmlog = require('npmlog') +const path = require('path') +const Tacks = require('tacks') +const test = require('tap').test +const testDir = require('./util/test-dir.js')(__filename) + +const Dir = Tacks.Dir +const File = Tacks.File + +const manifest = require('../manifest') + +const OPTS = { + cache: path.join(testDir, 'cache'), + registry: 'https://mock.reg', + log: npmlog, + retry: { + retries: 1, + factor: 1, + minTimeout: 1, + maxTimeout: 10 + } +} + +test('get manifest from package.json in git clone', t => { + const fixture = new Tacks(Dir({ + 'foo': Dir({ + 'package.json': File({ + name: 'foo', + version: '1.2.3' + }), + 'index.js': File('hello') + }) + })) + fixture.create(testDir) + return BB.using(gitMock({cwd: path.join(testDir, 'foo')}), srv => { + return manifest(`bar@git://127.0.0.1:${srv.port}/`, OPTS) + .then(mani => { + t.similar(mani, { + name: 'foo', + version: '1.2.3', + _resolved: new RegExp(`git://127.0.0.1:${srv.port}/#[a-f0-9]{40}$`), + _shasum: null, + _shrinkwrap: null, + _id: 'foo@1.2.3' + }, 'manifest fetched correctly') + }) + }) +}) diff --git a/test/util/git.js b/test/util/git.js index f725c15..70b5f23 100644 --- a/test/util/git.js +++ b/test/util/git.js @@ -5,41 +5,68 @@ const BB = require('bluebird') const cp = BB.promisifyAll(require('child_process')) const git = require('../../lib/util/git') const mkdirp = BB.promisify(require('mkdirp')) +const retry = require('promise-retry') module.exports = mockRepo function mockRepo (opts) { - return mkdirp() + opts = opts || {} + const cwd = opts.cwd || process.cwd() + return mkdirp(cwd).then(() => { + return git._exec(['init'], {cwd}) + }).then(() => { + return git._exec(['add', '.'], {cwd}) + }).then(() => { + return git._exec(['commit', '-m', '"initial commit"'], {cwd}) + }).then(() => { + return daemon(opts) + }) } +const PORT = 1234 + module.exports.daemon = daemon function daemon (opts) { opts = opts || {} - return BB.fromNode(cb => { - const srv = cp.spawn('git', [ - 'daemon', - '--verbose', - '--listen=localhost', - `--port=${opts.port || 1234}`, - '--reuseaddr', - '--export-all', - '--base-path=.' - ], { - cwd: opts.cwd || process.cwd() - }) - srv.stderr.on('data', d => { - console.warn(d.toString('utf8')) + + return BB.resolve(retry((tryAgain, attempt) => { + let stderr = '' + const port = (opts.port || PORT) + attempt + return BB.fromNode(cb => { + const srv = cp.spawn('git', [ + 'daemon', + '--verbose', + '--listen=localhost', + `--port=${port}`, + '--reuseaddr', + '--export-all', + '--base-path=.' + ], { + cwd: opts.cwd || process.cwd() + }) + srv.stderr.on('data', d => { + const str = d.toString('utf8') + stderr += str + const match = str.match(/\[(\d+)\] Ready to rumble/i) + if (match) { + srv.pid = parseInt(match[1]) + srv.port = port + cb(null, srv) + } + }) + srv.once('exit', cb) + srv.once('error', cb) }) - srv.stdout.on('data', d => { - const str = d.toString('utf8') - const match = str.match(/\[(\d+)\]/) - if (match) { - srv.pid = parseInt(match[1]) - cb(null, srv) + .then(srv => { + return srv + }, e => { + if (stderr.match(/already in use/i)) { + return tryAgain(e) + } else { + throw e } }) - srv.once('exit', cb) - srv.once('error', cb) - }).disposer(srv => BB.fromNode(cb => { + }, { factor: 1, minTimeout: 100 })) + .disposer(srv => BB.fromNode(cb => { srv.on('error', cb) srv.on('close', cb) srv.kill()