mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 10:45:47 +00:00
feature: server: cloudcmd: get rid of mock-require
This commit is contained in:
parent
eff34215ee
commit
e080a54022
2 changed files with 45 additions and 63 deletions
|
|
@ -1,5 +1,6 @@
|
|||
'use strict';
|
||||
|
||||
const fullstore = require('fullstore');
|
||||
const process = require('process');
|
||||
const DIR = `${__dirname}/`;
|
||||
const DIR_COMMON = `${DIR}../common/`;
|
||||
|
|
@ -34,11 +35,12 @@ const nomine = require('nomine');
|
|||
const fileop = require('@cloudcmd/fileop');
|
||||
const DIR_ROOT = `${DIR}../`;
|
||||
|
||||
const isDev = process.env.NODE_ENV === 'development';
|
||||
const getDist = (isDev) => isDev ? 'dist-dev' : 'dist';
|
||||
|
||||
const isDev = fullstore(process.env.NODE_ENV === 'development');
|
||||
|
||||
const getIndexPath = (isDev) => path.join(DIR, '..', `${getDist(isDev)}/index.html`);
|
||||
const html = fs.readFileSync(getIndexPath(isDev), 'utf8');
|
||||
const html = fs.readFileSync(getIndexPath(isDev()), 'utf8');
|
||||
|
||||
const initAuth = currify(_initAuth);
|
||||
const notEmpty = (a) => a;
|
||||
|
|
@ -255,9 +257,10 @@ function logout(req, res, next) {
|
|||
res.sendStatus(401);
|
||||
}
|
||||
|
||||
module.exports._isDev = isDev;
|
||||
module.exports._replaceDist = replaceDist;
|
||||
function replaceDist(url) {
|
||||
if (!isDev)
|
||||
if (!isDev())
|
||||
return url;
|
||||
|
||||
return url.replace(/^\/dist\//, '/dist-dev/');
|
||||
|
|
|
|||
|
|
@ -1,16 +1,9 @@
|
|||
'use strict';
|
||||
|
||||
const process = require('process');
|
||||
const path = require('path');
|
||||
|
||||
const {test, stub} = require('supertape');
|
||||
const cloudcmd = require('./cloudcmd.js');
|
||||
|
||||
const {reRequire} = require('mock-require');
|
||||
|
||||
const DIR = './';
|
||||
const cloudcmdPath = `${DIR}cloudcmd`;
|
||||
|
||||
const cloudcmd = require(cloudcmdPath);
|
||||
const {request} = require('serve-once')(cloudcmd, {
|
||||
config: {
|
||||
auth: false,
|
||||
|
|
@ -19,6 +12,8 @@ const {request} = require('serve-once')(cloudcmd, {
|
|||
});
|
||||
|
||||
const {
|
||||
_isDev,
|
||||
_replaceDist,
|
||||
createConfigManager,
|
||||
_getPrefix,
|
||||
_initAuth,
|
||||
|
|
@ -26,13 +21,13 @@ const {
|
|||
|
||||
test('cloudcmd: defaults: config', (t) => {
|
||||
const configManager = createConfigManager();
|
||||
|
||||
|
||||
configManager('configDialog', false);
|
||||
|
||||
|
||||
cloudcmd({
|
||||
configManager,
|
||||
});
|
||||
|
||||
|
||||
t.notOk(configManager('configDialog'), 'should not override config with defaults');
|
||||
t.end();
|
||||
});
|
||||
|
|
@ -40,11 +35,11 @@ test('cloudcmd: defaults: config', (t) => {
|
|||
test('cloudcmd: defaults: console', (t) => {
|
||||
const configManager = createConfigManager();
|
||||
configManager('console', false);
|
||||
|
||||
|
||||
cloudcmd({
|
||||
configManager,
|
||||
});
|
||||
|
||||
|
||||
t.notOk(configManager('console'), 'should not override config with defaults');
|
||||
t.end();
|
||||
});
|
||||
|
|
@ -52,7 +47,7 @@ test('cloudcmd: defaults: console', (t) => {
|
|||
test('cloudcmd: getPrefix', (t) => {
|
||||
const value = 'hello';
|
||||
const result = _getPrefix(value);
|
||||
|
||||
|
||||
t.equal(result, value);
|
||||
t.end();
|
||||
});
|
||||
|
|
@ -61,7 +56,7 @@ test('cloudcmd: getPrefix: function', (t) => {
|
|||
const value = 'hello';
|
||||
const fn = () => value;
|
||||
const result = _getPrefix(fn);
|
||||
|
||||
|
||||
t.equal(result, value);
|
||||
t.end();
|
||||
});
|
||||
|
|
@ -70,38 +65,34 @@ test('cloudcmd: getPrefix: function: empty', (t) => {
|
|||
const value = null;
|
||||
const fn = () => value;
|
||||
const result = _getPrefix(fn);
|
||||
|
||||
|
||||
t.equal(result, '');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('cloudcmd: replaceDist', (t) => {
|
||||
const {NODE_ENV} = process.env;
|
||||
|
||||
process.env.NODE_ENV = 'development';
|
||||
|
||||
const {_replaceDist} = reRequire(cloudcmdPath);
|
||||
|
||||
const currentIsDev = _isDev();
|
||||
|
||||
_isDev(true);
|
||||
const url = '/dist/hello';
|
||||
const result = _replaceDist(url);
|
||||
const expected = '/dist-dev/hello';
|
||||
|
||||
process.env.NODE_ENV = NODE_ENV;
|
||||
|
||||
|
||||
_isDev(currentIsDev);
|
||||
|
||||
t.equal(result, expected);
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('cloudcmd: replaceDist: !isDev', (t) => {
|
||||
const url = '/dist/hello';
|
||||
const cloudcmdPath = `${DIR}cloudcmd`;
|
||||
|
||||
const reset = cleanNodeEnv();
|
||||
const {_replaceDist} = reRequire(cloudcmdPath);
|
||||
|
||||
const currentIsDev = _isDev();
|
||||
_isDev(false);
|
||||
const result = _replaceDist(url);
|
||||
|
||||
reset();
|
||||
|
||||
|
||||
_isDev(currentIsDev);
|
||||
|
||||
t.equal(result, url);
|
||||
t.end();
|
||||
});
|
||||
|
|
@ -109,18 +100,18 @@ test('cloudcmd: replaceDist: !isDev', (t) => {
|
|||
test('cloudcmd: auth: reject', (t) => {
|
||||
const accept = stub();
|
||||
const reject = stub();
|
||||
|
||||
|
||||
const config = createConfigManager();
|
||||
|
||||
|
||||
const username = 'root';
|
||||
const password = 'toor';
|
||||
|
||||
|
||||
config('auth', true);
|
||||
config('username', username);
|
||||
config('password', password);
|
||||
|
||||
|
||||
_initAuth(config, accept, reject, username, 'abc');
|
||||
|
||||
|
||||
t.ok(reject.called, 'should reject');
|
||||
t.end();
|
||||
});
|
||||
|
|
@ -128,18 +119,18 @@ test('cloudcmd: auth: reject', (t) => {
|
|||
test('cloudcmd: auth: accept', (t) => {
|
||||
const accept = stub();
|
||||
const reject = stub();
|
||||
|
||||
|
||||
const username = 'root';
|
||||
const password = 'toor';
|
||||
const auth = true;
|
||||
|
||||
|
||||
const config = createConfigManager();
|
||||
config('username', username);
|
||||
config('password', password);
|
||||
config('auth', auth);
|
||||
|
||||
|
||||
_initAuth(config, accept, reject, username, password);
|
||||
|
||||
|
||||
t.ok(accept.called, 'should accept');
|
||||
t.end();
|
||||
});
|
||||
|
|
@ -147,18 +138,18 @@ test('cloudcmd: auth: accept', (t) => {
|
|||
test('cloudcmd: auth: accept: no auth', (t) => {
|
||||
const accept = stub();
|
||||
const reject = stub();
|
||||
|
||||
|
||||
const auth = false;
|
||||
const username = 'root';
|
||||
const password = 'toor';
|
||||
|
||||
|
||||
const config = createConfigManager();
|
||||
config('username', username);
|
||||
config('password', password);
|
||||
config('auth', auth);
|
||||
|
||||
|
||||
_initAuth(config, accept, reject, username, password);
|
||||
|
||||
|
||||
t.ok(accept.called, 'should accept');
|
||||
t.end();
|
||||
});
|
||||
|
|
@ -166,7 +157,7 @@ test('cloudcmd: auth: accept: no auth', (t) => {
|
|||
test('cloudcmd: getIndexPath: production', (t) => {
|
||||
const isDev = false;
|
||||
const name = path.join(__dirname, '..', 'dist', 'index.html');
|
||||
|
||||
|
||||
t.equal(cloudcmd._getIndexPath(isDev), name);
|
||||
t.end();
|
||||
});
|
||||
|
|
@ -174,26 +165,14 @@ test('cloudcmd: getIndexPath: production', (t) => {
|
|||
test('cloudcmd: getIndexPath: development', (t) => {
|
||||
const isDev = true;
|
||||
const name = path.join(__dirname, '..', 'dist-dev', 'index.html');
|
||||
|
||||
|
||||
t.equal(cloudcmd._getIndexPath(isDev), name);
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('cloudcmd: sw', async (t) => {
|
||||
const {status} = await request.get('/sw.js');
|
||||
|
||||
|
||||
t.equal(status, 200, 'should return sw');
|
||||
t.end();
|
||||
});
|
||||
|
||||
function cleanNodeEnv() {
|
||||
const {NODE_ENV} = process.env;
|
||||
|
||||
process.env.NODE_ENV = '';
|
||||
|
||||
const reset = () => {
|
||||
process.env.NODE_ENV = NODE_ENV;
|
||||
};
|
||||
|
||||
return reset;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue