forked from deprecate/metal-soy-critic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.ts
93 lines (69 loc) · 2.52 KB
/
config.ts
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
import * as process from 'process';
import * as Config from '../config';
describe('config', () => {
describe('getConfigFilePath', () => {
const cwd = process.cwd();
afterAll(() => {
process.chdir(cwd);
});
it('should return null if no file is found', () => {
const path = Config.getConfigFilePath();
expect(path).toBeNull();
});
it('should return a string if a file is found', () => {
process.chdir('./test/fixtures/config');
const path = Config.getConfigFilePath();
expect(path).toMatch(/\.soycriticrc/);
});
});
describe('readConfig', () => {
const cwd = process.cwd();
afterEach(() => {
process.chdir(cwd);
});
it('should return a default configuration if no file is found', () => {
const config = Config.readConfig();
expect(config).toBeInstanceOf(Object);
expect(config.callToImport).toEqual([{regex: '(.*)', replace: '{$1}'}]);
});
it('should return a configuration specified in a file', () => {
process.chdir('./test/fixtures/config');
const config = Config.readConfig();
expect(config.callToImport).toEqual([{regex: '(\\S+)', replace: '{$1|param}'}]);
});
it('should read config files with a json extension', () => {
process.chdir('./test/fixtures/config-json');
const config = Config.readConfig();
expect(config.callToImport).toEqual([{regex: 'json', replace: '{$1|param}'}]);
});
it('should return a converted configuration specified in a file if is found in the old way', () => {
process.chdir('./test/fixtures/deprecated-config');
const config = Config.readConfig();
expect(config.callToImport).toEqual([{regex: '(\\S+)', replace: '{$1|param}'}]);
});
});
describe('isRegex', () => {
it('should return true if the string is a regex', () => {
expect(Config.isRegex('foo')).toBe(true);
expect(Config.isRegex('foo.*')).toBe(true);
expect(Config.isRegex('foo(.*')).toBe(false);
});
});
describe('validateConfig', () => {
it('should return the config for valid a config', () => {
expect(Config.validateConfig(Config.DEFAULT_CONFIG)).toMatchObject(Config.DEFAULT_CONFIG);
});
it('should throw an Error if the config is invalid', () => {
const invalidConfig = {
callToImport: [
{
regex: '(asd',
replace: 'bar',
},
],
implicitParams: {},
};
expect(() => Config.validateConfig(invalidConfig)).toThrowError();
});
});
});