forked from JPeer264/node-semantic-git-commit-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetConfig.js
More file actions
75 lines (57 loc) · 2.38 KB
/
Copy pathgetConfig.js
File metadata and controls
75 lines (57 loc) · 2.38 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
import test from 'ava';
import fs from 'fs-extra';
import json from 'json-extra';
import os from 'os';
import path from 'path';
import getConfig from '../lib/getConfig';
const cwd = process.cwd();
const homedir = os.homedir();
const fixtures = path.join(cwd, 'test', '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
test.before(() => {
// 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'));
});
test.after.always(() => {
// 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'));
});
test('read config from a specific path', (t) => {
t.deepEqual(getConfig(path.join(fixtures, '.sgcrc')), json.readToObjSync(path.join(fixtures, '.sgcrc')));
});
test('read config from a .sgcrc_default', (t) => {
const globalConfig = json.readToObjSync(path.join(cwd, '.sgcrc_default'));
t.deepEqual(getConfig(), globalConfig);
});
test('read config from package.json', (t) => {
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 = getConfig();
// 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'));
t.deepEqual(config, sgcrc);
});
test('read global config', (t) => {
const sgcrc = json.readToObjSync(path.join(fixtures, '.sgcrc'));
fs.writeFileSync(path.join(homedir, '.sgcrc'), JSON.stringify(sgcrc));
t.deepEqual(getConfig(), sgcrc);
fs.removeSync(path.join(homedir, '.sgcrc'));
});