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