Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

'ipfs init': CLI + tests #96

Merged
merged 6 commits into from
Apr 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions src/cli/commands/init.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
const Command = require('ronin').Command
const IpfsRepo = require('ipfs-repo')
const Ipfs = require('../../core')
const fsBlobStore = require('fs-blob-store')
const utils = require('../utils')

module.exports = Command.extend({
desc: 'Initialize ipfs local configuration',
desc: 'Initialize a local IPFS node',

options: {
bits: {
Expand All @@ -22,5 +26,30 @@ module.exports = Command.extend({
}
},

run: () => {}
run: (bits, force, empty) => {
const path = utils.getRepoPath()

const repo = new IpfsRepo(path, {
stores: {
keys: fsBlobStore,
config: fsBlobStore,
datastore: fsBlobStore,
logs: fsBlobStore,
locks: fsBlobStore,
version: fsBlobStore
}
})

var ipfs = new Ipfs(repo)
ipfs.init({
bits: bits,
force: force,
emptyRepo: empty
}, function (err, res) {
if (err) {
console.error(err.toString())
process.exit(1)
}
})
}
})
10 changes: 6 additions & 4 deletions src/cli/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ log.error = debug('cli:error')

exports = module.exports

const repoPath = process.env.IPFS_PATH || os.homedir() + '/.ipfs'

exports.isDaemonOn = isDaemonOn
function isDaemonOn () {
try {
fs.readFileSync(repoPath + '/api')
fs.readFileSync(exports.getRepoPath() + '/api')
log('daemon is on')
return true
} catch (err) {
Expand All @@ -29,7 +27,7 @@ function getAPICtl () {
throw new Error('daemon is not on')
}

const apiAddr = multiaddr(fs.readFileSync(repoPath + '/api').toString())
const apiAddr = multiaddr(fs.readFileSync(exports.getRepoPath() + '/api').toString())
return APIctl(apiAddr.toString())
}

Expand All @@ -44,3 +42,7 @@ exports.getIPFS = (callback) => {

callback(null, getAPICtl())
}

exports.getRepoPath = () => {
return process.env.IPFS_PATH || os.homedir() + '/.ipfs'
}
84 changes: 84 additions & 0 deletions tests/test-cli/test-init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/* eslint-env mocha */

const expect = require('chai').expect
const nexpect = require('nexpect')
const rimraf = require('rimraf')
const path = require('path')
const fs = require('fs')
const utils = require('../../src/cli/utils')

function repoExistsSync (p) {
return fs.existsSync(path.join(utils.getRepoPath(), p))
}

describe('init', function () {
this.timeout(10000)

var oldRepoPath = process.env.IPFS_PATH
beforeEach((done) => {
oldRepoPath = process.env.IPFS_PATH
const repoPath = '/tmp/ipfs-test-' + Math.random().toString().substring(2, 8) + '/'
process.env.IPFS_PATH = repoPath
done()
})

afterEach((done) => {
rimraf(process.env.IPFS_PATH, (err) => {
expect(err).to.not.exist
process.env.IPFS_PATH = oldRepoPath
done()
})
})

it('basic', (done) => {
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'init'])
.run((err, stdout, exitcode) => {
expect(err).to.not.exist
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only ensuring that no error is thrown, seems a bit superficial, maybe check for directories/files to exist at least?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. On it!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

expect(repoExistsSync('blocks')).to.equal(true)
expect(repoExistsSync('config')).to.equal(true)
expect(repoExistsSync('version')).to.equal(true)
done()
})
})

it('bits', (done) => {
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'init', '--bits', '64'])
.run((err, stdout, exitcode) => {
expect(err).to.not.exist
done()
})
})

it('empty', (done) => {
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'init', '--bits', '64', '--empty-repo', 'true'])
.run((err, stdout, exitcode) => {
expect(err).to.not.exist
expect(repoExistsSync('blocks')).to.equal(false)
expect(repoExistsSync('config')).to.equal(true)
expect(repoExistsSync('version')).to.equal(true)
done()
})
})

it('force', (done) => {
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'init', '--bits', '64', '--force'])
.run((err, stdout, exitcode) => {
expect(err).to.not.exist
expect(exitcode).to.equal(0)

nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'init', '--bits', '64'])
.run((err, stdout, exitcode) => {
expect(err).to.not.exist
expect(exitcode).to.equal(1)

nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'init', '--bits', '64', '--force'])
.run((err, stdout, exitcode) => {
expect(err).to.not.exist
expect(exitcode).to.equal(0)
done()
})
})
})
})
})