Skip to content
This repository has been archived by the owner on Jul 3, 2019. It is now read-only.

Commit

Permalink
test: add baseline git manifest test
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Nov 7, 2017
1 parent 56c4cb0 commit dda8238
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 24 deletions.
2 changes: 2 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ install:
- npm install

test_script:
- git config --global user.email "[email protected]"
- git config --global user.name "AppVeyor Test User"
- npm test

matrix:
Expand Down
53 changes: 53 additions & 0 deletions test/git.manifest.js
Original file line number Diff line number Diff line change
@@ -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: '[email protected]'
}, 'manifest fetched correctly')
})
})
})
75 changes: 51 additions & 24 deletions test/util/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit dda8238

Please sign in to comment.