From d9848db9d1e7dfed8f5cd985967abf7d53bb2ed1 Mon Sep 17 00:00:00 2001 From: Bunyanuch Saengnet <53788417+bunysae@users.noreply.github.com> Date: Tue, 3 Mar 2020 08:33:08 +0100 Subject: [PATCH] Add tests for the config (#495) Co-authored-by: Itai Steinherz --- test/config.js | 76 +++++++++++++++++++ test/fixtures/config/homedir1/.np-config.json | 3 + test/fixtures/config/homedir2/.np-config.js | 3 + test/fixtures/config/local1/.np-config.json | 3 + test/fixtures/config/local2/.np-config.js | 3 + test/fixtures/config/pkg-dir/package.json | 6 ++ 6 files changed, 94 insertions(+) create mode 100644 test/config.js create mode 100644 test/fixtures/config/homedir1/.np-config.json create mode 100644 test/fixtures/config/homedir2/.np-config.js create mode 100644 test/fixtures/config/local1/.np-config.json create mode 100644 test/fixtures/config/local2/.np-config.js create mode 100644 test/fixtures/config/pkg-dir/package.json diff --git a/test/config.js b/test/config.js new file mode 100644 index 00000000..ae02024f --- /dev/null +++ b/test/config.js @@ -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'})); +}); diff --git a/test/fixtures/config/homedir1/.np-config.json b/test/fixtures/config/homedir1/.np-config.json new file mode 100644 index 00000000..a9eede31 --- /dev/null +++ b/test/fixtures/config/homedir1/.np-config.json @@ -0,0 +1,3 @@ +{ + "source": "homedir/.np-config.json" +} diff --git a/test/fixtures/config/homedir2/.np-config.js b/test/fixtures/config/homedir2/.np-config.js new file mode 100644 index 00000000..6934b198 --- /dev/null +++ b/test/fixtures/config/homedir2/.np-config.js @@ -0,0 +1,3 @@ +module.exports = { + source: 'homedir/.np-config.js' +}; diff --git a/test/fixtures/config/local1/.np-config.json b/test/fixtures/config/local1/.np-config.json new file mode 100644 index 00000000..6d62b869 --- /dev/null +++ b/test/fixtures/config/local1/.np-config.json @@ -0,0 +1,3 @@ +{ + "source": "packagedir/.np-config.json" +} diff --git a/test/fixtures/config/local2/.np-config.js b/test/fixtures/config/local2/.np-config.js new file mode 100644 index 00000000..6f891e7a --- /dev/null +++ b/test/fixtures/config/local2/.np-config.js @@ -0,0 +1,3 @@ +module.exports = { + source: 'packagedir/.np-config.js' +}; diff --git a/test/fixtures/config/pkg-dir/package.json b/test/fixtures/config/pkg-dir/package.json new file mode 100644 index 00000000..83127724 --- /dev/null +++ b/test/fixtures/config/pkg-dir/package.json @@ -0,0 +1,6 @@ +{ + "name": "test-fixtures", + "np": { + "source": "package.json" + } +}