Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for the config #495

Merged
merged 5 commits into from
Mar 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
itaisteinherz marked this conversation as resolved.
Show resolved Hide resolved
}
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"
}
}