From b7816c061ff50f5b4d00019f6e31b1dcdac44ed1 Mon Sep 17 00:00:00 2001 From: Moos Date: Tue, 17 Apr 2018 23:20:24 -0700 Subject: [PATCH 01/11] fix exposing of .env variables in Create React App --- src/browser.js | 17 ++++++++++++++--- src/env.js | 2 ++ 2 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 src/env.js diff --git a/src/browser.js b/src/browser.js index f18d63e7..88ba3a2e 100644 --- a/src/browser.js +++ b/src/browser.js @@ -2,6 +2,7 @@ * This is the web browser implementation of `debug()`. */ + exports.log = log; exports.formatArgs = formatArgs; exports.save = save; @@ -12,6 +13,15 @@ exports.storage = 'undefined' != typeof chrome ? chrome.storage.local : localstorage(); + +/** + * Are we in Electron? + * @returns {boolean} + */ +function isElectron() { + return (typeof window !== 'undefined' && window.process && window.process.type === 'renderer'); +} + /** * Colors. */ @@ -42,7 +52,7 @@ function useColors() { // NB: In an Electron preload script, document will be defined but not fully // initialized. Since we know we're in Chrome, we'll just detect this case // explicitly - if (typeof window !== 'undefined' && window.process && window.process.type === 'renderer') { + if (isElectron()) { return true; } @@ -147,9 +157,10 @@ function load() { r = exports.storage.debug; } catch(e) {} + // If debug isn't set in LS, and we're in Electron, try to load $DEBUG - if (!r && typeof process !== 'undefined' && 'env' in process) { - r = process.env.DEBUG; + if (!r && isElectron()) { + r = require('./env').DEBUG; } return r; diff --git a/src/env.js b/src/env.js new file mode 100644 index 00000000..6be9a724 --- /dev/null +++ b/src/env.js @@ -0,0 +1,2 @@ + +module.exports = process.env || {}; \ No newline at end of file From 3d42235dac35b1fb4f3b77c00589128759bd1202 Mon Sep 17 00:00:00 2001 From: Moos Date: Tue, 17 Apr 2018 23:23:56 -0700 Subject: [PATCH 02/11] minor fixes --- src/browser.js | 2 -- src/env.js | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/browser.js b/src/browser.js index 88ba3a2e..a0e3e6d2 100644 --- a/src/browser.js +++ b/src/browser.js @@ -2,7 +2,6 @@ * This is the web browser implementation of `debug()`. */ - exports.log = log; exports.formatArgs = formatArgs; exports.save = save; @@ -13,7 +12,6 @@ exports.storage = 'undefined' != typeof chrome ? chrome.storage.local : localstorage(); - /** * Are we in Electron? * @returns {boolean} diff --git a/src/env.js b/src/env.js index 6be9a724..efee3982 100644 --- a/src/env.js +++ b/src/env.js @@ -1,2 +1,2 @@ -module.exports = process.env || {}; \ No newline at end of file +module.exports = process.env || {}; From 80ae3aecfb4245733326a4702a0637d32935ca03 Mon Sep 17 00:00:00 2001 From: Moos Date: Wed, 18 Apr 2018 01:53:09 -0700 Subject: [PATCH 03/11] take II - completely separate Electron path, add convenience npm scripts for devs --- Makefile | 4 ++++ package.json | 14 +++++++++++++- src/browser.js | 42 +++++++++++++++++++----------------------- src/electron.js | 27 +++++++++++++++++++++++++++ src/env.js | 2 -- src/index.js | 2 +- test/debug_spec.js | 13 ++++++++++--- 7 files changed, 74 insertions(+), 30 deletions(-) create mode 100644 src/electron.js delete mode 100644 src/env.js diff --git a/Makefile b/Makefile index 3ddd1360..ae90361a 100644 --- a/Makefile +++ b/Makefile @@ -36,6 +36,9 @@ test-node: @istanbul cover node_modules/mocha/bin/_mocha -- test/**.js @cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js +test-electron: + node_modules/mocha/bin/_mocha -- test/**.js + test-browser: @$(MAKE) browser @karma start --single-run @@ -43,6 +46,7 @@ test-browser: test-all: @concurrently \ "make test-node" \ + "make test-electron" \ "make test-browser" test: diff --git a/package.json b/package.json index 9af3874d..e49e2677 100644 --- a/package.json +++ b/package.json @@ -39,5 +39,17 @@ "sinon-chai": "^2.8.0" }, "main": "./src/index.js", - "browser": "./src/browser.js" + "browser": "./src/browser.js", + "scripts": { + "prebuild": "mkdir dist || echo", + "build": "./node_modules/.bin/browserify --standalone debug . > dist/debug.js", + "lint": "npx eslint *.js src/*.js", + "test": "npm run test-all", + "test-browser": "npx karma start --single-run", + "test-electron": "npm --electron=1 run test-electron-internal", + "test-electron-internal": "npx mocha test/**.js", + "test-node": "npx istanbul cover node_modules/mocha/bin/_mocha -- test/**.js", + "test-all": "npx concurrently \"npm run test-node\" \"npm run test-browser\" \"npm run test-electron\"", + "clean": "npx rimraf dist coverage" + } } diff --git a/src/browser.js b/src/browser.js index a0e3e6d2..395a8f7c 100644 --- a/src/browser.js +++ b/src/browser.js @@ -17,7 +17,7 @@ exports.storage = 'undefined' != typeof chrome * @returns {boolean} */ function isElectron() { - return (typeof window !== 'undefined' && window.process && window.process.type === 'renderer'); + return (typeof process !== 'undefined' && process.type === 'renderer'); } /** @@ -150,18 +150,9 @@ function save(namespaces) { */ function load() { - var r; try { - r = exports.storage.debug; + return exports.storage.debug; } catch(e) {} - - - // If debug isn't set in LS, and we're in Electron, try to load $DEBUG - if (!r && isElectron()) { - r = require('./env').DEBUG; - } - - return r; } /** @@ -181,18 +172,23 @@ function localstorage() { } catch (e) {} } -module.exports = require('./common')(exports); +if (isElectron()) { + module.exports = exports; + module.exports.humanize = require('ms'); +} else { + module.exports = require('./common')(exports); -var formatters = module.exports.formatters; + var formatters = module.exports.formatters; -/** - * Map %j to `JSON.stringify()`, since no Web Inspectors do that by default. - */ + /** + * Map %j to `JSON.stringify()`, since no Web Inspectors do that by default. + */ -formatters.j = function(v) { - try { - return JSON.stringify(v); - } catch (err) { - return '[UnexpectedJSONParseError]: ' + err.message; - } -}; + formatters.j = function (v) { + try { + return JSON.stringify(v); + } catch (err) { + return '[UnexpectedJSONParseError]: ' + err.message; + } + }; +} diff --git a/src/electron.js b/src/electron.js new file mode 100644 index 00000000..72f9bcf6 --- /dev/null +++ b/src/electron.js @@ -0,0 +1,27 @@ +var exports = require('./browser'); +var browserLoad = exports.load; + +// If debug isn't set in LS, and we're in Electron, try to load $DEBUG +exports.load = function load() { + var r = browserLoad(); + if (!r && 'env' in process) { + r = process.env.DEBUG; + } + return r; +}; + +module.exports = require('./common')(exports); + +var formatters = module.exports.formatters; + +/** + * Map %j to `JSON.stringify()`, since no Web Inspectors do that by default. + */ + +formatters.j = function (v) { + try { + return JSON.stringify(v); + } catch (err) { + return '[UnexpectedJSONParseError]: ' + err.message; + } +}; \ No newline at end of file diff --git a/src/env.js b/src/env.js deleted file mode 100644 index efee3982..00000000 --- a/src/env.js +++ /dev/null @@ -1,2 +0,0 @@ - -module.exports = process.env || {}; diff --git a/src/index.js b/src/index.js index cabcbcda..f4d3e68e 100644 --- a/src/index.js +++ b/src/index.js @@ -4,7 +4,7 @@ */ if (typeof process === 'undefined' || process.type === 'renderer') { - module.exports = require('./browser.js'); + module.exports = require('./electron.js'); } else { module.exports = require('./node.js'); } diff --git a/test/debug_spec.js b/test/debug_spec.js index 142fbe79..2c96547d 100644 --- a/test/debug_spec.js +++ b/test/debug_spec.js @@ -5,20 +5,27 @@ var chai , expect , debug , sinon - , sinonChai; + , sinonChai + , env = 'browser'; if (typeof module !== 'undefined') { chai = require('chai'); expect = chai.expect; + env = 'node'; + if (process.env.npm_config_electron) { + // force Electron mode + process.type = 'renderer'; + env = 'Electron'; + } debug = require('../src/index'); sinon = require('sinon'); sinonChai = require("sinon-chai"); chai.use(sinonChai); } +env = ' (' + env + ')'; - -describe('debug', function () { +describe('debug' + env, function () { var log = debug('test'); log.log = sinon.stub(); From c9c6c704697fc4e61ef2eedd3f3b3673204c2f76 Mon Sep 17 00:00:00 2001 From: Moos Date: Tue, 17 Apr 2018 23:20:24 -0700 Subject: [PATCH 04/11] fix exposing of .env variables in Create React App --- src/browser.js | 17 ++++++++++++++--- src/env.js | 2 ++ 2 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 src/env.js diff --git a/src/browser.js b/src/browser.js index ac3f7e13..f267c872 100644 --- a/src/browser.js +++ b/src/browser.js @@ -10,6 +10,16 @@ exports.load = load; exports.useColors = useColors; exports.storage = localstorage(); + +/** + * Are we in Electron? + * @returns {boolean} + */ +function isElectron() { + return typeof window !== 'undefined' && window.process && + (window.process.type === 'renderer' || window.process.__nwjs); +} + /** * Colors. */ @@ -106,7 +116,7 @@ function useColors() { // NB: In an Electron preload script, document will be defined but not fully // initialized. Since we know we're in Chrome, we'll just detect this case // explicitly - if (typeof window !== 'undefined' && window.process && (window.process.type === 'renderer' || window.process.__nwjs)) { + if (isElectron()) { return true; } @@ -212,9 +222,10 @@ function load() { // XXX (@Qix-) should we be logging these? } + // If debug isn't set in LS, and we're in Electron, try to load $DEBUG - if (!r && typeof process !== 'undefined' && 'env' in process) { - r = process.env.DEBUG; + if (!r && isElectron()) { + r = require('./env').DEBUG; } return r; diff --git a/src/env.js b/src/env.js new file mode 100644 index 00000000..efee3982 --- /dev/null +++ b/src/env.js @@ -0,0 +1,2 @@ + +module.exports = process.env || {}; From 76e69d214630873b64fb5831f4985e7216dc8263 Mon Sep 17 00:00:00 2001 From: Moos Date: Tue, 17 Apr 2018 23:23:56 -0700 Subject: [PATCH 05/11] minor fixes --- src/browser.js | 1 - src/env.js | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/browser.js b/src/browser.js index f267c872..7b9b0e04 100644 --- a/src/browser.js +++ b/src/browser.js @@ -10,7 +10,6 @@ exports.load = load; exports.useColors = useColors; exports.storage = localstorage(); - /** * Are we in Electron? * @returns {boolean} diff --git a/src/env.js b/src/env.js index efee3982..6be9a724 100644 --- a/src/env.js +++ b/src/env.js @@ -1,2 +1,2 @@ -module.exports = process.env || {}; +module.exports = process.env || {}; \ No newline at end of file From 11cc167d384bf538f5455d599a5397624454c0e4 Mon Sep 17 00:00:00 2001 From: Moos Date: Wed, 18 Apr 2018 01:53:09 -0700 Subject: [PATCH 06/11] take II - completely separate Electron path, add convenience npm scripts for devs --- package.json | 14 +++++++++++++- src/browser.js | 18 ++++++++++-------- src/electron.js | 27 +++++++++++++++++++++++++++ src/env.js | 2 -- src/index.js | 3 ++- 5 files changed, 52 insertions(+), 12 deletions(-) create mode 100644 src/electron.js delete mode 100644 src/env.js diff --git a/package.json b/package.json index 643af019..ad2c80a8 100644 --- a/package.json +++ b/package.json @@ -47,5 +47,17 @@ "xo": "^0.23.0" }, "main": "./src/index.js", - "browser": "./src/browser.js" + "browser": "./src/browser.js", + "scripts": { + "prebuild": "mkdir dist || echo", + "build": "./node_modules/.bin/browserify --standalone debug . > dist/debug.js", + "lint": "npx eslint *.js src/*.js", + "test": "npm run test-all", + "test-browser": "npx karma start --single-run", + "test-electron": "npm --electron=1 run test-electron-internal", + "test-electron-internal": "npx mocha test/**.js", + "test-node": "npx istanbul cover node_modules/mocha/bin/_mocha -- test/**.js", + "test-all": "npx concurrently \"npm run test-node\" \"npm run test-browser\" \"npm run test-electron\"", + "clean": "npx rimraf dist coverage" + } } diff --git a/src/browser.js b/src/browser.js index 7b9b0e04..0083cf3b 100644 --- a/src/browser.js +++ b/src/browser.js @@ -15,8 +15,7 @@ exports.storage = localstorage(); * @returns {boolean} */ function isElectron() { - return typeof window !== 'undefined' && window.process && - (window.process.type === 'renderer' || window.process.__nwjs); + return typeof process !== 'undefined' && (process.type === 'renderer' || process.__nwjs); } /** @@ -215,18 +214,16 @@ function save(namespaces) { function load() { let r; try { - r = exports.storage.getItem('debug'); + return exports.storage.getItem('debug'); } catch (error) { // Swallow // XXX (@Qix-) should we be logging these? } - // If debug isn't set in LS, and we're in Electron, try to load $DEBUG - if (!r && isElectron()) { - r = require('./env').DEBUG; + if (!r && isElectron()) { + r = require('./env').DEBUG; } - return r; } @@ -252,6 +249,10 @@ function localstorage() { } } +if (isElectron()) { + module.exports = exports; + module.exports.humanize = require('ms'); +} else { module.exports = require('./common')(exports); const {formatters} = module.exports; @@ -265,5 +266,6 @@ formatters.j = function (v) { return JSON.stringify(v); } catch (error) { return '[UnexpectedJSONParseError]: ' + error.message; - } + } }; +} diff --git a/src/electron.js b/src/electron.js new file mode 100644 index 00000000..72f9bcf6 --- /dev/null +++ b/src/electron.js @@ -0,0 +1,27 @@ +var exports = require('./browser'); +var browserLoad = exports.load; + +// If debug isn't set in LS, and we're in Electron, try to load $DEBUG +exports.load = function load() { + var r = browserLoad(); + if (!r && 'env' in process) { + r = process.env.DEBUG; + } + return r; +}; + +module.exports = require('./common')(exports); + +var formatters = module.exports.formatters; + +/** + * Map %j to `JSON.stringify()`, since no Web Inspectors do that by default. + */ + +formatters.j = function (v) { + try { + return JSON.stringify(v); + } catch (err) { + return '[UnexpectedJSONParseError]: ' + err.message; + } +}; \ No newline at end of file diff --git a/src/env.js b/src/env.js deleted file mode 100644 index 6be9a724..00000000 --- a/src/env.js +++ /dev/null @@ -1,2 +0,0 @@ - -module.exports = process.env || {}; \ No newline at end of file diff --git a/src/index.js b/src/index.js index bf4c57f2..26317eed 100644 --- a/src/index.js +++ b/src/index.js @@ -4,7 +4,8 @@ */ if (typeof process === 'undefined' || process.type === 'renderer' || process.browser === true || process.__nwjs) { - module.exports = require('./browser.js'); + module.exports = require('./browser.js'); + module.exports = require('./electron.js'); } else { module.exports = require('./node.js'); } From 52d06bf3c2dffcd11e27b828de22b38a6ad668cc Mon Sep 17 00:00:00 2001 From: Moos Date: Sat, 2 Feb 2019 00:07:57 -0800 Subject: [PATCH 07/11] Update after rebasing from upstream --- karma.conf.js | 2 +- package.json | 18 ++++-------------- src/browser.js | 38 +++++++++++++++++--------------------- src/electron.js | 31 ++++++++++++++++--------------- src/index.js | 10 +++++----- test.js | 14 ++++++++++++-- 6 files changed, 55 insertions(+), 58 deletions(-) diff --git a/karma.conf.js b/karma.conf.js index 6e835858..e9fc611a 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -14,7 +14,7 @@ module.exports = function (config) { // Test results reporter to use // possible values: 'dots', 'progress' // available reporters: https://npmjs.org/browse/keyword/karma-reporter - reporters: ['progress'], + reporters: ['mocha'], // Web server port port: 9876, diff --git a/package.json b/package.json index ad2c80a8..0909d69d 100644 --- a/package.json +++ b/package.json @@ -25,9 +25,10 @@ "license": "MIT", "scripts": { "lint": "xo", - "test": "npm run test:node && npm run test:browser", + "test": "npm --node run test:node && npm run test:browser && npm --electron run test:electron", "test:node": "istanbul cover _mocha -- test.js", "test:browser": "karma start --single-run", + "test:electron": "mocha test.js", "test:coverage": "cat ./coverage/lcov.info | coveralls" }, "dependencies": { @@ -42,22 +43,11 @@ "karma-browserify": "^6.0.0", "karma-chrome-launcher": "^2.2.0", "karma-mocha": "^1.3.0", + "karma-mocha-reporter": "^2.2.5", "mocha": "^5.2.0", "mocha-lcov-reporter": "^1.2.0", "xo": "^0.23.0" }, "main": "./src/index.js", - "browser": "./src/browser.js", - "scripts": { - "prebuild": "mkdir dist || echo", - "build": "./node_modules/.bin/browserify --standalone debug . > dist/debug.js", - "lint": "npx eslint *.js src/*.js", - "test": "npm run test-all", - "test-browser": "npx karma start --single-run", - "test-electron": "npm --electron=1 run test-electron-internal", - "test-electron-internal": "npx mocha test/**.js", - "test-node": "npx istanbul cover node_modules/mocha/bin/_mocha -- test/**.js", - "test-all": "npx concurrently \"npm run test-node\" \"npm run test-browser\" \"npm run test-electron\"", - "clean": "npx rimraf dist coverage" - } + "browser": "./src/browser.js" } diff --git a/src/browser.js b/src/browser.js index 0a4ecbdb..8f976be7 100644 --- a/src/browser.js +++ b/src/browser.js @@ -15,7 +15,7 @@ exports.storage = localstorage(); * @returns {boolean} */ function isElectron() { - return typeof process !== 'undefined' && (process.type === 'renderer' || process.__nwjs); + return typeof process !== 'undefined' && (process.type === 'renderer' || process.__nwjs); } /** @@ -114,7 +114,7 @@ function useColors() { // NB: In an Electron preload script, document will be defined but not fully // initialized. Since we know we're in Chrome, we'll just detect this case // explicitly - if (isElectron()) { + if (isElectron()) { return true; } @@ -212,18 +212,14 @@ function save(namespaces) { * @api private */ function load() { - let r; try { return exports.storage.getItem('debug'); } catch (error) { // Swallow // XXX (@Qix-) should we be logging these? } +} - // If debug isn't set in LS, and we're in Electron, try to load $DEBUG - if (!r && isElectron()) { - r = require('./env').DEBUG; - } /** * Localstorage attempts to return the localstorage. * @@ -247,22 +243,22 @@ function localstorage() { } if (isElectron()) { - module.exports = exports; - module.exports.humanize = require('ms'); + module.exports = exports; + module.exports.humanize = require('ms'); } else { -module.exports = require('./common')(exports); + module.exports = require('./common')(exports); -const {formatters} = module.exports; + const {formatters} = module.exports; -/** - * Map %j to `JSON.stringify()`, since no Web Inspectors do that by default. - */ + /** + * Map %j to `JSON.stringify()`, since no Web Inspectors do that by default. + */ -formatters.j = function (v) { - try { - return JSON.stringify(v); - } catch (error) { - return '[UnexpectedJSONParseError]: ' + error.message; - } -}; + formatters.j = function (v) { + try { + return JSON.stringify(v); + } catch (error) { + return '[UnexpectedJSONParseError]: ' + error.message; + } + }; } diff --git a/src/electron.js b/src/electron.js index 72f9bcf6..988c6455 100644 --- a/src/electron.js +++ b/src/electron.js @@ -1,27 +1,28 @@ -var exports = require('./browser'); -var browserLoad = exports.load; +const exports = require('./browser'); + +const browserLoad = exports.load; // If debug isn't set in LS, and we're in Electron, try to load $DEBUG -exports.load = function load() { - var r = browserLoad(); - if (!r && 'env' in process) { - r = process.env.DEBUG; - } - return r; +exports.load = function () { + let r = browserLoad(); + if (!r && 'env' in process) { + r = process.env.DEBUG; + } + return r; }; module.exports = require('./common')(exports); -var formatters = module.exports.formatters; +const {formatters} = module.exports; /** * Map %j to `JSON.stringify()`, since no Web Inspectors do that by default. */ formatters.j = function (v) { - try { - return JSON.stringify(v); - } catch (err) { - return '[UnexpectedJSONParseError]: ' + err.message; - } -}; \ No newline at end of file + try { + return JSON.stringify(v); + } catch (error) { + return '[UnexpectedJSONParseError]: ' + error.message; + } +}; diff --git a/src/index.js b/src/index.js index c700e771..e8c73b13 100644 --- a/src/index.js +++ b/src/index.js @@ -4,11 +4,11 @@ */ if (typeof process === 'undefined' || process.type === 'renderer' || process.browser === true || process.__nwjs) { - if (process.browser === true) { - module.exports = require('./browser.js'); - } else { - module.exports = require('./electron.js'); - } + if (process.browser === true) { + module.exports = require('./browser.js'); + } else { + module.exports = require('./electron.js'); + } } else { module.exports = require('./node.js'); } diff --git a/test.js b/test.js index f61e079b..e6511cb7 100644 --- a/test.js +++ b/test.js @@ -2,8 +2,18 @@ const assert = require('assert'); const debug = require('./src'); - -describe('debug', () => { +let env = 'browser'; + +if (process.env.npm_config_electron) { + // force Electron mode + process.type = 'renderer'; + env = 'Electron'; +} else if (process.env.npm_config_node) { + env = 'node'; +} +env = ' (' + env + ')'; + +describe('debug' + env, () => { it('passes a basic sanity check', () => { const log = debug('test'); log.enabled = true; From 0fa1a286a2c971a026c257701c00dc1d5efee957 Mon Sep 17 00:00:00 2001 From: Moos Date: Sat, 2 Feb 2019 22:23:46 -0800 Subject: [PATCH 08/11] fix lint, add more test cases and coverage --- package.json | 7 ++++--- src/electron.js | 2 +- test.js | 33 ++++++++++++++++++++++++++++----- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 0909d69d..d9ef7280 100644 --- a/package.json +++ b/package.json @@ -25,10 +25,11 @@ "license": "MIT", "scripts": { "lint": "xo", - "test": "npm --node run test:node && npm run test:browser && npm --electron run test:electron", - "test:node": "istanbul cover _mocha -- test.js", + "test": "npm --node run test:node && npm --electron run test:electron && npm run test:browser", "test:browser": "karma start --single-run", - "test:electron": "mocha test.js", + "test:node": "istanbul cover --dir coverage/node -x test.js node_modules/mocha/bin/_mocha", + "test:electron": "istanbul cover --dir coverage/electron -x test.js node_modules/mocha/bin/_mocha", + "posttest": "istanbul report --include coverage/**/coverage.json", "test:coverage": "cat ./coverage/lcov.info | coveralls" }, "dependencies": { diff --git a/src/electron.js b/src/electron.js index 988c6455..1360207c 100644 --- a/src/electron.js +++ b/src/electron.js @@ -1,4 +1,4 @@ -const exports = require('./browser'); +exports = require('./browser'); const browserLoad = exports.load; diff --git a/test.js b/test.js index e6511cb7..18eb0fb0 100644 --- a/test.js +++ b/test.js @@ -1,17 +1,22 @@ /* eslint-env mocha */ -const assert = require('assert'); -const debug = require('./src'); +/** pre-load conditions */ let env = 'browser'; - if (process.env.npm_config_electron) { - // force Electron mode + // Force Electron mode process.type = 'renderer'; env = 'Electron'; } else if (process.env.npm_config_node) { env = 'node'; } env = ' (' + env + ')'; +process.env.DEBUG_YES = 'yes'; +process.env.DEBUG_NO = 'no'; +process.env.DEBUG_NULL = 'null'; +process.env.DEBUG_NUM = 111; + +const assert = require('assert'); +const debug = require('./src'); describe('debug' + env, () => { it('passes a basic sanity check', () => { @@ -40,6 +45,7 @@ describe('debug' + env, () => { }); it('uses custom log function', () => { + debug.useColors = () => false; const log = debug('test'); log.enabled = true; @@ -49,8 +55,13 @@ describe('debug' + env, () => { log('using custom log function'); log('using custom log function again'); log('%O', 12345); + log('%o', 12345); + log('%j', {abc: 12345}); + assert.deepStrictEqual(messages.length, 5); - assert.deepStrictEqual(messages.length, 3); + const numInstance = debug.instances.length; + assert(log.destroy()); + assert.strictEqual(debug.instances.length, numInstance - 1); }); describe('extend namespace', () => { @@ -128,4 +139,16 @@ describe('debug' + env, () => { assert.deepStrictEqual(oldSkips.map(String), debug.skips.map(String)); }); }); + + if (env === 'node') { + describe('inspectOpts', () => { + it('handles env vars', () => { + const d = debug.inspectOpts; + assert.strictEqual(d.no, false); + assert.strictEqual(d.null, null); + assert.strictEqual(d.num, 111); + assert.strictEqual(d.yes, true); + }); + }); + } }); From 4065feb6785448692aa0361cdb7e3de9f74c1c66 Mon Sep 17 00:00:00 2001 From: Moos Date: Sat, 2 Feb 2019 22:39:26 -0800 Subject: [PATCH 09/11] hmmm... - fix travis-only failure! --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d9ef7280..10af753d 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ "test:browser": "karma start --single-run", "test:node": "istanbul cover --dir coverage/node -x test.js node_modules/mocha/bin/_mocha", "test:electron": "istanbul cover --dir coverage/electron -x test.js node_modules/mocha/bin/_mocha", - "posttest": "istanbul report --include coverage/**/coverage.json", + "posttest": "istanbul report --include coverage/**/coverage.json lcov", "test:coverage": "cat ./coverage/lcov.info | coveralls" }, "dependencies": { From 9a8a9cd33cf89a98c8e59ea56fe881e9735debc0 Mon Sep 17 00:00:00 2001 From: Moos Date: Sat, 2 Feb 2019 23:39:16 -0800 Subject: [PATCH 10/11] unable to get istanbul to work on travis combining two runs -- upgrade to nyc! --- .gitignore | 1 + package.json | 9 +++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 15be2208..6ff0eb6f 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ npm-debug.log yarn-error.log /dist/ /coverage/ +.nyc_output/ # lockfiles yarn.lock diff --git a/package.json b/package.json index 10af753d..360ac955 100644 --- a/package.json +++ b/package.json @@ -25,11 +25,12 @@ "license": "MIT", "scripts": { "lint": "xo", + "pretest": "rm -rf .nyc_output", "test": "npm --node run test:node && npm --electron run test:electron && npm run test:browser", "test:browser": "karma start --single-run", - "test:node": "istanbul cover --dir coverage/node -x test.js node_modules/mocha/bin/_mocha", - "test:electron": "istanbul cover --dir coverage/electron -x test.js node_modules/mocha/bin/_mocha", - "posttest": "istanbul report --include coverage/**/coverage.json lcov", + "test:node": "nyc --no-clean mocha --exit", + "test:electron": "nyc --no-clean mocha --exit", + "posttest": "nyc report --reporter lcov", "test:coverage": "cat ./coverage/lcov.info | coveralls" }, "dependencies": { @@ -39,7 +40,6 @@ "brfs": "^2.0.1", "browserify": "^16.2.3", "coveralls": "^3.0.2", - "istanbul": "^0.4.5", "karma": "^3.1.4", "karma-browserify": "^6.0.0", "karma-chrome-launcher": "^2.2.0", @@ -47,6 +47,7 @@ "karma-mocha-reporter": "^2.2.5", "mocha": "^5.2.0", "mocha-lcov-reporter": "^1.2.0", + "nyc": "^13.1.0", "xo": "^0.23.0" }, "main": "./src/index.js", From 86c9684b10dd508ac544ed70bf22350efdfa39c2 Mon Sep 17 00:00:00 2001 From: Moos Date: Sat, 2 Feb 2019 23:51:40 -0800 Subject: [PATCH 11/11] fix for old npm version --- package.json | 2 +- test.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 360ac955..b885f3a2 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "scripts": { "lint": "xo", "pretest": "rm -rf .nyc_output", - "test": "npm --node run test:node && npm --electron run test:electron && npm run test:browser", + "test": "npm --env=node run test:node && npm --env=electron run test:electron && npm run test:browser", "test:browser": "karma start --single-run", "test:node": "nyc --no-clean mocha --exit", "test:electron": "nyc --no-clean mocha --exit", diff --git a/test.js b/test.js index 18eb0fb0..2e1d71e4 100644 --- a/test.js +++ b/test.js @@ -2,11 +2,11 @@ /** pre-load conditions */ let env = 'browser'; -if (process.env.npm_config_electron) { +if (process.env.npm_config_env === 'electron') { // Force Electron mode process.type = 'renderer'; env = 'Electron'; -} else if (process.env.npm_config_node) { +} else if (process.env.npm_config_env === 'node') { env = 'node'; } env = ' (' + env + ')';