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 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
85 changes: 85 additions & 0 deletions test/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
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 => {
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});
itaisteinherz marked this conversation as resolved.
Show resolved Hide resolved
});

test('should return config from `.np-config.js` in home-directory when global binary used', async t => {
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'});
});

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});
itaisteinherz marked this conversation as resolved.
Show resolved Hide resolved
});

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');
itaisteinherz marked this conversation as resolved.
Show resolved Hide resolved
}
});
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(fixtureBasePath, 'local');
},
os: {
itaisteinherz marked this conversation as resolved.
Show resolved Hide resolved
homedir: () => {
throw new Error('expected');
}
}
});
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');
}
}
});
await t.throwsAsync(globalConfig());
itaisteinherz marked this conversation as resolved.
Show resolved Hide resolved
t.deepEqual(await localConfig(), {local: true});
});
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 @@
{
"yarn": false
}
4 changes: 4 additions & 0 deletions test/fixtures/config/homedir2/.np-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
yarn: true,
contents: 'dist'
}
3 changes: 3 additions & 0 deletions test/fixtures/config/local/.np-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"local": true
}
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": {
"yarn": true
}
}