Skip to content

Commit

Permalink
Improve output tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ai committed Mar 19, 2020
1 parent ef5292e commit a079b5c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 40 deletions.
7 changes: 5 additions & 2 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
var fs = require('fs')

var browserslist = require('./')
var updateDB = require('./update-db')
var updateDb = require('./update-db')
var pkg = require('./package.json')

var args = process.argv.slice(2)

var USAGE = 'Usage:\n' +
Expand Down Expand Up @@ -35,7 +36,9 @@ if (isArg('--help') || isArg('-h')) {
} else if (isArg('--version') || isArg('-v')) {
process.stdout.write(pkg.name + ' ' + pkg.version + '\n')
} else if (isArg('--update-db')) {
updateDB()
updateDb(function (str) {
process.stdout.write(str)
})
} else {
var mode = 'browsers'
var opts = { }
Expand Down
70 changes: 37 additions & 33 deletions test/update-db.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ let { tmpdir } = require('os')
let { join } = require('path')
let fs = require('fs')

let updateDB = require('../update-db')
let updateDd = require('../update-db')

let readFile = promisify(fs.readFile)
let fsify = createFsify({
Expand Down Expand Up @@ -43,8 +43,12 @@ async function createProject (name, lockfile) {
return fakedir
}

function isInstalled (tool) {
return execSync(`whereis ${ tool }`).toString().trim() !== 'pnpm:'
function runUpdate () {
let out = ''
updateDd(str => {
out += str
})
return out
}

let caniuse = JSON.parse(execSync('npm show caniuse-lite --json').toString())
Expand All @@ -65,7 +69,7 @@ it('throws on missing package.json', async () => {
let directory = tree[0].name
process.chdir(directory)

expect(() => updateDB()).toThrow(
expect(runUpdate).toThrow(
'Cannot find package.json. ' +
'Is it a right project to run npx browserslist --update-db?'
)
Expand All @@ -89,32 +93,20 @@ it('throws on missing lockfile', async () => {
let directory = tree[0].name
process.chdir(directory)

expect(() => updateDB()).toThrow(
expect(runUpdate).toThrow(
'No lockfile found. Run "npm install", "yarn install" or "pnpm install"'
)
})

it('prints the latest version', async () => {
jest.spyOn(console, 'log').mockImplementation(() => true)
await createProject('update-npm', 'package-lock.json')

updateDB()

expect(console.log).toHaveBeenNthCalledWith(
1, 'Current version: 1.0.30001030'
)
expect(console.log).toHaveBeenNthCalledWith(
2, `New version: ${ caniuse.version }\nUpdating caniuse-lite…`
)
expect(console.log).toHaveBeenNthCalledWith(
3, 'caniuse-lite has been successfully updated'
)
})

it('updates caniuse-lite if the user uses npm', async () => {
let dir = await createProject('update-npm', 'package-lock.json')

updateDB()
expect(runUpdate()).toEqual(
'Current version: 1.0.30001030\n' +
`New version: ${ caniuse.version }\n` +
'Updating caniuse-lite…\n' +
'caniuse-lite has been successfully updated'
)

let lock = JSON.parse(await readFile(join(dir, 'package-lock.json')))
expect(lock.dependencies['caniuse-lite'].version).toEqual(caniuse.version)
Expand All @@ -123,7 +115,11 @@ it('updates caniuse-lite if the user uses npm', async () => {
it('missing caniuse-lite if the user uses npm', async () => {
let dir = await createProject('update-npm-missing', 'package-lock.json')

updateDB()
expect(runUpdate()).toEqual(
`New version: ${ caniuse.version }\n` +
'Updating caniuse-lite…\n' +
'caniuse-lite has been successfully updated'
)

let lock = JSON.parse(await readFile(join(dir, 'package-lock.json')))
expect(lock.dependencies['caniuse-lite']).toBeUndefined()
Expand All @@ -132,7 +128,12 @@ it('missing caniuse-lite if the user uses npm', async () => {
it('updates caniuse-lite if the user uses yarn', async () => {
let dir = await createProject('update-yarn', 'yarn.lock')

updateDB()
expect(runUpdate()).toEqual(
'Current version: 1.0.30001035\n' +
`New version: ${ caniuse.version }\n` +
'Updating caniuse-lite…\n' +
'caniuse-lite has been successfully updated'
)

let lock = (await readFile(join(dir, 'yarn.lock'))).toString()
expect(lock).toContain(
Expand All @@ -141,13 +142,16 @@ it('updates caniuse-lite if the user uses yarn', async () => {
)
})

if (isInstalled('pnpm')) {
it('updates caniuse-lite if the user uses pnpm', async () => {
let dir = await createProject('update-pnpm', 'pnpm-lock.yaml')
it('updates caniuse-lite if the user uses pnpm', async () => {
let dir = await createProject('update-pnpm', 'pnpm-lock.yaml')

updateDB()
expect(runUpdate()).toEqual(
'Current version: 1.0.30001035\n' +
`New version: ${ caniuse.version }\n` +
'Updating caniuse-lite…\n' +
'caniuse-lite has been successfully updated'
)

let lock = (await readFile(join(dir, 'pnpm-lock.yaml'))).toString()
expect(lock).toContain(`/caniuse-lite/${ caniuse.version }:`)
})
}
let lock = (await readFile(join(dir, 'pnpm-lock.yaml'))).toString()
expect(lock).toContain(`/caniuse-lite/${ caniuse.version }:`)
})
10 changes: 5 additions & 5 deletions update-db.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,23 +109,23 @@ function deletePackage (node) {
return node
}

module.exports = function updateDB () {
module.exports = function updateDB (print) {
var lock = detectLockfile()
lock.content = fs.readFileSync(lock.file).toString()

var current = getCurrentVersion(lock)
var latest = getLastestInfo()

if (typeof current === 'string') {
console.log('Current version: ' + current)
print('Current version: ' + current + '\n')
}
console.log(
print(
'New version: ' + latest.version + '\n' +
'Updating caniuse-lite…'
'Updating caniuse-lite…\n'
)

fs.writeFileSync(lock.file, updateLockfile(lock, latest))
childProcess.execSync(lock.mode + ' install')

console.log('caniuse-lite has been successfully updated')
print('caniuse-lite has been successfully updated')
}

0 comments on commit a079b5c

Please sign in to comment.