Files: b2cf98073f3c52efa61e1a9d9a2a6b4af4944906 / test / run.js
3851 bytesRaw
1 | var spawn = require('child_process').spawn |
2 | var tape = require('tape') |
3 | var path = require('path') |
4 | var mktemp = require('mktemp') |
5 | var rimraf = require('rimraf') |
6 | var fs = require('fs') |
7 | var repo = require('./repo') |
8 | |
9 | var env = Object.create(process.env) |
10 | env.PATH = path.join(__dirname, 'remote') + ':' + env.PATH |
11 | env.GIT_AUTHOR_DATE = env.GIT_COMMITTER_DATE = repo.date |
12 | var user = repo.user |
13 | var remote = { |
14 | empty: 'empty.js://', |
15 | full: 'full.js://' |
16 | } |
17 | |
18 | var tmpDir = mktemp.createDirSync(path.join(require('os').tmpdir(), 'XXXXXXX')) |
19 | tape.onFinish(function () { |
20 | if (tmpDir) |
21 | rimraf.sync(tmpDir) |
22 | }) |
23 | |
24 | function 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 | |
36 | tape.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 | |
49 | tape.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 | |
56 | tape('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 | |
69 | tape('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 | |
77 | tape('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 refs = 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.ref) |
119 | refs(msg.ref) |
120 | else |
121 | t.notOk(msg, 'unexpected message') |
122 | }, function (code) { |
123 | t.equals(code, 0, 'pushed') |
124 | }) |
125 | }) |
126 | }) |
127 | }) |
128 | }) |
129 | |
130 | tape('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 | |
139 | tape('clone into new dir', function (t) { |
140 | var dir = path.join(tmpDir, 'clonedir') |
141 | t.plan(2) |
142 | t.git('clone', remote.full, dir, function (msg) { |
143 | if (msg.want) |
144 | t.deepEquals(msg.want, { |
145 | type: 'want', |
146 | hash: 'edb5b50e8019797925820007d318870f8c346726' |
147 | }, 'got want') |
148 | else |
149 | t.notOk(msg, 'unexpected message') |
150 | }, function (code) { |
151 | t.equals(code, 0, 'cloned') |
152 | }) |
153 | }) |
154 |
Built with git-ssb-web