UNPKG

2.85 kBJavaScriptView Raw
1/*
2 * file-store-test.js: Tests for the nconf File store.
3 *
4 * (C) 2011, Charlie Robbins
5 *
6 */
7
8var assert = require('assert'),
9 fs = require('fs'),
10 path = require('path'),
11 spawn = require('child_process').spawn,
12 vows = require('vows'),
13 helpers = require('./helpers'),
14 nconf = require('../lib/nconf');
15
16var fixturesDir = path.join(__dirname, 'fixtures'),
17 mergeFixtures = path.join(fixturesDir, 'merge'),
18 files = [path.join(mergeFixtures, 'file1.json'), path.join(mergeFixtures, 'file2.json')],
19 override = JSON.parse(fs.readFileSync(files[0]), 'utf8');
20
21vows.describe('nconf/provider').addBatch({
22 "When using nconf": {
23 "an instance of 'nconf.Provider'": {
24 "calling the use() method with the same store type and different options": {
25 topic: new nconf.Provider().use('file', { file: files[0] }),
26 "should use a new instance of the store type": function (provider) {
27 var old = provider.file;
28
29 assert.equal(provider.file.file, files[0]);
30 provider.use('file', { file: files[1] });
31
32 assert.notStrictEqual(old, provider.file);
33 assert.equal(provider.file.file, files[1]);
34 }
35 },
36 "when 'argv' is true": helpers.assertSystemConf({
37 script: path.join(fixturesDir, 'scripts', 'provider-argv.js'),
38 argv: ['--something', 'foobar']
39 }),
40 "when 'env' is true": helpers.assertSystemConf({
41 script: path.join(fixturesDir, 'scripts', 'provider-env.js'),
42 env: { SOMETHING: 'foobar' }
43 }),
44 },
45 "the default nconf provider": {
46 "when 'argv' is set to true": helpers.assertSystemConf({
47 script: path.join(fixturesDir, 'scripts', 'nconf-argv.js'),
48 argv: ['--something', 'foobar'],
49 env: { SOMETHING: true }
50 }),
51 "when 'env' is set to true": helpers.assertSystemConf({
52 script: path.join(fixturesDir, 'scripts', 'nconf-env.js'),
53 env: { SOMETHING: 'foobar' }
54 })
55 }
56 }
57}).addBatch({
58 "When using nconf": {
59 "an instance of 'nconf.Provider'": {
60 "the merge() method": {
61 topic: new nconf.Provider().use('file', { file: files[1] }),
62 "should have the result merged in": function (provider) {
63 provider.load();
64 provider.merge(override);
65 helpers.assertMerged(null, provider.file.store);
66 }
67 },
68 "when sources are passed in": {
69 topic: new nconf.Provider({
70 sources: {
71 user: {
72 type: 'file',
73 file: files[0]
74 },
75 global: {
76 type: 'file',
77 file: files[1]
78 }
79 }
80 }),
81 "should have the result merged in": function (provider) {
82 helpers.assertMerged(null, provider.load());
83 }
84 }
85 }
86 }
87}).export(module);
\No newline at end of file