Files: b634a730e0a867de83e309323cb2c57c2714d0da / test / run.js
3694 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 = '68bd10497ea68e91fa85024d0a0b2fe54e212914' |
80 | var treeHash = '75c54aa020772a916853987a03bff7079463a861' |
81 | var commitHash = 'edb5b50e8019797925820007d318870f8c346726' |
82 | var fileHashBuf = new Buffer(20) |
83 | fileHashBuf.hexWrite(fileHash) |
84 | |
85 | var objects = t.items(t.deepEquals, [ |
86 | [{ |
87 | type: 'commit', |
88 | data: 'tree ' + treeHash + '\nauthor ' + userStr + ' 1000000000 -0500\ncommitter ' + userStr + ' 1000000000 -0500\n\n' + commitMessage + '\n' |
89 | }, 'got the commit'], |
90 | [{ |
91 | type: 'tree', |
92 | data: '100644 ' + fileName + '\0' + fileHashBuf.toString('ascii') |
93 | }, 'got the tree'], |
94 | [{ |
95 | type: 'blob', data: fileContents |
96 | }, 'got the blob'] |
97 | ]) |
98 | |
99 | var refs = t.items(t.deepEquals, [ |
100 | [{ |
101 | name: 'refs/heads/master', |
102 | new: commitHash, |
103 | old: null |
104 | }, 'got the ref'] |
105 | ]) |
106 | |
107 | var filePath = path.join(tmpDir, fileName) |
108 | fs.writeFile(filePath, fileContents, function (err) { |
109 | t.error(err, 'wrote a file') |
110 | t.git('add', filePath, function (code) { |
111 | t.equals(code, 0, 'added file') |
112 | t.git('commit', '-m', commitMessage, function (code) { |
113 | t.equals(code, 0, 'made initial commit') |
114 | t.git('push', '-vv', remote, 'master', function (msg) { |
115 | if (msg.object) |
116 | objects(msg.object) |
117 | else if (msg.ref) |
118 | refs(msg.ref) |
119 | else |
120 | t.notOk(msg, 'unexpected message') |
121 | }, function (code) { |
122 | t.equals(code, 0, 'pushed') |
123 | t.end() |
124 | }) |
125 | }) |
126 | }) |
127 | }) |
128 | }) |
129 | |
130 | /* |
131 | tape('fetch', function (t) { |
132 | t.git('fetch', '-vv', remote, function (code) { |
133 | t.equals(code, 0, 'fetched') |
134 | t.end() |
135 | }) |
136 | }) |
137 | */ |
138 | |
139 | tape.onFinish(function () { |
140 | if (tmpDir) |
141 | rimraf.sync(tmpDir) |
142 | }) |
143 |
Built with git-ssb-web