git ssb

0+

cel / pull-git-remote-helper



Tree: 89903ba2fa42a5bb486607c2b44b9d740697af3b

Files: 89903ba2fa42a5bb486607c2b44b9d740697af3b / test / run.js

3719 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')
7var repo = require('./repo')
8
9var env = Object.create(process.env)
10env.PATH = path.join(__dirname, 'remote') + ':' + env.PATH
11env.GIT_AUTHOR_DATE = env.GIT_COMMITTER_DATE = repo.date
12var user = repo.user
13var remote = {
14 empty: 'empty.js://',
15 full: 'full.js://'
16}
17
18var tmpDir = mktemp.createDirSync(path.join(require('os').tmpdir(), 'XXXXXXX'))
19tape.onFinish(function () {
20 if (tmpDir)
21 rimraf.sync(tmpDir)
22})
23
24function handleIpcMessage(t, cb) {
25 return function (msg) {
26 if (msg.error) {
27 var err = new Error(msg.error.message)
28 err.stack = msg.error.stack
29 t.error(err)
30 } else {
31 cb(msg)
32 }
33 }
34}
35
36tape.Test.prototype.git = function () {
37 var args = [].slice.call(arguments)
38 var doneCb = args.pop()
39 var msgCb = (typeof args[args.length-1] == 'function') && args.pop()
40 return spawn('git', args, {
41 env: env,
42 cwd: tmpDir,
43 stdio: ['ignore', 'inherit', 'inherit', 'ipc']
44 })
45 .on('close', doneCb)
46 .on('message', handleIpcMessage(this, msgCb))
47}
48
49tape.Test.prototype.items = function (fn, items) {
50 var i = 0
51 return function (item) {
52 fn.apply(this, [item].concat(items[i++]))
53 }.bind(this)
54}
55
56tape('init repo', function (t) {
57 t.git('init', function (code) {
58 t.equals(code, 0, 'git init')
59 t.git('config', 'user.name', user.name, function (code) {
60 t.equals(code, 0, 'set user name')
61 t.git('config', 'user.email', user.email, function (code) {
62 t.equals(code, 0, 'set user email')
63 t.end()
64 })
65 })
66 })
67})
68
69tape('push with empty repo', function (t) {
70 t.git('push', remote.empty, function (msg) {
71 }, function (code) {
72 t.equals(code, 0, 'pushed')
73 t.end()
74 })
75})
76
77tape('make a commit and push', function (t) {
78 t.plan(8) // write, add, commit, push, ref, commit, tree, blob
79
80 var file = repo.file
81 var commitMessage = repo.commitMessage
82 var commit = repo.commit
83 var tree = repo.tree
84
85 function obj(type, o) {
86 return {
87 type: type,
88 data: o.data.toString('ascii'),
89 length: o.data.length,
90 hash: o.hash
91 }
92 }
93
94 var objects = t.items(t.deepEquals, [
95 [obj('commit', commit), 'got the commit'],
96 [obj('tree', tree), 'got the tree'],
97 [obj('blob', file), 'got the blob']
98 ])
99
100 var updates = t.items(t.deepEquals, [
101 [{
102 name: 'refs/heads/master',
103 new: commit.hash,
104 old: null
105 }, 'got the ref']
106 ])
107
108 var filePath = path.join(tmpDir, repo.fileName)
109 fs.writeFile(filePath, file.data, function (err) {
110 t.error(err, 'wrote a file')
111 t.git('add', filePath, function (code) {
112 t.equals(code, 0, 'added file')
113 t.git('commit', '-m', commitMessage, function (code) {
114 t.equals(code, 0, 'made initial commit')
115 t.git('push', remote.empty, 'master', function (msg) {
116 if (msg.object)
117 objects(msg.object)
118 else if (msg.update)
119 updates(msg.update)
120 else
121 t.notOk(msg, 'unexpected message')
122 }, function (code) {
123 t.equals(code, 0, 'pushed')
124 })
125 })
126 })
127 })
128})
129
130tape('fetch when already up-to-date', function (t) {
131 t.git('fetch', remote.full, function (msg) {
132 t.notOk(msg, 'should not get a message here')
133 }, function (code) {
134 t.equals(code, 0, 'fetched')
135 t.end()
136 })
137})
138
139tape('clone into new dir', function (t) {
140 var dir = path.join(tmpDir, 'clonedir')
141 t.git('clone', remote.full, dir, function (msg) {
142 t.notOk(msg, 'unexpected message')
143 }, function (code) {
144 t.equals(code, 0, 'cloned')
145 t.end()
146 })
147})
148

Built with git-ssb-web