git ssb

2+

cel / git-remote-ssb



Tree: 9a9f61c8a685733a3ea47dbf68dd64ef9730f49f

Files: 9a9f61c8a685733a3ea47dbf68dd64ef9730f49f / test.js

2881 bytesRaw
1var test = require('tape')
2var path = require('path')
3var rimraf = require('rimraf')
4var fs = require('fs')
5var os = require('os')
6var ssbKeys = require('ssb-keys')
7var cp = require('child_process')
8
9function randstr() {
10 return Math.random().toString(36).substr(2)
11}
12
13var 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
19var appName = 'git_ssb_test_' + randstr()
20var sbotPath = path.join(os.tmpdir(), appName)
21var sbotPort = 45400 + ~~(Math.random()*100)
22var 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.
25process.env.ssb_path = sbotPath
26process.env.ssb_appname = appName
27process.env[appName + '_port'] = sbotPort
28
29var sbot = createSbot({
30 path: sbotPath,
31 port: sbotPort,
32 timeout: 200,
33 allowPrivate: true,
34 keys: ssbKeys.loadOrCreateSync(path.join(sbotPath, 'secret'))
35})
36
37fs.writeFileSync(path.join(sbotPath, 'manifest.json'),
38 JSON.stringify(sbot.getManifest()))
39
40test.onFinish(function () {
41 sbot.close(true, function (err) {
42 rimraf.sync(sbotPath)
43 rimraf.sync(repoPath)
44 if (err) throw err
45 })
46})
47
48var srcPath = path.dirname(__filename)
49var cwd = process.cwd()
50
51function 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
60var url
61
62test('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
70test('clone empty repo', function (t) {
71 git('clone', url, repoPath, function (ret) {
72 t.error(ret, 'git clone')
73 t.end()
74 })
75})
76
77test('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
86test('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
94test('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