Skip to content

Commit

Permalink
Issue 1124 (#1310)
Browse files Browse the repository at this point in the history
* server: fixes #1124, clear the disk cache whenever launching the browser

* server: bump express to 4.16.2

* server: fixes failing tests
  • Loading branch information
brian-mann authored Feb 14, 2018
1 parent 3c0b08b commit 4db34c9
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 12 deletions.
12 changes: 8 additions & 4 deletions packages/server/lib/browsers/chrome.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -129,19 +129,23 @@ module.exports = {
args = newArgs
.then =>
Promise.all([
## ensure that we have a chrome profile dir
utils.ensureProfile(browserName)
## ensure that we have a clean cache dir
## before launching the browser every time
utils.ensureCleanCache(browserName)

@_writeExtension(options.proxyUrl, options.socketIoRoute)
])
.spread (dir, dest) ->
.spread (cacheDir, dest) ->
## normalize the --load-extensions argument by
## massaging what the user passed into our own
args = _normalizeArgExtensions(dest, args)

userDir = utils.getProfileDir(browserName)

## this overrides any previous user-data-dir args
## by being the last one
args.push("--user-data-dir=#{dir}")
args.push("--user-data-dir=#{userDir}")
args.push("--disk-cache-dir=#{cacheDir}")

debug("launch in chrome: %s, %s", url, args)

Expand Down
14 changes: 12 additions & 2 deletions packages/server/lib/browsers/electron.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,22 @@ module.exports = {
if ua = options.userAgent
@_setUserAgent(win.webContents, ua)

if ps = options.proxyServer
@_setProxy(win.webContents, ps)
setProxy = =>
if ps = options.proxyServer
@_setProxy(win.webContents, ps)

Promise.join(
setProxy(),
@_clearCache(win.webContents)
)
.then ->
win.loadURL(url)
.return(win)

_clearCache: (webContents) ->
new Promise (resolve) ->
webContents.session.clearCache(resolve)

_setUserAgent: (webContents, userAgent) ->
## set both because why not
webContents.setUserAgent(userAgent)
Expand Down
13 changes: 10 additions & 3 deletions packages/server/lib/browsers/utils.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@ fs = Promise.promisifyAll(fs)
profiles = appData.path("browsers")

module.exports = {
ensureProfile: (name) ->
p = path.join(profiles, name)
getProfileDir: (name) ->
path.join(profiles, name)

fs.ensureDirAsync(p).return(p)
ensureCleanCache: (name) ->
p = path.join(profiles, name, "CypressCache")

fs
.removeAsync(p)
.then ->
fs.ensureDirAsync(p)
.return(p)

copyExtension: (src, dest) ->
fs.copyAsync(src, dest)
Expand Down
2 changes: 1 addition & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
"errorhandler": "1.1.1",
"evil-dns": "^0.2.0",
"execa": "^0.8.0",
"express": "4.12.3",
"express": "4.16.2",
"fluent-ffmpeg": "^2.1.0",
"fs-extra": "4.0.1",
"getos": "^2.8.2",
Expand Down
27 changes: 27 additions & 0 deletions packages/server/test/e2e/cache_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ replacerRe = /(<h1>)\w+(<\/h1>)/

e2ePath = Fixtures.projectPath("e2e")

requestsForCache = 0

onServer = (app) ->
app.post "/write/:text", (req, res) ->
file = path.join(e2ePath, "index.html")
Expand All @@ -19,6 +21,13 @@ onServer = (app) ->
fs.writeFile file, str, (err) ->
res.sendStatus(200)

app.get "/cached", (req, res) ->
requestsForCache += 1

res
.set("cache-control", "public, max-age=3600")
.send("this response will be disk cached")

describe "e2e cache", ->
e2e.setup({
servers: {
Expand All @@ -37,3 +46,21 @@ describe "e2e cache", ->
snapshot: true
expectedExitCode: 0
})

it "clears cache when browser is spawned", ->
e2e.exec(@, {
spec: "cache_clearing_spec.coffee"
expectedExitCode: 0
})
.then =>
## only 1 request should have gone out
expect(requestsForCache).to.eq(1)

e2e.exec(@, {
spec: "cache_clearing_spec.coffee"
expectedExitCode: 0
})
.then ->
## and after the cache is cleaned before
## opening the browser, it'll make a new request
expect(requestsForCache).to.eq(2)
7 changes: 6 additions & 1 deletion packages/server/test/integration/cypress_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,11 @@ describe "lib/cypress", ->
ee.emit("closed")
ee.isDestroyed = -> false
ee.loadURL = ->
ee.webContents = {
session: {
clearCache: @sandbox.stub().yieldsAsync()
}
}

@sandbox.stub(utils, "launch").resolves(ee)
@sandbox.stub(Windows, "create").returns(ee)
Expand All @@ -705,7 +710,7 @@ describe "lib/cypress", ->

browserArgs = args[2]

expect(browserArgs).to.have.length(6)
expect(browserArgs).to.have.length(7)

expect(browserArgs.slice(0, 4)).to.deep.eq([
"chrome", "foo", "bar", "baz"
Expand Down
4 changes: 4 additions & 0 deletions packages/server/test/scripts/e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ const glob = Promise.promisify(require('glob'))

const options = minimist(process.argv.slice(2))

if (options.browser) {
process.env.BROWSER = options.browser
}

const started = new Date()

let numFailed = 0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
req = (win) ->
new Promise (resolve, reject) ->
rand = Math.random()

xhr = new win.XMLHttpRequest()
xhr.open("GET", "http://localhost:1515/cached/")
xhr.onload = -> resolve(win)
xhr.onerror = reject
xhr.send()

it "makes cached request", ->
cy
.visit("http://localhost:1515")
.then(req) ## this creates the disk cache
.then(req) ## this should not hit our server
5 changes: 4 additions & 1 deletion packages/server/test/unit/browsers/chrome_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ describe "lib/browsers/chrome", ->
@sandbox.stub(plugins, "has")
@sandbox.stub(plugins, "execute")
@sandbox.stub(utils, "launch")
@sandbox.stub(utils, "ensureProfile").resolves("/profile/dir")
@sandbox.stub(utils, "getProfileDir").returns("/profile/dir")
@sandbox.stub(utils, "ensureCleanCache").resolves("/profile/dir/CypressCache")

it "is noop without before:browser:launch", ->
plugins.has.returns(false)
Expand Down Expand Up @@ -51,6 +52,7 @@ describe "lib/browsers/chrome", ->
"--foo=bar"
"--load-extension=/foo/bar/baz.js,/path/to/ext,#{pathToTheme}"
"--user-data-dir=/profile/dir"
"--disk-cache-dir=/profile/dir/CypressCache"
])

it "normalizes multiple extensions from plugins", ->
Expand All @@ -72,6 +74,7 @@ describe "lib/browsers/chrome", ->
"--foo=bar"
"--load-extension=/foo/bar/baz.js,/quux.js,/path/to/ext,#{pathToTheme}"
"--user-data-dir=/profile/dir"
"--disk-cache-dir=/profile/dir/CypressCache"
])

context "#_getArgs", ->
Expand Down
1 change: 1 addition & 0 deletions packages/server/test/unit/browsers/electron_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ describe "lib/browsers/electron", ->
context "._launch", ->
beforeEach ->
@sandbox.stub(menu, "set")
@sandbox.stub(electron, "_clearCache").resolves()
@sandbox.stub(electron, "_setProxy").resolves()
@sandbox.stub(electron, "_setUserAgent")

Expand Down

0 comments on commit 4db34c9

Please sign in to comment.