Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

feat: add test and docs for listing config profiles #536

Merged
merged 2 commits into from
Oct 4, 2019
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
59 changes: 59 additions & 0 deletions SPEC/CONFIG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* [config.get](#configget)
* [config.set](#configset)
* [config.replace](#configreplace)
* [config.profiles.list](#configprofileslist)
* [config.profiles.apply](#configprofilesapply)

#### `config.get`

Expand Down Expand Up @@ -85,5 +87,62 @@ ipfs.config.replace(newConfig, (err) => {

A great source of [examples][] can be found in the tests for this API.

#### `config.profiles.list`

> List available config profiles

##### `ipfs.config.profiles.list([options], [callback])`

`options` is a object.
`callback` must follow `function (err, result) {}` signature, where `err` is an error if the operation was not successful.

If no callback is passed, a [promise][] is returned

**Example:**

```JavaScript
ipfs.config.profiles.list(newConfig, (err, profiles) => {
if (err) {
throw err
}

profiles.forEach(profile => {
console.info(profile.name, profile.description)
})
})
```

A great source of [examples][] can be found in the tests for this API.

#### `config.profiles.apply`

> Apply a config profile

##### `ipfs.config.profiles.apply(name, [options], [callback])`

`name` is a string. Call `config.profiles.list()` for a list of valid profile names.
`options` an object that might contain the following values:
- `dryRun` is a boolean which if true does not apply the profile
`callback` must follow the `function (err, result) {}` signature, where `err` is an error if the operation was not successful.

If no callback is passed, a [promise][] is returned

**Example:**

```JavaScript
ipfs.config.profiles.apply('lowpower', (err, diff) => {
if (err) {
throw err
}

console.info(diff.old)
console.info(diff.new)
})
```

Note that you will need to restart your node for config changes to take effect.

A great source of [examples][] can be found in the tests for this API.

[promise]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
[examples]: https://github.com/ipfs/interface-ipfs-core/blob/master/src/config
2 changes: 1 addition & 1 deletion src/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const tests = {
get: require('./get'),
set: require('./set'),
replace: require('./replace'),
profile: require('./profile')
profiles: require('./profiles')
}

module.exports = createSuite(tests)
69 changes: 0 additions & 69 deletions src/config/profile.js

This file was deleted.

49 changes: 49 additions & 0 deletions src/config/profiles/apply.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* eslint-env mocha */
'use strict'

const { getDescribe, getIt, expect } = require('../../utils/mocha')
const waterfall = require('async/waterfall')

module.exports = (createCommon, options) => {
const describe = getDescribe(options)
const it = getIt(options)
const common = createCommon()

describe('.config.profiles.apply', function () {
this.timeout(30 * 1000)
let ipfs

before(function (done) {
// CI takes longer to instantiate the daemon, so we need to increase the
// timeout for the before step
this.timeout(60 * 1000)

common.setup((err, factory) => {
expect(err).to.not.exist()
factory.spawnNode((err, node) => {
expect(err).to.not.exist()
ipfs = node
done()
})
})
})

after((done) => common.teardown(done))

it('should apply a config profile', (done) => {
let diff
waterfall([
(cb) => ipfs.config.profiles.apply('lowpower', cb),
(_diff, cb) => {
diff = _diff
expect(diff.old.Swarm.ConnMgr.LowWater).to.not.equal(diff.new.Swarm.ConnMgr.LowWater)
ipfs.config.get(cb)
},
(newConfig, cb) => {
expect(newConfig.Swarm.ConnMgr.LowWater).to.equal(diff.new.Swarm.ConnMgr.LowWater)
cb()
}
], done)
})
})
}
9 changes: 9 additions & 0 deletions src/config/profiles/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict'
const { createSuite } = require('../../utils/suite')

const tests = {
list: require('./list'),
apply: require('./apply')
}

module.exports = createSuite(tests)
50 changes: 50 additions & 0 deletions src/config/profiles/list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* eslint-env mocha */
'use strict'

const { getDescribe, getIt, expect } = require('../../utils/mocha')
const waterfall = require('async/waterfall')

module.exports = (createCommon, options) => {
const describe = getDescribe(options)
const it = getIt(options)
const common = createCommon()

describe('.config.profiles.list', function () {
this.timeout(30 * 1000)
let ipfs

before(function (done) {
// CI takes longer to instantiate the daemon, so we need to increase the
// timeout for the before step
this.timeout(60 * 1000)

common.setup((err, factory) => {
expect(err).to.not.exist()
factory.spawnNode((err, node) => {
expect(err).to.not.exist()
ipfs = node
done()
})
})
})

after((done) => common.teardown(done))

it('should list config profiles', (done) => {
waterfall([
(cb) => ipfs.config.profiles.list(cb),
(profiles, cb) => {
expect(profiles).to.be.an('array')
expect(profiles).not.to.be.empty()

profiles.forEach(profile => {
expect(profile.name).to.be.a('string')
expect(profile.description).to.be.a('string')
})

cb()
}
], done)
})
})
}