test/run.jsView |
---|
4 | 4 | var mktemp = require('mktemp') |
5 | 5 | var rimraf = require('rimraf') |
6 | 6 | var fs = require('fs') |
7 | 7 | |
| 8 | +function noop() {} |
| 9 | + |
8 | 10 | var env = Object.create(process.env) |
9 | 11 | env.PATH = __dirname + ':' + env.PATH |
10 | 12 | 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 + '>' |
12 | 18 | var remote = 'test.js://foo' |
13 | 19 | |
14 | 20 | var tmpDir = mktemp.createDirSync(path.join(require('os').tmpdir(), 'XXXXXXX')) |
15 | 21 | |
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 () { |
17 | 35 | 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, { |
20 | 39 | env: env, |
21 | 40 | 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)) |
24 | 45 | } |
25 | 46 | |
| 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 | + |
26 | 54 | 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) { |
30 | 58 | 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) { |
32 | 60 | t.equals(code, 0, 'set user email') |
33 | 61 | t.end() |
34 | 62 | }) |
35 | 63 | }) |
36 | 64 | }) |
37 | 65 | }) |
38 | 66 | |
39 | 67 | tape('push with empty repo', function (t) { |
40 | | - git('push', remote, function (code) { |
| 68 | + t.git('push', remote, function (msg) { |
| 69 | + }, function (code) { |
41 | 70 | t.equals(code, 0, 'pushed') |
42 | 71 | t.end() |
43 | 72 | }) |
44 | 73 | }) |
45 | 74 | |
46 | 75 | 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) { |
49 | 104 | t.error(err, 'wrote a file') |
50 | | - git('add', filename, function (code) { |
| 105 | + t.git('add', filePath, function (code) { |
51 | 106 | t.equals(code, 0, 'added file') |
52 | | - git('commit', '-mInitial commit', function (code) { |
| 107 | + t.git('commit', '-m', commitMessage, function (code) { |
53 | 108 | 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) { |
55 | 117 | t.equals(code, 0, 'pushed') |
56 | 118 | t.end() |
57 | 119 | }) |
58 | 120 | }) |
61 | 123 | }) |
62 | 124 | |
63 | 125 | |
64 | 126 | tape('fetch', function (t) { |
65 | | - git('fetch', '-vv', remote, function (code) { |
| 127 | + t.git('fetch', '-vv', remote, function (code) { |
66 | 128 | t.equals(code, 0, 'fetched') |
67 | 129 | t.end() |
68 | 130 | }) |
69 | 131 | }) |