Files: 9a9f61c8a685733a3ea47dbf68dd64ef9730f49f / test.js
2881 bytesRaw
1 | var test = require('tape') |
2 | var path = require('path') |
3 | var rimraf = require('rimraf') |
4 | var fs = require('fs') |
5 | var os = require('os') |
6 | var ssbKeys = require('ssb-keys') |
7 | var cp = require('child_process') |
8 | |
9 | function randstr() { |
10 | return Math.random().toString(36).substr(2) |
11 | } |
12 | |
13 | var createSbot = require('scuttlebot') |
14 | .use(require('scuttlebot/plugins/master')) |
15 | .use(require('scuttlebot/plugins/replicate')) |
16 | .use(require('ssb-blobs')) |
17 | .use(require('ssb-friends')) |
18 | |
19 | var appName = 'git_ssb_test_' + randstr() |
20 | var sbotPath = path.join(os.tmpdir(), appName) |
21 | var sbotPort = 45400 + ~~(Math.random()*100) |
22 | var repoPath = path.join(os.tmpdir(), 'ssb-git-repo-' + randstr()) |
23 | // Set path explicitly so we won't risk leaving temp dotfiles in the user's |
24 | // home directory. |
25 | process.env.ssb_path = sbotPath |
26 | process.env.ssb_appname = appName |
27 | process.env[appName + '_port'] = sbotPort |
28 | |
29 | var sbot = createSbot({ |
30 | path: sbotPath, |
31 | port: sbotPort, |
32 | timeout: 200, |
33 | allowPrivate: true, |
34 | keys: ssbKeys.loadOrCreateSync(path.join(sbotPath, 'secret')) |
35 | }) |
36 | |
37 | fs.writeFileSync(path.join(sbotPath, 'manifest.json'), |
38 | JSON.stringify(sbot.getManifest())) |
39 | |
40 | test.onFinish(function () { |
41 | sbot.close(true, function (err) { |
42 | rimraf.sync(sbotPath) |
43 | rimraf.sync(repoPath) |
44 | if (err) throw err |
45 | }) |
46 | }) |
47 | |
48 | var srcPath = path.dirname(__filename) |
49 | var cwd = process.cwd() |
50 | |
51 | function git() { |
52 | var args = [].concat.apply([], arguments) |
53 | var doneCb = args.pop() |
54 | cp.spawn('git', args, { |
55 | stdio: ['ignore', process.stderr, process.stderr], |
56 | cwd: cwd |
57 | }).on('close', doneCb) |
58 | } |
59 | |
60 | var url |
61 | |
62 | test('create ssb git-repo', function (t) { |
63 | sbot.publish({ type: 'git-repo' }, function (err, msg) { |
64 | t.error(err, 'publish git-repo message') |
65 | url = 'ssb://' + msg.key |
66 | t.end() |
67 | }) |
68 | }) |
69 | |
70 | test('clone empty repo', function (t) { |
71 | git('clone', url, repoPath, function (ret) { |
72 | t.error(ret, 'git clone') |
73 | t.end() |
74 | }) |
75 | }) |
76 | |
77 | test('push package repo to the remote', function (t) { |
78 | // TODO: use something other than the project repo |
79 | cwd = srcPath |
80 | git('push', '-q', url, 'master', function (ret) { |
81 | t.error(ret, 'git push') |
82 | t.end() |
83 | }) |
84 | }) |
85 | |
86 | test('pull from the remote', function (t) { |
87 | cwd = repoPath |
88 | git('pull', '-q', url, 'master', function (ret) { |
89 | t.error(ret, 'git pull') |
90 | t.end() |
91 | }) |
92 | }) |
93 | |
94 | test('make a commit and push it', function (t) { |
95 | cwd = repoPath |
96 | var newdir = randstr() |
97 | var filenames = [randstr(), randstr(), path.join(newdir, randstr())] |
98 | fs.mkdirSync(path.join(repoPath, newdir)) |
99 | filenames.forEach(function (filename) { |
100 | fs.writeFileSync(path.join(repoPath, filename), randstr()) |
101 | }) |
102 | git('add', filenames, function (ret) { |
103 | t.error(ret, 'git add') |
104 | git('commit', '-am', 'Add some files', function (ret) { |
105 | t.error(ret, 'git push') |
106 | git('push', url, 'master', function (ret) { |
107 | t.error(ret, 'git push') |
108 | t.end() |
109 | }) |
110 | }) |
111 | }) |
112 | }) |
113 |
Built with git-ssb-web