-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig-file-spec.js
131 lines (106 loc) · 3.21 KB
/
config-file-spec.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
const fs = require('fs-plus');
const path = require('path');
const temp = require('temp').track();
const dedent = require('dedent');
const ConfigFile = require('../src/config-file');
describe('ConfigFile', () => {
let filePath, configFile, subscription;
beforeEach(async () => {
jasmine.useRealClock();
const tempDir = fs.realpathSync(temp.mkdirSync());
filePath = path.join(tempDir, 'the-config.cson');
});
afterEach(() => {
subscription.dispose();
});
describe('when the file does not exist', () => {
it('returns an empty object from .get()', async () => {
configFile = new ConfigFile(filePath);
subscription = await configFile.watch();
expect(configFile.get()).toEqual({});
});
});
describe('when the file is empty', () => {
it('returns an empty object from .get()', async () => {
writeFileSync(filePath, '');
configFile = new ConfigFile(filePath);
subscription = await configFile.watch();
expect(configFile.get()).toEqual({});
});
});
describe('when the file is updated with valid CSON', () => {
it('notifies onDidChange observers with the data', async () => {
configFile = new ConfigFile(filePath);
subscription = await configFile.watch();
const event = new Promise(resolve => configFile.onDidChange(resolve));
writeFileSync(
filePath,
dedent`
'*':
foo: 'bar'
'javascript':
foo: 'baz'
`
);
expect(await event).toEqual({
'*': { foo: 'bar' },
javascript: { foo: 'baz' }
});
expect(configFile.get()).toEqual({
'*': { foo: 'bar' },
javascript: { foo: 'baz' }
});
});
});
describe('when the file is updated with invalid CSON', () => {
it('notifies onDidError observers', async () => {
configFile = new ConfigFile(filePath);
subscription = await configFile.watch();
const message = new Promise(resolve => configFile.onDidError(resolve));
writeFileSync(
filePath,
dedent`
um what?
`,
2
);
expect(await message).toContain('Failed to load `the-config.cson`');
const event = new Promise(resolve => configFile.onDidChange(resolve));
writeFileSync(
filePath,
dedent`
'*':
foo: 'bar'
'javascript':
foo: 'baz'
`,
4
);
expect(await event).toEqual({
'*': { foo: 'bar' },
javascript: { foo: 'baz' }
});
});
});
describe('ConfigFile.at()', () => {
let path0, path1;
beforeEach(() => {
path0 = filePath;
path1 = path.join(fs.realpathSync(temp.mkdirSync()), 'the-config.cson');
configFile = ConfigFile.at(path0);
});
it('returns an existing ConfigFile', () => {
const cf = ConfigFile.at(path0);
expect(cf).toEqual(configFile);
});
it('creates a new ConfigFile for unrecognized paths', () => {
const cf = ConfigFile.at(path1);
expect(cf).not.toEqual(configFile);
});
});
});
function writeFileSync(filePath, content, seconds = 2) {
const utime = Date.now() / 1000 + seconds;
fs.writeFileSync(filePath, content);
fs.utimesSync(filePath, utime, utime);
}