git ssb

0+

cel / pull-git-remote-helper



Tree: b634a730e0a867de83e309323cb2c57c2714d0da

Files: b634a730e0a867de83e309323cb2c57c2714d0da / test / run.js

3694 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}
17var userStr = 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
75tape('make a commit and push', function (t) {
76 var commitMessage = 'Initial commit'
77 var fileName = 'blah.txt'
78 var fileContents = 'i am a file'
79 var fileHash = '68bd10497ea68e91fa85024d0a0b2fe54e212914'
80 var treeHash = '75c54aa020772a916853987a03bff7079463a861'
81 var commitHash = 'edb5b50e8019797925820007d318870f8c346726'
82 var fileHashBuf = new Buffer(20)
83 fileHashBuf.hexWrite(fileHash)
84
85 var objects = t.items(t.deepEquals, [
86 [{
87 type: 'commit',
88 data: 'tree ' + treeHash + '\nauthor ' + userStr + ' 1000000000 -0500\ncommitter ' + userStr + ' 1000000000 -0500\n\n' + commitMessage + '\n'
89 }, 'got the commit'],
90 [{
91 type: 'tree',
92 data: '100644 ' + fileName + '\0' + fileHashBuf.toString('ascii')
93 }, 'got the tree'],
94 [{
95 type: 'blob', data: fileContents
96 }, 'got the blob']
97 ])
98
99 var refs = t.items(t.deepEquals, [
100 [{
101 name: 'refs/heads/master',
102 new: commitHash,
103 old: null
104 }, 'got the ref']
105 ])
106
107 var filePath = path.join(tmpDir, fileName)
108 fs.writeFile(filePath, fileContents, function (err) {
109 t.error(err, 'wrote a file')
110 t.git('add', filePath, function (code) {
111 t.equals(code, 0, 'added file')
112 t.git('commit', '-m', commitMessage, function (code) {
113 t.equals(code, 0, 'made initial commit')
114 t.git('push', '-vv', remote, 'master', function (msg) {
115 if (msg.object)
116 objects(msg.object)
117 else if (msg.ref)
118 refs(msg.ref)
119 else
120 t.notOk(msg, 'unexpected message')
121 }, function (code) {
122 t.equals(code, 0, 'pushed')
123 t.end()
124 })
125 })
126 })
127 })
128})
129
130/*
131tape('fetch', function (t) {
132 t.git('fetch', '-vv', remote, function (code) {
133 t.equals(code, 0, 'fetched')
134 t.end()
135 })
136})
137*/
138
139tape.onFinish(function () {
140 if (tmpDir)
141 rimraf.sync(tmpDir)
142})
143

Built with git-ssb-web