git ssb

0+

cel / pull-git-remote-helper



Commit cfeb530e50e371d1f66077bccf8a22e8e15f6427

Test commit and push with the actual objects

Use IPC to send send messages from test helper to test runner for testing
Charles Lehner committed on 2/8/2016, 4:37:24 AM
Parent: 280fd5659e55bc23f80c03d64513c47db2dc839b

Files changed

test/git-remote-test.jschanged
test/run.jschanged
test/git-remote-test.jsView
@@ -2,8 +2,15 @@
22
33 var toPull = require('stream-to-pull-stream')
44 var pull = require('pull-stream')
55
6+process.on('uncaughtException', function (err) {
7+ if (err.stack)
8+ err = {stack: err.stack, message: err.message}
9+ process.send({error: err})
10+ process.exit(1)
11+})
12+
613 pull(
714 toPull(process.stdin),
815 require('../')({
916 prefix: 'foo',
@@ -15,19 +22,20 @@
1522 read,
1623 pull.collect(function (err, bufs) {
1724 if (err) throw err
1825 var data = Buffer.concat(bufs).toString('ascii')
19- console.error('object', type, bufs.length, data.length, JSON.stringify(data))
26+ process.send({object: {type: type, data: data}})
2027 readObject(null, next)
2128 })
2229 )
2330 })
2431 },
2532 refSink: pull.drain(function (ref) {
26- console.error('ref', ref)
33+ process.send({ref: ref})
2734 }),
2835 }),
2936 toPull(process.stdout, function (err) {
3037 if (err)
3138 throw err
39+ process.disconnect()
3240 })
3341 )
test/run.jsView
@@ -4,55 +4,117 @@
44 var mktemp = require('mktemp')
55 var rimraf = require('rimraf')
66 var fs = require('fs')
77
8+function noop() {}
9+
810 var env = Object.create(process.env)
911 env.PATH = __dirname + ':' + env.PATH
1012 env.GIT_AUTHOR_DATE = env.GIT_COMMITTER_DATE = '1000000000 -0500'
11-var author = 'root <root@localhost>'
13+var user = {
14+ name: 'test',
15+ email: 'test@localhost'
16+}
17+var userStr = user.name + ' <' + user.email + '>'
1218 var remote = 'test.js://foo'
1319
1420 var tmpDir = mktemp.createDirSync(path.join(require('os').tmpdir(), 'XXXXXXX'))
1521
16-function git() {
22+function handleIpcMessage(t, cb) {
23+ return function (msg) {
24+ if (msg.error) {
25+ var err = new Error(msg.error.message)
26+ err.stack = msg.error.stack
27+ t.error(err)
28+ } else {
29+ cb(msg)
30+ }
31+ }
32+}
33+
34+tape.Test.prototype.git = function () {
1735 var args = [].slice.call(arguments)
18- var cb = args.pop()
19- spawn('git', args, {
36+ var doneCb = args.pop()
37+ var msgCb = (typeof args[args.length-1] == 'function') && args.pop()
38+ return spawn('git', args, {
2039 env: env,
2140 cwd: tmpDir,
22- stdio: ['ignore', process.stderr, process.stderr]
23- }).on('close', cb)
41+ stdio: ['ignore', 'inherit', 'inherit', 'ipc']
42+ })
43+ .on('close', doneCb)
44+ .on('message', handleIpcMessage(this, msgCb))
2445 }
2546
47+tape.Test.prototype.items = function (fn, items) {
48+ var i = 0
49+ return function (item) {
50+ fn.apply(this, [item].concat(items[i++]))
51+ }.bind(this)
52+}
53+
2654 tape('init repo', function (t) {
27- git('init', function (code) {
28- t.equals(code, 0, 'inited')
29- git('config', 'user.name', 'test', function (code) {
55+ t.git('init', function (code) {
56+ t.equals(code, 0, 'git init')
57+ t.git('config', 'user.name', user.name, function (code) {
3058 t.equals(code, 0, 'set user name')
31- git('config', 'user.email', 'test@localhost', function (code) {
59+ t.git('config', 'user.email', user.email, function (code) {
3260 t.equals(code, 0, 'set user email')
3361 t.end()
3462 })
3563 })
3664 })
3765 })
3866
3967 tape('push with empty repo', function (t) {
40- git('push', remote, function (code) {
68+ t.git('push', remote, function (msg) {
69+ }, function (code) {
4170 t.equals(code, 0, 'pushed')
4271 t.end()
4372 })
4473 })
4574
4675 tape('make a commit and push', function (t) {
47- var filename = path.join(tmpDir, 'blah.txt')
48- fs.writeFile(filename, 'i am a file', function (err) {
76+ var commitMessage = 'Initial commit'
77+ var fileName = 'blah.txt'
78+ var fileContents = 'i am a file'
79+
80+ var objects = t.items(t.deepEquals, [
81+ [{
82+ type: 'commit',
83+ data: 'tree 75c54aa020772a916853987a03bff7079463a861\nauthor ' + userStr + ' 1000000000 -0500\ncommitter ' + userStr + ' 1000000000 -0500\n\n' + commitMessage + '\n'
84+ }, 'got the commit'],
85+ [{
86+ type: 'tree',
87+ data: '100644 ' + fileName + '\u0000h=\u0010I~&\u000e\u0011z\u0005\u0002M\n\u000b/eN!)\u0014'
88+ }, 'got the tree'],
89+ [{
90+ type: 'blob', data: fileContents
91+ }, 'got the blob']
92+ ])
93+
94+ var refs = t.items(t.deepEquals, [
95+ [{
96+ name: 'refs/heads/master',
97+ new: 'edb5b50e8019797925820007d318870f8c346726',
98+ old: null
99+ }, 'got the ref']
100+ ])
101+
102+ var filePath = path.join(tmpDir, fileName)
103+ fs.writeFile(filePath, fileContents, function (err) {
49104 t.error(err, 'wrote a file')
50- git('add', filename, function (code) {
105+ t.git('add', filePath, function (code) {
51106 t.equals(code, 0, 'added file')
52- git('commit', '-mInitial commit', function (code) {
107+ t.git('commit', '-m', commitMessage, function (code) {
53108 t.equals(code, 0, 'made initial commit')
54- git('push', '-vv', remote, 'master', function (code) {
109+ t.git('push', '-vv', remote, 'master', function (msg) {
110+ if (msg.object)
111+ objects(msg.object)
112+ else if (msg.ref)
113+ refs(msg.ref)
114+ else
115+ t.notOk(msg, 'unexpected message')
116+ }, function (code) {
55117 t.equals(code, 0, 'pushed')
56118 t.end()
57119 })
58120 })
@@ -61,9 +123,9 @@
61123 })
62124
63125 /*
64126 tape('fetch', function (t) {
65- git('fetch', '-vv', remote, function (code) {
127+ t.git('fetch', '-vv', remote, function (code) {
66128 t.equals(code, 0, 'fetched')
67129 t.end()
68130 })
69131 })

Built with git-ssb-web