diff --git a/cli/__snapshots__/cache_spec.js b/cli/__snapshots__/cache_spec.js index f4809c8128db..f900b9e5d564 100644 --- a/cli/__snapshots__/cache_spec.js +++ b/cli/__snapshots__/cache_spec.js @@ -35,3 +35,23 @@ exports['lib/tasks/cache .list some versions have never been opened 1'] = ` │ 2.3.4 │ unknown │ └─────────┴──────────────┘ ` + +exports['cache list with silent log level'] = ` +┌─────────┬───────────┐ +│ version │ last used │ +├─────────┼───────────┤ +│ 1.2.3 │ unknown │ +├─────────┼───────────┤ +│ 2.3.4 │ unknown │ +└─────────┴───────────┘ +` + +exports['cache list with warn log level'] = ` +┌─────────┬───────────┐ +│ version │ last used │ +├─────────┼───────────┤ +│ 1.2.3 │ unknown │ +├─────────┼───────────┤ +│ 2.3.4 │ unknown │ +└─────────┴───────────┘ +` diff --git a/cli/__snapshots__/cli_spec.js b/cli/__snapshots__/cli_spec.js index b7f3c328ae61..2ff6b0b1e7ab 100644 --- a/cli/__snapshots__/cli_spec.js +++ b/cli/__snapshots__/cli_spec.js @@ -428,3 +428,13 @@ exports['cli CYPRESS_INTERNAL_ENV allows and warns when staging environment 1'] ------- ` + +exports['cli version and binary version with npm log silent'] = ` +Cypress package version: 1.2.3 +Cypress binary version: X.Y.Z +` + +exports['cli version and binary version with npm log warn'] = ` +Cypress package version: 1.2.3 +Cypress binary version: X.Y.Z +` diff --git a/cli/lib/cli.js b/cli/lib/cli.js index d90a7d45c768..2875750b712c 100644 --- a/cli/lib/cli.js +++ b/cli/lib/cli.js @@ -154,8 +154,8 @@ function showVersions () { return require('./exec/versions') .getVersions() .then((versions = {}) => { - logger.log('Cypress package version:', versions.package) - logger.log('Cypress binary version:', versions.binary) + logger.always('Cypress package version:', versions.package) + logger.always('Cypress binary version:', versions.binary) process.exit(0) }) .catch(util.logErrorExit1) diff --git a/cli/lib/logger.js b/cli/lib/logger.js index 0ab54f311123..679aeb52f517 100644 --- a/cli/lib/logger.js +++ b/cli/lib/logger.js @@ -26,6 +26,11 @@ const log = (...messages) => { console.log(...messages) // eslint-disable-line no-console } +const always = (...messages) => { + logs.push(messages.join(' ')) + console.log(...messages) // eslint-disable-line no-console +} + // splits long text into lines and calls log() // on each one to allow easy unit testing for specific message const logLines = (text) => { @@ -46,6 +51,7 @@ module.exports = { log, warn, error, + always, logLines, print, reset, diff --git a/cli/lib/tasks/cache.js b/cli/lib/tasks/cache.js index 4cd9371ec4d9..54c7fac97199 100644 --- a/cli/lib/tasks/cache.js +++ b/cli/lib/tasks/cache.js @@ -15,9 +15,8 @@ const colors = { values: chalk.green, } -// TODO: rename this function -const path = () => { - logger.log(state.getCacheDir()) +const logCachePath = () => { + logger.always(state.getCacheDir()) return undefined } @@ -26,6 +25,10 @@ const clear = () => { return fs.removeAsync(state.getCacheDir()) } +/** + * Collects all cached versions, finds when each was used + * and prints a table with results to the terminal + */ const list = () => { return getCachedVersions() .then((binaries) => { @@ -40,7 +43,7 @@ const list = () => { return table.push([versionString, lastUsed]) }) - logger.log(table.toString()) + logger.always(table.toString()) }) } @@ -84,7 +87,7 @@ const getCachedVersions = () => { } module.exports = { - path, + path: logCachePath, clear, list, getCachedVersions, diff --git a/cli/test/lib/cli_spec.js b/cli/test/lib/cli_spec.js index 76f9848d4280..0460aba5ea2b 100644 --- a/cli/test/lib/cli_spec.js +++ b/cli/test/lib/cli_spec.js @@ -13,6 +13,7 @@ const install = require(`${lib}/tasks/install`) const snapshot = require('../support/snapshot') const debug = require('debug')('test') const execa = require('execa-wrap') +const mockedEnv = require('mocked-env') describe('cli', () => { require('mocha-banner').register() @@ -131,6 +132,15 @@ describe('cli', () => { }) context('cypress version', () => { + let restoreEnv + + afterEach(() => { + if (restoreEnv) { + restoreEnv() + restoreEnv = null + } + }) + const binaryDir = '/binary/dir' beforeEach(() => { @@ -162,6 +172,38 @@ describe('cli', () => { }) }) + it('reports package and binary message with npm log silent', (done) => { + restoreEnv = mockedEnv({ + npm_config_loglevel: 'silent', + }) + + sinon.stub(util, 'pkgVersion').returns('1.2.3') + sinon.stub(state, 'getBinaryPkgVersionAsync').resolves('X.Y.Z') + + this.exec('version') + process.exit.callsFake(() => { + // should not be empty! + snapshot('cli version and binary version with npm log silent', logger.print()) + done() + }) + }) + + it('reports package and binary message with npm log warn', (done) => { + restoreEnv = mockedEnv({ + npm_config_loglevel: 'warn', + }) + + sinon.stub(util, 'pkgVersion').returns('1.2.3') + sinon.stub(state, 'getBinaryPkgVersionAsync').resolves('X.Y.Z') + + this.exec('version') + process.exit.callsFake(() => { + // should not be empty! + snapshot('cli version and binary version with npm log warn', logger.print()) + done() + }) + }) + it('handles non-existent binary version', (done) => { sinon.stub(util, 'pkgVersion').returns('1.2.3') sinon.stub(state, 'getBinaryPkgVersionAsync').resolves(null) diff --git a/cli/test/lib/tasks/cache_spec.js b/cli/test/lib/tasks/cache_spec.js index bfb08406b026..5cfa9f11a875 100644 --- a/cli/test/lib/tasks/cache_spec.js +++ b/cli/test/lib/tasks/cache_spec.js @@ -11,6 +11,7 @@ const moment = require('moment') const stripAnsi = require('strip-ansi') const path = require('path') const termToHtml = require('term-to-html') +const mockedEnv = require('mocked-env') const outputHtmlFolder = path.join(__dirname, '..', '..', 'html') @@ -54,10 +55,15 @@ describe('lib/tasks/cache', () => { mockfs.restore() }) - const defaultSnapshot = () => { + const defaultSnapshot = (snapshotName) => { const stdoutAsString = getSnapshotText() + const withoutAnsi = stripAnsi(stdoutAsString) - snapshot(stripAnsi(stdoutAsString)) + if (snapshotName) { + snapshot(snapshotName, withoutAnsi) + } else { + snapshot(withoutAnsi) + } } const snapshotWithHtml = async (htmlFilename) => { @@ -72,11 +78,38 @@ describe('lib/tasks/cache', () => { } describe('.path', () => { + let restoreEnv + + afterEach(() => { + if (restoreEnv) { + restoreEnv() + restoreEnv = null + } + }) + it('lists path to cache', () => { cache.path() expect(this.stdout.toString()).to.eql('/.cache/Cypress\n') defaultSnapshot() }) + + it('lists path to cache with silent npm loglevel', () => { + restoreEnv = mockedEnv({ + npm_config_loglevel: 'silent', + }) + + cache.path() + expect(this.stdout.toString()).to.eql('/.cache/Cypress\n') + }) + + it('lists path to cache with silent npm warn', () => { + restoreEnv = mockedEnv({ + npm_config_loglevel: 'warn', + }) + + cache.path() + expect(this.stdout.toString()).to.eql('/.cache/Cypress\n') + }) }) describe('.clear', () => { @@ -93,6 +126,15 @@ describe('lib/tasks/cache', () => { }) describe('.list', () => { + let restoreEnv + + afterEach(() => { + if (restoreEnv) { + restoreEnv() + restoreEnv = null + } + }) + it('lists all versions of cached binary', async function () { // unknown access times sinon.stub(state, 'getPathToExecutable').returns('/.cache/Cypress/1.2.3/app/cypress') @@ -102,6 +144,34 @@ describe('lib/tasks/cache', () => { defaultSnapshot() }) + it('lists all versions of cached binary with npm log level silent', async function () { + restoreEnv = mockedEnv({ + npm_config_loglevel: 'silent', + }) + + // unknown access times + sinon.stub(state, 'getPathToExecutable').returns('/.cache/Cypress/1.2.3/app/cypress') + + await cache.list() + + // log output snapshot should have a grid of versions + defaultSnapshot('cache list with silent log level') + }) + + it('lists all versions of cached binary with npm log level warn', async function () { + restoreEnv = mockedEnv({ + npm_config_loglevel: 'warn', + }) + + // unknown access times + sinon.stub(state, 'getPathToExecutable').returns('/.cache/Cypress/1.2.3/app/cypress') + + await cache.list() + + // log output snapshot should have a grid of versions + defaultSnapshot('cache list with warn log level') + }) + it('lists all versions of cached binary with last access', async function () { sinon.stub(state, 'getPathToExecutable').returns('/.cache/Cypress/1.2.3/app/cypress')