git ssb

0+

cel / pull-git-remote-helper



Tree: 378b71dc16d7ede4e69a83b6615b8811276606c3

Files: 378b71dc16d7ede4e69a83b6615b8811276606c3 / test / run.js

3922 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 = 'test.js://foo'
19
20var tmpDir = mktemp.createDirSync(path.join(require('os').tmpdir(), 'XXXXXXX'))
21
22function 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
34tape.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
47tape.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
54tape('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
67tape('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
75function hexToStr(str) {
76 var buf = new Buffer(str.length / 2)
77 buf.hexWrite(str)
78 return buf.toString('ascii')
79}
80
81tape('make a commit and push', function (t) {
82 t.plan(8) // write, add, commit, push, ref, commit, tree, blob
83
84 var file = {
85 name: 'blah.txt',
86 data: 'i am a file',
87 hash: '68bd10497ea68e91fa85024d0a0b2fe54e212914'
88 }
89
90 var tree = {
91 hash: '75c54aa020772a916853987a03bff7079463a861',
92 data: '100644 ' + file.name + '\0' + hexToStr(file.hash)
93 }
94
95 var commitMessage = 'Initial commit'
96 var commit = {
97 hash: 'edb5b50e8019797925820007d318870f8c346726',
98 data: ['tree ' + tree.hash,
99 'author ' + user.str + ' 1000000000 -0500',
100 'committer ' + user.str + ' 1000000000 -0500',
101 '', commitMessage, ''
102 ].join('\n')
103 }
104
105 function obj(type, o) {
106 return {
107 type: type,
108 data: o.data,
109 length: o.data.length,
110 hash: o.hash
111 }
112 }
113
114 var objects = t.items(t.deepEquals, [
115 [obj('commit', commit), 'got the commit'],
116 [obj('tree', tree), 'got the tree'],
117 [obj('blob', file), 'got the blob']
118 ])
119
120 var refs = t.items(t.deepEquals, [
121 [{
122 name: 'refs/heads/master',
123 new: commit.hash,
124 old: null
125 }, 'got the ref']
126 ])
127
128 var filePath = path.join(tmpDir, file.name)
129 fs.writeFile(filePath, file.data, function (err) {
130 t.error(err, 'wrote a file')
131 t.git('add', filePath, function (code) {
132 t.equals(code, 0, 'added file')
133 t.git('commit', '-m', commitMessage, function (code) {
134 t.equals(code, 0, 'made initial commit')
135 t.git('push', '-vv', remote, 'master', function (msg) {
136 if (msg.object)
137 objects(msg.object)
138 else if (msg.ref)
139 refs(msg.ref)
140 else
141 t.notOk(msg, 'unexpected message')
142 }, function (code) {
143 t.equals(code, 0, 'pushed')
144 })
145 })
146 })
147 })
148})
149
150/*
151tape('fetch', function (t) {
152 t.git('fetch', '-vv', remote, function (code) {
153 t.equals(code, 0, 'fetched')
154 t.end()
155 })
156})
157*/
158
159tape.onFinish(function () {
160 if (tmpDir)
161 rimraf.sync(tmpDir)
162})
163

Built with git-ssb-web