pulsar/spec/config-file-spec.js

132 lines
3.2 KiB
JavaScript
Raw Normal View History

2019-05-31 19:33:56 +03:00
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', () => {
2019-05-31 19:33:56 +03:00
let filePath, configFile, subscription;
beforeEach(async () => {
2019-05-31 19:33:56 +03:00
jasmine.useRealClock();
const tempDir = fs.realpathSync(temp.mkdirSync());
filePath = path.join(tempDir, 'the-config.cson');
});
afterEach(() => {
2019-05-31 19:33:56 +03:00
subscription.dispose();
});
describe('when the file does not exist', () => {
it('returns an empty object from .get()', async () => {
2019-05-31 19:33:56 +03:00
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 () => {
2019-05-31 19:33:56 +03:00
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 () => {
2019-05-31 19:33:56 +03:00
configFile = new ConfigFile(filePath);
subscription = await configFile.watch();
2019-05-31 19:33:56 +03:00
const event = new Promise(resolve => configFile.onDidChange(resolve));
2019-02-22 10:55:17 +03:00
writeFileSync(
filePath,
dedent`
'*':
foo: 'bar'
'javascript':
foo: 'baz'
2019-02-22 10:55:17 +03:00
`
2019-05-31 19:33:56 +03:00
);
expect(await event).toEqual({
2019-02-22 10:55:17 +03:00
'*': { foo: 'bar' },
javascript: { foo: 'baz' }
2019-05-31 19:33:56 +03:00
});
expect(configFile.get()).toEqual({
2019-02-22 10:55:17 +03:00
'*': { foo: 'bar' },
javascript: { foo: 'baz' }
2019-05-31 19:33:56 +03:00
});
});
});
2018-03-27 15:54:16 +03:00
describe('when the file is updated with invalid CSON', () => {
it('notifies onDidError observers', async () => {
2019-05-31 19:33:56 +03:00
configFile = new ConfigFile(filePath);
subscription = await configFile.watch();
2019-05-31 19:33:56 +03:00
const message = new Promise(resolve => configFile.onDidError(resolve));
2019-02-22 10:55:17 +03:00
writeFileSync(
filePath,
dedent`
um what?
2019-02-22 10:55:17 +03:00
`,
2
2019-05-31 19:33:56 +03:00
);
2019-05-31 19:33:56 +03:00
expect(await message).toContain('Failed to load `the-config.cson`');
2019-05-31 19:33:56 +03:00
const event = new Promise(resolve => configFile.onDidChange(resolve));
2019-02-22 10:55:17 +03:00
writeFileSync(
filePath,
dedent`
'*':
foo: 'bar'
'javascript':
foo: 'baz'
2019-02-22 10:55:17 +03:00
`,
4
2019-05-31 19:33:56 +03:00
);
expect(await event).toEqual({
2019-02-22 10:55:17 +03:00
'*': { foo: 'bar' },
javascript: { foo: 'baz' }
2019-05-31 19:33:56 +03:00
});
});
});
describe('ConfigFile.at()', () => {
2019-05-31 19:33:56 +03:00
let path0, path1;
beforeEach(() => {
2019-05-31 19:33:56 +03:00
path0 = filePath;
path1 = path.join(fs.realpathSync(temp.mkdirSync()), 'the-config.cson');
2019-05-31 19:33:56 +03:00
configFile = ConfigFile.at(path0);
});
it('returns an existing ConfigFile', () => {
2019-05-31 19:33:56 +03:00
const cf = ConfigFile.at(path0);
expect(cf).toEqual(configFile);
});
it('creates a new ConfigFile for unrecognized paths', () => {
2019-05-31 19:33:56 +03:00
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);
}