git ssb

0+

cel / pull-git-remote-helper



Tree: b1ed4631de0942640e4c7591c2be9dbdf8e98240

Files: b1ed4631de0942640e4c7591c2be9dbdf8e98240 / test / run.js

4038 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'))
24
25function 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
37tape.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
50tape.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
57tape('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
70tape('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
78function hexToStr(str) {
79 var buf = new Buffer(str.length / 2)
80 buf.hexWrite(str)
81 return buf.toString('ascii')
82}
83
84tape('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
153tape('fetch', function (t) {
154 t.git('fetch', '-vv', remote.full, function (msg) {
155 t.notOk('should not get a message here', msg)
156 }, function (code) {
157 t.equals(code, 0, 'fetched')
158 t.end()
159 })
160})
161
162tape.onFinish(function () {
163 if (tmpDir)
164 rimraf.sync(tmpDir)
165})
166

Built with git-ssb-web