Files: cfeb530e50e371d1f66077bccf8a22e8e15f6427 / test / run.js
3528 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 | |
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) { |
104 | t.error(err, 'wrote a file') |
105 | t.git('add', filePath, function (code) { |
106 | t.equals(code, 0, 'added file') |
107 | t.git('commit', '-m', commitMessage, function (code) { |
108 | t.equals(code, 0, 'made initial commit') |
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) { |
117 | t.equals(code, 0, 'pushed') |
118 | t.end() |
119 | }) |
120 | }) |
121 | }) |
122 | }) |
123 | }) |
124 | |
125 | /* |
126 | tape('fetch', function (t) { |
127 | t.git('fetch', '-vv', remote, function (code) { |
128 | t.equals(code, 0, 'fetched') |
129 | t.end() |
130 | }) |
131 | }) |
132 | */ |
133 | |
134 | tape.onFinish(function () { |
135 | if (tmpDir) |
136 | rimraf.sync(tmpDir) |
137 | }) |
138 |
Built with git-ssb-web