Skip to content

Commit

Permalink
cli: add global cache for binaries
Browse files Browse the repository at this point in the history
This closes cypress-io#1188
  • Loading branch information
maxime1992 committed Feb 4, 2018
1 parent 84f7353 commit 556565c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
33 changes: 26 additions & 7 deletions cli/lib/tasks/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const url = require('url')
const debug = require('debug')('cypress:cli')
const { stripIndent } = require('common-tags')
const is = require('check-more-types')
const home = require('os').homedir()

const { throwFormErrorText, errors } = require('../errors')
const fs = require('../fs')
Expand Down Expand Up @@ -120,15 +121,21 @@ const downloadFromUrl = (options) => {
// send up our percent and seconds remaining
options.onProgress(state.percent, util.secsRemaining(eta))
})
// save this download here
// save this download here...
.pipe(fs.createWriteStream(options.downloadDestination))
.on('finish', () => {
debug('downloading finished')

resolve({
filename: options.downloadDestination,
downloaded: true,
})
// ... and also add it to the global cache
fs.copy(
options.downloadDestination,
`${home}/.cypress/${options.version}`
).then(() =>
resolve({
filename: options.downloadDestination,
downloaded: true,
})
)
})
})
}
Expand All @@ -155,15 +162,27 @@ const download = (options = {}) => {
}

const possibleFile = formAbsolutePath(options.version)
const possibleFileFromGlobalCache = `${home}/.cypress/${options.version}`
debug('checking local file', possibleFile, 'cwd', process.cwd())
return fs.pathExists(possibleFile).then((exists) => {
if (exists) {
debug('checking local global file', possibleFileFromGlobalCache, 'cwd', process.cwd())
return Promise.all([
fs.pathExists(possibleFile),
fs.pathExists(possibleFileFromGlobalCache),
]).then(([localFileExists, globalFileExists]) => {
if (localFileExists) {
debug('found local file', possibleFile)
debug('skipping download')
return {
filename: possibleFile,
downloaded: false,
}
} else if (globalFileExists) {
debug('found local file in global cache', possibleFileFromGlobalCache)
debug('skipping download')
return {
filename: possibleFileFromGlobalCache,
downloaded: false,
}
} else {
return downloadFromUrl(options)
}
Expand Down
25 changes: 25 additions & 0 deletions cli/test/lib/tasks/download_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const nock = require('nock')
const snapshot = require('snap-shot-it')
const la = require('lazy-ass')
const is = require('check-more-types')
const home = require('os').homedir()
const Promise = require('bluebird')

const fs = require(`${lib}/fs`)
const logger = require(`${lib}/logger`)
Expand Down Expand Up @@ -104,6 +106,29 @@ describe('download', function () {
})
})

it('searches from global cache if there is no local binary', function () {
const version = '0.13.0'
const pathFileGlobalCache = `${home}/.cypress/${version}`

this.options = Object.assign({}, this.options, { version })

// simulate that no local file was found but the global one was
this.sandbox.stub(fs, 'pathExists').callsFake((path) => {
if (path === pathFileGlobalCache) {
return Promise.resolve(true)
}

return Promise.resolve(false)
})

return download.start(this.options).then((result) => {
expect(result).to.deep.eq({
filename: `${home}/.cypress/${version}`,
downloaded: false,
})
})
})

it('catches download status errors and exits', function () {
const ctx = this

Expand Down

0 comments on commit 556565c

Please sign in to comment.