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

Do not silence cli commands #6909

Merged
merged 3 commits into from
Apr 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions cli/__snapshots__/cache_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 │
└─────────┴───────────┘
`
10 changes: 10 additions & 0 deletions cli/__snapshots__/cli_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
`
4 changes: 2 additions & 2 deletions cli/lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions cli/lib/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -46,6 +51,7 @@ module.exports = {
log,
warn,
error,
always,
logLines,
print,
reset,
Expand Down
13 changes: 8 additions & 5 deletions cli/lib/tasks/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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) => {
Expand All @@ -40,7 +43,7 @@ const list = () => {
return table.push([versionString, lastUsed])
})

logger.log(table.toString())
logger.always(table.toString())
})
}

Expand Down Expand Up @@ -84,7 +87,7 @@ const getCachedVersions = () => {
}

module.exports = {
path,
path: logCachePath,
clear,
list,
getCachedVersions,
Expand Down
42 changes: 42 additions & 0 deletions cli/test/lib/cli_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -131,6 +132,15 @@ describe('cli', () => {
})

context('cypress version', () => {
let restoreEnv

afterEach(() => {
if (restoreEnv) {
restoreEnv()
restoreEnv = null
}
})

const binaryDir = '/binary/dir'

beforeEach(() => {
Expand Down Expand Up @@ -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)
Expand Down
74 changes: 72 additions & 2 deletions cli/test/lib/tasks/cache_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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) => {
Expand All @@ -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', () => {
Expand All @@ -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')
Expand All @@ -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')

Expand Down