git ssb

0+

cel / pull-git-remote-helper



Tree: 20c102b7036f7fee19f151e269d33cd7da837a3b

Files: 20c102b7036f7fee19f151e269d33cd7da837a3b / test / run.js

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

Built with git-ssb-web