-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathConfig.ts
More file actions
97 lines (73 loc) · 3.26 KB
/
Copy pathConfig.ts
File metadata and controls
97 lines (73 loc) · 3.26 KB
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
import fs from 'fs-extra';
import * as json from 'json-extra';
import os from 'os';
import path from 'path';
import Config from '../lib/Config';
const cwd = process.cwd();
const homedir = os.homedir();
const fixtures = path.join(cwd, '__tests__', 'fixtures');
const date = new Date();
const datetime = date.toISOString().slice(0, 10);
const randomString = Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 4);
let globalExist = false;
// rename global .sgcrc
beforeAll(() => {
// rename global sgcrc
if (fs.existsSync(path.join(homedir, '.sgcrc'))) {
globalExist = true;
fs.renameSync(path.join(homedir, '.sgcrc'), path.join(homedir, `.sgcrc.${randomString}-${datetime}.back`));
}
// rename local sgcrc
fs.renameSync(path.join(cwd, '.sgcrc'), path.join(cwd, '.sgcrc_default'));
});
afterAll(() => {
// rename global sgrc
if (globalExist) {
fs.renameSync(path.join(homedir, `.sgcrc.${randomString}-${datetime}.back`), path.join(homedir, '.sgcrc'));
}
// rename local sgcrc
fs.renameSync(path.join(cwd, '.sgcrc_default'), path.join(cwd, '.sgcrc'));
});
it('read config from a specific path', () => {
expect(new Config(fixtures).config).toEqual(json.readToObjSync(path.join(fixtures, '.sgcrc')));
});
it('read config from a .sgcrc_default', () => {
const globalConfig = json.readToObjSync(path.join(cwd, '.sgcrc_default'));
expect(new Config().config).toEqual(globalConfig);
});
it('read config from package.json', () => {
const sgcrc = json.readToObjSync(path.join(fixtures, '.sgcrc'));
const packageJson = json.readToObjSync(path.join(cwd, 'package.json'));
packageJson.sgc = sgcrc;
// manipulate local package
fs.renameSync(path.join(cwd, 'package.json'), path.join(cwd, `package.json.${randomString}-${datetime}.back`));
fs.writeFileSync(path.join(cwd, 'package.json'), JSON.stringify(packageJson));
const { config } = new Config();
// revert local package
fs.removeSync(path.join(cwd, 'package.json'));
fs.renameSync(path.join(cwd, `package.json.${randomString}-${datetime}.back`), path.join(cwd, 'package.json'));
expect(config).toEqual(sgcrc);
});
it('read global config', () => {
const sgcrc = json.readToObjSync(path.join(fixtures, '.sgcrc'));
fs.writeFileSync(path.join(homedir, '.sgcrc'), JSON.stringify(sgcrc));
expect(new Config().config).toEqual(sgcrc);
fs.removeSync(path.join(homedir, '.sgcrc'));
});
it('read local config from `sgc.config.js`', () => {
const sgcrc = json.readToObjSync(path.join(fixtures, '.sgcrc'));
fs.writeFileSync(path.join(cwd, 'sgc.config.js'), `module.exports = (${JSON.stringify(sgcrc)})`);
expect(new Config().config).toEqual(sgcrc);
fs.removeSync(path.join(cwd, 'sgc.config.js'));
});
it('read global config from `sgc.config.js`', () => {
const sgcrc = json.readToObjSync(path.join(fixtures, '.sgcrc'));
fs.writeFileSync(path.join(homedir, 'sgc.config.js'), `module.exports = (${JSON.stringify(sgcrc)})`);
expect(new Config().config).toEqual(sgcrc);
fs.removeSync(path.join(homedir, 'sgc.config.js'));
});
it('read a .sgcrc_default from a deep nested cwd', () => {
const deepCwd = path.join(fixtures, 'very', 'deep', 'directory');
const fixturesConfig = json.readToObjSync(path.join(fixtures, '.sgcrc'));
expect(new Config(deepCwd).config).toEqual(fixturesConfig);
});