Files: 16cb3742dd9d961d752709c70a7d7e0d0139c0f8 / test / run.js
3966 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 | user.str = user.name + ' <' + user.email + '>' |
18 | var remote = { |
19 | empty: 'empty.js://', |
20 | full: 'full.js://' |
21 | } |
22 | |
23 | var tmpDir = mktemp.createDirSync(path.join(require('os').tmpdir(), 'XXXXXXX')) |
24 | |
25 | function handleIpcMessage(t, cb) { |
26 | return function (msg) { |
27 | if (msg.error) { |
28 | var err = new Error(msg.error.message) |
29 | err.stack = msg.error.stack |
30 | t.error(err) |
31 | } else { |
32 | cb(msg) |
33 | } |
34 | } |
35 | } |
36 | |
37 | tape.Test.prototype.git = function () { |
38 | var args = [].slice.call(arguments) |
39 | var doneCb = args.pop() |
40 | var msgCb = (typeof args[args.length-1] == 'function') && args.pop() |
41 | return spawn('git', args, { |
42 | env: env, |
43 | cwd: tmpDir, |
44 | stdio: ['ignore', 'inherit', 'inherit', 'ipc'] |
45 | }) |
46 | .on('close', doneCb) |
47 | .on('message', handleIpcMessage(this, msgCb)) |
48 | } |
49 | |
50 | tape.Test.prototype.items = function (fn, items) { |
51 | var i = 0 |
52 | return function (item) { |
53 | fn.apply(this, [item].concat(items[i++])) |
54 | }.bind(this) |
55 | } |
56 | |
57 | tape('init repo', function (t) { |
58 | t.git('init', function (code) { |
59 | t.equals(code, 0, 'git init') |
60 | t.git('config', 'user.name', user.name, function (code) { |
61 | t.equals(code, 0, 'set user name') |
62 | t.git('config', 'user.email', user.email, function (code) { |
63 | t.equals(code, 0, 'set user email') |
64 | t.end() |
65 | }) |
66 | }) |
67 | }) |
68 | }) |
69 | |
70 | tape('push with empty repo', function (t) { |
71 | t.git('push', remote.empty, function (msg) { |
72 | }, function (code) { |
73 | t.equals(code, 0, 'pushed') |
74 | t.end() |
75 | }) |
76 | }) |
77 | |
78 | function hexToStr(str) { |
79 | var buf = new Buffer(str.length / 2) |
80 | buf.hexWrite(str) |
81 | return buf.toString('ascii') |
82 | } |
83 | |
84 | tape('make a commit and push', function (t) { |
85 | t.plan(8) // write, add, commit, push, ref, commit, tree, blob |
86 | |
87 | var file = { |
88 | name: 'blah.txt', |
89 | data: 'i am a file', |
90 | hash: '68bd10497ea68e91fa85024d0a0b2fe54e212914' |
91 | } |
92 | |
93 | var tree = { |
94 | hash: '75c54aa020772a916853987a03bff7079463a861', |
95 | data: '100644 ' + file.name + '\0' + hexToStr(file.hash) |
96 | } |
97 | |
98 | var commitMessage = 'Initial commit' |
99 | var commit = { |
100 | hash: 'edb5b50e8019797925820007d318870f8c346726', |
101 | data: ['tree ' + tree.hash, |
102 | 'author ' + user.str + ' 1000000000 -0500', |
103 | 'committer ' + user.str + ' 1000000000 -0500', |
104 | '', commitMessage, '' |
105 | ].join('\n') |
106 | } |
107 | |
108 | function obj(type, o) { |
109 | return { |
110 | type: type, |
111 | data: o.data, |
112 | length: o.data.length, |
113 | hash: o.hash |
114 | } |
115 | } |
116 | |
117 | var objects = t.items(t.deepEquals, [ |
118 | [obj('commit', commit), 'got the commit'], |
119 | [obj('tree', tree), 'got the tree'], |
120 | [obj('blob', file), 'got the blob'] |
121 | ]) |
122 | |
123 | var refs = t.items(t.deepEquals, [ |
124 | [{ |
125 | name: 'refs/heads/master', |
126 | new: commit.hash, |
127 | old: null |
128 | }, 'got the ref'] |
129 | ]) |
130 | |
131 | var filePath = path.join(tmpDir, file.name) |
132 | fs.writeFile(filePath, file.data, function (err) { |
133 | t.error(err, 'wrote a file') |
134 | t.git('add', filePath, function (code) { |
135 | t.equals(code, 0, 'added file') |
136 | t.git('commit', '-m', commitMessage, function (code) { |
137 | t.equals(code, 0, 'made initial commit') |
138 | t.git('push', '-vv', remote.empty, 'master', function (msg) { |
139 | if (msg.object) |
140 | objects(msg.object) |
141 | else if (msg.ref) |
142 | refs(msg.ref) |
143 | else |
144 | t.notOk(msg, 'unexpected message') |
145 | }, function (code) { |
146 | t.equals(code, 0, 'pushed') |
147 | }) |
148 | }) |
149 | }) |
150 | }) |
151 | }) |
152 | |
153 | tape('fetch', function (t) { |
154 | t.git('fetch', '-vv', remote.full, function (code) { |
155 | t.equals(code, 0, 'fetched') |
156 | t.end() |
157 | }) |
158 | }) |
159 | |
160 | tape.onFinish(function () { |
161 | if (tmpDir) |
162 | rimraf.sync(tmpDir) |
163 | }) |
164 |
Built with git-ssb-web