Skip to content

Commit

Permalink
🐛 Fix: remove global sgcrc before test and revert it after (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
JPeer264 committed Mar 18, 2017
1 parent e59ccaa commit 6bbb556
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions test/getConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,24 @@ import getConfig from '../lib/getConfig';
const cwd = process.cwd();
const homedir = os.homedir();
const fixtures = path.join(cwd, 'test', 'fixtures');
const randomString = Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 10);

let globalExist = false;


// rename global .sgcrc
test.before(() => {
if (fs.existsSync(path.join(homedir, '.sgcrc'))) {
globalExist = true;
fs.renameSync(path.join(homedir, '.sgcrc'), path.join(homedir, `.sgcrc.${randomString}.back`));
}
});

test.after(() => {
if (globalExist) {
fs.renameSync(path.join(homedir, `.sgcrc.${randomString}.back`), path.join(homedir, '.sgcrc'));
}
});

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

packageJson.sgc = sgcrc;

// manipulate local package
fs.renameSync(path.join(cwd, 'package.json'), path.join(cwd, `package.json.${randomString}.back`));
fs.writeFileSync(path.join(cwd, 'package.json'), JSON.stringify(packageJson));

t.deepEqual(getConfig(), sgcrc);

// revert local package
fs.removeSync(path.join(cwd, 'package.json'));
fs.renameSync(path.join(cwd, `package.json.${randomString}.back`), path.join(cwd, 'package.json'));
});

test('read global config', (t) => {
let globalExist = false;
const sgcrc = json.readToObjSync(path.join(fixtures, '.sgcrc'));
const randomString = Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 10);

if (fs.existsSync(path.join(homedir, '.sgcrc'))) {
globalExist = true;
fs.renameSync(path.join(homedir, '.sgcrc'), path.join(homedir, `.sgcrc.${randomString}.back`));
}

fs.writeFileSync(path.join(homedir, '.sgcrc'), JSON.stringify(sgcrc));
t.deepEqual(getConfig(), sgcrc);
fs.removeSync(path.join(homedir, '.sgcrc'));

if (globalExist) {
fs.renameSync(path.join(homedir, `.sgcrc.${randomString}.back`), path.join(homedir, '.sgcrc'));
}
});

0 comments on commit 6bbb556

Please sign in to comment.