Skip to content

Commit

Permalink
Add tests for the config (#495)
Browse files Browse the repository at this point in the history
Co-authored-by: Itai Steinherz <itaisteinherz@gmail.com>
  • Loading branch information
bunysae and itaisteinherz authored Mar 3, 2020
1 parent eab7bbd commit d9848db
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 0 deletions.
76 changes: 76 additions & 0 deletions test/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import path from 'path';
import test from 'ava';
import sinon from 'sinon';
import proxyquire from 'proxyquire';

const fixtureBasePath = path.resolve('test', 'fixtures', 'config');

const getConfigsWhenGlobalBinaryIsUsed = async homedirStub => {
const pathsPkgDir = [path.resolve(fixtureBasePath, 'pkg-dir'),
path.resolve(fixtureBasePath, 'local1'),
path.resolve(fixtureBasePath, 'local2')];

const promises = [];
pathsPkgDir.forEach(pathPkgDir => {
promises.push(proxyquire('../source/config', {
'is-installed-globally': true,
'pkg-dir': async () => {
return pathPkgDir;
},
os: {
homedir: homedirStub
}
})());
});
return Promise.all(promises);
};

const getConfigsWhenLocalBinaryIsUsed = async pathPkgDir => {
const homedirs = [path.resolve(fixtureBasePath, 'homedir1'),
path.resolve(fixtureBasePath, 'homedir2')];

const promises = [];
homedirs.forEach(homedir => {
promises.push(proxyquire('../source/config', {
'is-installed-globally': false,
'pkg-dir': async () => {
return pathPkgDir;
},
os: {
homedir: () => {
return homedir;
}
}
})());
});
return Promise.all(promises);
};

test('returns config from home directory when global binary is used and `.np-config-json` exists in home directory', async t => {
const homedirStub = sinon.stub();
homedirStub.returns(path.resolve(fixtureBasePath, 'homedir1'));
const configs = await getConfigsWhenGlobalBinaryIsUsed(homedirStub);
configs.forEach(config => t.deepEqual(config, {source: 'homedir/.np-config.json'}));
});

test('returns config from home directory when global binary is used and `.np-config.js` exists in home directory', async t => {
const homedirStub = sinon.stub();
homedirStub.returns(path.resolve(fixtureBasePath, 'homedir2'));
const configs = await getConfigsWhenGlobalBinaryIsUsed(homedirStub);
configs.forEach(config => t.deepEqual(config, {source: 'homedir/.np-config.js'}));
});

test('returns config from package directory when local binary is used and `package.json` exists in package directory', async t => {
const configs = await getConfigsWhenLocalBinaryIsUsed(path.resolve(fixtureBasePath, 'pkg-dir'));
configs.forEach(config => t.deepEqual(config, {source: 'package.json'}));
});

test('returns config from package directory when local binary is used and `.np-config.json` exists in package directory', async t => {
const configs = await getConfigsWhenLocalBinaryIsUsed(path.resolve(fixtureBasePath, 'local1'));
configs.forEach(config => t.deepEqual(config, {source: 'packagedir/.np-config.json'}));
});

test('returns config from package directory when local binary is used and `.np-config.js` exists in package directory', async t => {
const configs = await getConfigsWhenLocalBinaryIsUsed(path.resolve(fixtureBasePath, 'local2'));
configs.forEach(config => t.deepEqual(config, {source: 'packagedir/.np-config.js'}));
});
3 changes: 3 additions & 0 deletions test/fixtures/config/homedir1/.np-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"source": "homedir/.np-config.json"
}
3 changes: 3 additions & 0 deletions test/fixtures/config/homedir2/.np-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
source: 'homedir/.np-config.js'
};
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'
};
6 changes: 6 additions & 0 deletions test/fixtures/config/pkg-dir/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "test-fixtures",
"np": {
"source": "package.json"
}
}

0 comments on commit d9848db

Please sign in to comment.