Files: 17c29f2cc34026a1af2ec44ea384e15716514a8f / test / run.js
3593 bytesRaw
1 | var spawn = require('child_process').spawn |
2 | var tape = require('tape') |
3 | var path = require('path') |
4 | var mktemp = require('mktemp') |
5 | var rimraf = require('rimraf') |
6 | var fs = require('fs') |
7 | |
8 | function noop() {} |
9 | |
10 | var env = Object.create(process.env) |
11 | env.PATH = __dirname + ':' + env.PATH |
12 | env.GIT_AUTHOR_DATE = env.GIT_COMMITTER_DATE = '1000000000 -0500' |
13 | var user = { |
14 | name: 'test', |
15 | email: 'test@localhost' |
16 | } |
17 | var userStr = user.name + ' <' + user.email + '>' |
18 | var remote = 'test.js://foo' |
19 | |
20 | var tmpDir = mktemp.createDirSync(path.join(require('os').tmpdir(), 'XXXXXXX')) |
21 | |
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 () { |
35 | var args = [].slice.call(arguments) |
36 | var doneCb = args.pop() |
37 | var msgCb = (typeof args[args.length-1] == 'function') && args.pop() |
38 | return spawn('git', args, { |
39 | env: env, |
40 | cwd: tmpDir, |
41 | stdio: ['ignore', 'inherit', 'inherit', 'ipc'] |
42 | }) |
43 | .on('close', doneCb) |
44 | .on('message', handleIpcMessage(this, msgCb)) |
45 | } |
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 | |
54 | tape('init repo', function (t) { |
55 | t.git('init', function (code) { |
56 | t.equals(code, 0, 'git init') |
57 | t.git('config', 'user.name', user.name, function (code) { |
58 | t.equals(code, 0, 'set user name') |
59 | t.git('config', 'user.email', user.email, function (code) { |
60 | t.equals(code, 0, 'set user email') |
61 | t.end() |
62 | }) |
63 | }) |
64 | }) |
65 | }) |
66 | |
67 | tape('push with empty repo', function (t) { |
68 | t.git('push', remote, function (msg) { |
69 | }, function (code) { |
70 | t.equals(code, 0, 'pushed') |
71 | t.end() |
72 | }) |
73 | }) |
74 | |
75 | tape('make a commit and push', function (t) { |
76 | var commitMessage = 'Initial commit' |
77 | var fileName = 'blah.txt' |
78 | var fileContents = 'i am a file' |
79 | var fileHash = new Buffer(20) |
80 | fileHash.hexWrite('68bd10497ea68e91fa85024d0a0b2fe54e212914') |
81 | |
82 | var objects = t.items(t.deepEquals, [ |
83 | [{ |
84 | type: 'commit', |
85 | data: 'tree 75c54aa020772a916853987a03bff7079463a861\nauthor ' + userStr + ' 1000000000 -0500\ncommitter ' + userStr + ' 1000000000 -0500\n\n' + commitMessage + '\n' |
86 | }, 'got the commit'], |
87 | [{ |
88 | type: 'tree', |
89 | data: '100644 ' + fileName + '\0' + fileHash.toString('ascii') |
90 | }, 'got the tree'], |
91 | [{ |
92 | type: 'blob', data: fileContents |
93 | }, 'got the blob'] |
94 | ]) |
95 | |
96 | var refs = t.items(t.deepEquals, [ |
97 | [{ |
98 | name: 'refs/heads/master', |
99 | new: 'edb5b50e8019797925820007d318870f8c346726', |
100 | old: null |
101 | }, 'got the ref'] |
102 | ]) |
103 | |
104 | var filePath = path.join(tmpDir, fileName) |
105 | fs.writeFile(filePath, fileContents, function (err) { |
106 | t.error(err, 'wrote a file') |
107 | t.git('add', filePath, function (code) { |
108 | t.equals(code, 0, 'added file') |
109 | t.git('commit', '-m', commitMessage, function (code) { |
110 | t.equals(code, 0, 'made initial commit') |
111 | t.git('push', '-vv', remote, 'master', function (msg) { |
112 | if (msg.object) |
113 | objects(msg.object) |
114 | else if (msg.ref) |
115 | refs(msg.ref) |
116 | else |
117 | t.notOk(msg, 'unexpected message') |
118 | }, function (code) { |
119 | t.equals(code, 0, 'pushed') |
120 | t.end() |
121 | }) |
122 | }) |
123 | }) |
124 | }) |
125 | }) |
126 | |
127 | /* |
128 | tape('fetch', function (t) { |
129 | t.git('fetch', '-vv', remote, function (code) { |
130 | t.equals(code, 0, 'fetched') |
131 | t.end() |
132 | }) |
133 | }) |
134 | */ |
135 | |
136 | tape.onFinish(function () { |
137 | if (tmpDir) |
138 | rimraf.sync(tmpDir) |
139 | }) |
140 |
Built with git-ssb-web