Skip to content

Commit

Permalink
Added edge cases for config test
Browse files Browse the repository at this point in the history
  • Loading branch information
bunysae committed Feb 21, 2020
1 parent b50b94c commit fe98489
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 8 deletions.
60 changes: 52 additions & 8 deletions test/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,31 @@ import test from 'ava';
import sinon from 'sinon';
import proxyquire from 'proxyquire';

const stubOs = sinon.stub(os, 'homedir');
const homedirStub = sinon.stub(os, 'homedir');

test('should return config from `.np-config.json` when installed globally', async t => {
stubOs.returns(path.resolve('test', 'fixtures', 'homedir1'));
test('should return config from `.np-config.json` in home-directory when global binary used', async t => {
homedirStub.returns(path.resolve('test', 'fixtures', 'homedir1'));
const config = proxyquire('../source/config', {
'is-installed-globally': true,
'pkg-dir': async () => {}
'pkg-dir': async () => {
return path.resolve('test', 'fixtures', 'pkg-dir');
}
});
t.deepEqual(await config(), {yarn: false});
});

test('should return config from `.np-config.js` when installed globally', async t => {
stubOs.returns(path.resolve('test', 'fixtures', 'homedir2'));
test('should return config from `.np-config.js` in home-directory when global binary used', async t => {
homedirStub.returns(path.resolve('test', 'fixtures', 'homedir2'));
const config = proxyquire('../source/config', {
'is-installed-globally': true,
'pkg-dir': async () => {}
'pkg-dir': async () => {
return path.resolve('test', 'fixtures', 'pkg-dir');
}
});
t.deepEqual(await config(), {yarn: true, contents: 'dist'});
});

test('should return config from `package.json` when installed localy', async t => {
test('should return config from `package.json` when local binary used', async t => {
const config = proxyquire('../source/config', {
'is-installed-globally': false,
'pkg-dir': async () => {
Expand All @@ -33,3 +37,43 @@ test('should return config from `package.json` when installed localy', async t =
});
t.deepEqual(await config(), {yarn: true});
});

test('should only return config from home-directory when global binary used', async t => {
homedirStub.returns(path.resolve('test', 'fixtures', 'homedir1'));
const globalConfig = proxyquire('../source/config', {
'is-installed-globally': true,
'pkg-dir': async () => {
throw new Error('access local config');
}
});
const localConfig = proxyquire('../source/config', {
'is-installed-globally': false,
'pkg-dir': async () => {
throw new Error('expected');
}
});
t.deepEqual(await globalConfig(), {yarn: false});
await t.throwsAsync(localConfig());
});

test('should only return config from local package when local binary used', async t => {
const globalConfig = proxyquire('../source/config', {
'is-installed-globally': true,
'pkg-dir': async () => {
return path.resolve('test', 'fixtures', 'local_config');
},
os: {
homedir: () => {
throw new Error('access global config');
}
}
});
const localConfig = proxyquire('../source/config', {
'is-installed-globally': false,
'pkg-dir': async () => {
return path.resolve('test', 'fixtures', 'local_config');
}
});
await t.throwsAsync(globalConfig());
t.deepEqual(await localConfig(), {local: true});
});
3 changes: 3 additions & 0 deletions test/fixtures/local_config/.np-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"local": true
}

0 comments on commit fe98489

Please sign in to comment.