git ssb

0+

cel / pull-git-remote-helper



Tree: f60f57ddecf31964b98f5147c7ee02faa6c871fc

Files: f60f57ddecf31964b98f5147c7ee02faa6c871fc / test / run.js

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

Built with git-ssb-web