Skip to content

Commit 6bbb556

Browse files
committed
🐛 Fix: remove global sgcrc before test and revert it after (#12)
1 parent e59ccaa commit 6bbb556

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

test/getConfig.js

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,24 @@ import getConfig from '../lib/getConfig';
88
const cwd = process.cwd();
99
const homedir = os.homedir();
1010
const fixtures = path.join(cwd, 'test', 'fixtures');
11+
const randomString = Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 10);
12+
13+
let globalExist = false;
14+
15+
16+
// rename global .sgcrc
17+
test.before(() => {
18+
if (fs.existsSync(path.join(homedir, '.sgcrc'))) {
19+
globalExist = true;
20+
fs.renameSync(path.join(homedir, '.sgcrc'), path.join(homedir, `.sgcrc.${randomString}.back`));
21+
}
22+
});
23+
24+
test.after(() => {
25+
if (globalExist) {
26+
fs.renameSync(path.join(homedir, `.sgcrc.${randomString}.back`), path.join(homedir, '.sgcrc'));
27+
}
28+
});
1129

1230
test('read config from a specific path', (t) => {
1331
t.deepEqual(getConfig(path.join(fixtures, '.sgcrc')), json.readToObjSync(path.join(fixtures, '.sgcrc')));
@@ -20,31 +38,24 @@ test('read config from a .sgcrc_default', (t) => {
2038
test('read config from package.json', (t) => {
2139
const sgcrc = json.readToObjSync(path.join(fixtures, '.sgcrc'));
2240
const packageJson = json.readToObjSync(path.join(cwd, 'package.json'));
23-
const randomString = Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 10);
41+
2442
packageJson.sgc = sgcrc;
2543

44+
// manipulate local package
2645
fs.renameSync(path.join(cwd, 'package.json'), path.join(cwd, `package.json.${randomString}.back`));
2746
fs.writeFileSync(path.join(cwd, 'package.json'), JSON.stringify(packageJson));
47+
2848
t.deepEqual(getConfig(), sgcrc);
49+
50+
// revert local package
2951
fs.removeSync(path.join(cwd, 'package.json'));
3052
fs.renameSync(path.join(cwd, `package.json.${randomString}.back`), path.join(cwd, 'package.json'));
3153
});
3254

3355
test('read global config', (t) => {
34-
let globalExist = false;
3556
const sgcrc = json.readToObjSync(path.join(fixtures, '.sgcrc'));
36-
const randomString = Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 10);
37-
38-
if (fs.existsSync(path.join(homedir, '.sgcrc'))) {
39-
globalExist = true;
40-
fs.renameSync(path.join(homedir, '.sgcrc'), path.join(homedir, `.sgcrc.${randomString}.back`));
41-
}
4257

4358
fs.writeFileSync(path.join(homedir, '.sgcrc'), JSON.stringify(sgcrc));
4459
t.deepEqual(getConfig(), sgcrc);
4560
fs.removeSync(path.join(homedir, '.sgcrc'));
46-
47-
if (globalExist) {
48-
fs.renameSync(path.join(homedir, `.sgcrc.${randomString}.back`), path.join(homedir, '.sgcrc'));
49-
}
5061
});

0 commit comments

Comments
 (0)