Skip to content

Commit

Permalink
Refactoring tests for config
Browse files Browse the repository at this point in the history
  • Loading branch information
bunysae committed Feb 27, 2020
1 parent 52556e4 commit 09a6a89
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 74 deletions.
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,15 @@
"proxyquire": "^2.1.0",
"sinon": "^8.0.1",
"xo": "^0.25.3"
},
"xo": {
"overrides": [
{
"files": "test/config.js",
"rules": {
"no-await-in-loop": "off"
}
}
]
}
}
161 changes: 95 additions & 66 deletions test/config.js
Original file line number Diff line number Diff line change
@@ -1,85 +1,114 @@
import path from 'path';
import os from 'os';
import test from 'ava';
import sinon from 'sinon';
import proxyquire from 'proxyquire';

const homedirStub = sinon.stub(os, 'homedir');
const fixtureBasePath = path.resolve('test', 'fixtures', 'config');

test('should return config from `.np-config.json` in home-directory when global binary used', async t => {
test('should return config only from home-directory when global binary used and `.np-config-json` in home-directory exists', async t => {
const homedirStub = sinon.stub();
homedirStub.returns(path.resolve(fixtureBasePath, 'homedir1'));
const config = proxyquire('../source/config', {
'is-installed-globally': true,
'pkg-dir': async () => {
return path.resolve(fixtureBasePath, 'pkg-dir');
}
});
t.deepEqual(await config(), {yarn: false});
const pkgDirStub = sinon.stub();
pkgDirStub.onFirstCall().returns(path.resolve(fixtureBasePath, 'pkg-dir'));
pkgDirStub.onSecondCall().returns(path.resolve(fixtureBasePath, 'local1'));
pkgDirStub.onThirdCall().returns(path.resolve(fixtureBasePath, 'local2'));

let i = 0;
while (i < 3) {
const config = proxyquire('../source/config', {
'is-installed-globally': true,
'pkg-dir': async () => {
return pkgDirStub();
},
os: {
homedir: homedirStub
}
});
t.deepEqual(await config(), {source: 'homedir/.np-config.json'});
i++;
}
});

test('should return config from `.np-config.js` in home-directory when global binary used', async t => {
test('should return config only from home-directory when global binary used and `.np-config.js` in home-directory exists', async t => {
const homedirStub = sinon.stub();
homedirStub.returns(path.resolve(fixtureBasePath, 'homedir2'));
const config = proxyquire('../source/config', {
'is-installed-globally': true,
'pkg-dir': async () => {
return path.resolve(fixtureBasePath, 'pkg-dir');
}
});
t.deepEqual(await config(), {yarn: true, contents: 'dist'});
});
const pkgDirStub = sinon.stub();
pkgDirStub.onFirstCall().returns(path.resolve(fixtureBasePath, 'pkg-dir'));
pkgDirStub.onSecondCall().returns(path.resolve(fixtureBasePath, 'local1'));
pkgDirStub.onThirdCall().returns(path.resolve(fixtureBasePath, 'local2'));

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 () => {
return path.resolve(fixtureBasePath, 'pkg-dir');
}
});
t.deepEqual(await config(), {yarn: true});
let i = 0;
while (i < 3) {
const config = proxyquire('../source/config', {
'is-installed-globally': true,
'pkg-dir': async () => {
return pkgDirStub();
},
os: {
homedir: homedirStub
}
});
t.deepEqual(await config(), {source: 'homedir/.np-config.js'});
i++;
}
});

test('should only return config from home-directory when global binary used', async t => {
homedirStub.returns(path.resolve(fixtureBasePath, '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 return config only from package-directory when local binary used and `package.json` in package-directory exists', async t => {
const homedirStub = sinon.stub();
homedirStub.onFirstCall().returns(path.resolve(fixtureBasePath, 'homedir1'));
homedirStub.onSecondCall().returns(path.resolve(fixtureBasePath, 'homedir2'));
let i = 0;
while (i < 2) {
const config = proxyquire('../source/config', {
'is-installed-globally': false,
'pkg-dir': async () => {
return path.resolve(fixtureBasePath, 'pkg-dir');
},
os: {
homedir: homedirStub
}
});
t.deepEqual(await config(), {source: 'package.json'});
i++;
}
});

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(fixtureBasePath, 'local');
},
os: {
homedir: () => {
throw new Error('expected');
test('should return config only from package-directory when local binary used and `.np-config.json` in package-directory exists', async t => {
const homedirStub = sinon.stub();
homedirStub.onFirstCall().returns(path.resolve(fixtureBasePath, 'homedir1'));
homedirStub.onSecondCall().returns(path.resolve(fixtureBasePath, 'homedir2'));
let i = 0;
while (i < 2) {
const config = proxyquire('../source/config', {
'is-installed-globally': false,
'pkg-dir': async () => {
return path.resolve(fixtureBasePath, 'local1');
},
os: {
homedir: homedirStub
}
}
});
const localConfig = proxyquire('../source/config', {
'is-installed-globally': false,
'pkg-dir': async () => {
return path.resolve(fixtureBasePath, 'local');
},
os: {
homedir: () => {
throw new Error('access global config in home-directory');
});
t.deepEqual(await config(), {source: 'packagedir/.np-config.json'});
i++;
}
});

test('should return config only from package-directory when local binary used and `.np-config.js` in package-directory exists', async t => {
const homedirStub = sinon.stub();
homedirStub.onFirstCall().returns(path.resolve(fixtureBasePath, 'homedir1'));
homedirStub.onSecondCall().returns(path.resolve(fixtureBasePath, 'homedir2'));
let i = 0;
while (i < 2) {
const config = proxyquire('../source/config', {
'is-installed-globally': false,
'pkg-dir': async () => {
return path.resolve(fixtureBasePath, 'local2');
},
os: {
homedir: homedirStub
}
}
});
await t.throwsAsync(globalConfig());
t.deepEqual(await localConfig(), {local: true});
});
t.deepEqual(await config(), {source: 'packagedir/.np-config.js'});
i++;
}
});
2 changes: 1 addition & 1 deletion test/fixtures/config/homedir1/.np-config.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"yarn": false
"source": "homedir/.np-config.json"
}
5 changes: 2 additions & 3 deletions test/fixtures/config/homedir2/.np-config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
module.exports = {
yarn: true,
contents: 'dist'
}
source: 'homedir/.np-config.js'
};
3 changes: 0 additions & 3 deletions test/fixtures/config/local/.np-config.json

This file was deleted.

3 changes: 3 additions & 0 deletions test/fixtures/config/local1/.np-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"source": "packagedir/.np-config.json"
}
3 changes: 3 additions & 0 deletions test/fixtures/config/local2/.np-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
source: 'packagedir/.np-config.js'
};
2 changes: 1 addition & 1 deletion test/fixtures/config/pkg-dir/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "test-fixtures",
"np": {
"yarn": true
"source": "package.json"
}
}

0 comments on commit 09a6a89

Please sign in to comment.