-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
test.js
152 lines (134 loc) · 3.75 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import fs from 'node:fs';
import path from 'node:path';
import os from 'node:os';
import test from 'ava';
import Configstore from './index.js';
const configstorePath = new Configstore('configstore-test').path;
const cleanUpFile = () => {
if (fs.existsSync(configstorePath)) {
fs.unlinkSync(configstorePath);
}
};
test.beforeEach(t => {
cleanUpFile();
t.context.config = new Configstore('configstore-test');
});
test('.set() and .get()', t => {
const {config} = t.context;
config.set('foo', 'bar');
config.set('baz.boo', true);
t.is(config.get('foo'), 'bar');
t.is(config.get('baz.boo'), true);
});
test('.set() with object and .get()', t => {
const {config} = t.context;
config.set({
foo1: 'bar1',
foo2: 'bar2',
baz: {
boo: 'foo',
foo: {
bar: 'baz',
},
},
});
t.is(config.get('foo1'), 'bar1');
t.is(config.get('foo2'), 'bar2');
t.deepEqual(config.get('baz'), {
boo: 'foo',
foo: {
bar: 'baz',
},
});
t.is(config.get('baz.boo'), 'foo');
t.deepEqual(config.get('baz.foo'), {bar: 'baz'});
t.is(config.get('baz.foo.bar'), 'baz');
});
test('.has()', t => {
const {config} = t.context;
config.set('foo', '🦄');
config.set('baz.boo', '🦄');
t.true(config.has('foo'));
t.true(config.has('baz.boo'));
t.false(config.has('missing'));
});
test('.delete()', t => {
const {config} = t.context;
config.set('foo', 'bar');
config.set('baz.boo', true);
config.set('baz.foo.bar', 'baz');
config.delete('foo');
t.not(config.get('foo'), 'bar');
config.delete('baz.boo');
t.not(config.get('baz.boo'), true);
config.delete('baz.foo');
t.not(config.get('baz.foo'), {bar: 'baz'});
config.set('foo.bar.baz', {awesome: 'icecream'});
config.set('foo.bar.zoo', {awesome: 'redpanda'});
config.delete('foo.bar.baz');
t.is(config.get('foo.bar.zoo.awesome'), 'redpanda');
});
test('.clear()', t => {
const {config} = t.context;
config.set('foo', 'bar');
config.set('foo1', 'bar1');
config.set('baz.boo', true);
config.clear();
t.is(config.size, 0);
});
test('.all', t => {
const {config} = t.context;
config.set('foo', 'bar');
config.set('baz.boo', true);
t.is(config.all.foo, 'bar');
t.deepEqual(config.all.baz, {boo: true});
});
test('.size', t => {
const {config} = t.context;
config.set('foo', 'bar');
t.is(config.size, 1);
});
test('.path', t => {
const {config} = t.context;
config.set('foo', 'bar');
t.true(fs.existsSync(config.path));
});
test('use default value', t => {
const config = new Configstore('configstore-test', {foo: 'bar'});
t.is(config.get('foo'), 'bar');
});
test('support `globalConfigPath` option', t => {
const config = new Configstore('configstore-test', {}, {globalConfigPath: true});
t.regex(config.path, /configstore-test(\/|\\)config.json$/);
});
test('support `configPath` option', t => {
const customPath = path.join(os.tmpdir(), 'configstore-custom-path', 'foo.json');
const config = new Configstore('ignored-namespace', {}, {
globalConfigPath: true,
configPath: customPath,
});
t.regex(config.path, /configstore-custom-path(\/|\\)foo.json$/);
});
test('ensure `.all` is always an object', t => {
cleanUpFile();
t.notThrows(() => {
t.context.config.get('foo');
});
});
test('the store is NOT created until write', t => {
cleanUpFile();
const config = new Configstore('configstore-test');
t.false(fs.existsSync(config.path));
config.set('foo', 'bar');
t.true(fs.existsSync(config.path));
});
test('ensure necessary sub-directories are created', t => {
const customPath = path.join(fs.mkdtempSync(path.join(os.tmpdir(), 'configstore-recursive-')), 'foo', 'bar', 'baz.json');
const config = new Configstore('ignored-namespace', undefined, {
globalConfigPath: true,
configPath: customPath,
});
t.false(fs.existsSync(config.path));
config.set('foo', 'bar');
t.true(fs.existsSync(config.path));
});