Skip to content

Commit

Permalink
replace handlebars with squirrelly (#5521)
Browse files Browse the repository at this point in the history
* WIP: rip out handlebars and implement with squirrelly

- handle caching ourselves
- TODO: add tests, make sure escaping and all that jazz works

* fixes squirrelly template handling

* only fire mocha events when in run mode

* add unit tests for template engine rendering + caching
  • Loading branch information
brian-mann committed Oct 30, 2019
1 parent 3182f62 commit 91a4477
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
window.top.hasRunOnce ?= false
window.top.previousHash ?= window.top.location.hash

isTextTerminal = Cypress.config("isTextTerminal")

describe "rerun state bugs", ->
it "stores viewport globally and does not hang on re-runs", ->
## NOTE: there's probably other ways to cause a re-run
Expand All @@ -28,8 +30,8 @@ describe "rerun state bugs", ->
else
if window.top.location.hash is window.top.previousHash
## 3rd time around
## let the mocha end events fire
Cypress.config("isTextTerminal", true)
## let the mocha end events fire if they're supposed to
Cypress.config("isTextTerminal", isTextTerminal)
else
## our test has already run so remove
## the query param
Expand Down
2 changes: 1 addition & 1 deletion packages/runner/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
window.__Cypress__ = true

setTimeout(function(){
Runner.start(document.getElementById('app'), "{{{base64Config}}}")
Runner.start(document.getElementById('app'), "{{base64Config | safe}}")
}, 0)
</script>
</body>
Expand Down
8 changes: 4 additions & 4 deletions packages/server/lib/html/iframe.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
})(window.opener || window.parent);
</script>

{{#each javascripts}}
<script type="text/javascript" src="{{{this}}}"></script>
{{each(options.javascripts)}}
<script type="text/javascript" src="{{@this}}"></script>
{{/each}}

{{#each specs}}
<script type="text/javascript" src="{{{this}}}"></script>
{{each(options.specs)}}
<script type="text/javascript" src="{{@this}}"></script>
{{/each}}
</body>
</html>
8 changes: 2 additions & 6 deletions packages/server/lib/server.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
_ = require("lodash")
exphbs = require("express-handlebars")
url = require("url")
http = require("http")
concatStream = require("concat-stream")
Expand Down Expand Up @@ -31,6 +30,7 @@ logger = require("./logger")
Socket = require("./socket")
Request = require("./request")
fileServer = require("./file_server")
templateEngine = require("./template_engine")

DEFAULT_DOMAIN_NAME = "localhost"
fullyQualifiedRe = /^https?:\/\//
Expand Down Expand Up @@ -85,11 +85,7 @@ class Server

## since we use absolute paths, configure express-handlebars to not automatically find layouts
## https://github.com/cypress-io/cypress/issues/2891
app.engine("html", exphbs({
defaultLayout: false
layoutsDir: []
partialsDir: []
}))
app.engine("html", templateEngine.render)

## handle the proxied url in case
## we have not yet started our websocket server
Expand Down
29 changes: 29 additions & 0 deletions packages/server/lib/template_engine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const Sqrl = require('squirrelly')
const fs = require('./util/fs')

const cache = {}

module.exports = {
cache,

render (filePath, options, cb) {
const cachedFn = cache[filePath]

// if we already have a cachedFn function
if (cachedFn) {
// just return it and move in
return cb(null, cachedFn(options, Sqrl))
}

// else go read it off the filesystem
return fs
.readFileAsync(filePath, 'utf8')
.then((str) => {
// and cache the Sqrl compiled template fn
const compiledFn = cache[filePath] = Sqrl.Compile(str)

return compiledFn(options, Sqrl)
})
.asCallback(cb)
},
}
2 changes: 1 addition & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@
"evil-dns": "0.2.0",
"execa": "1.0.0",
"express": "4.16.4",
"express-handlebars": "3.0.2",
"find-process": "1.4.1",
"fix-path": "2.1.0",
"fluent-ffmpeg": "2.1.2",
Expand Down Expand Up @@ -125,6 +124,7 @@
"shell-env": "3.0.0",
"signal-exit": "3.0.2",
"sinon": "5.1.1",
"squirrelly": "7.7.0",
"strip-ansi": "3.0.1",
"syntax-error": "1.4.0",
"term-size": "2.1.0",
Expand Down
53 changes: 53 additions & 0 deletions packages/server/test/unit/template_engine_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require('../spec_helper')

const os = require('os')
const path = require('path')
const Bluebird = require('bluebird')
const { cache, render } = require('../../lib/template_engine')
const fs = require('../../lib/util/fs')

describe('lib/template_engine', () => {
it('renders and caches a template function', () => {
sinon.spy(fs, 'readFile')

expect(cache).to.deep.eq({})

const tmpPath = path.join(os.tmpdir(), 'index.html')

return fs
.writeFileAsync(tmpPath, 'My favorite template engine is {{favorite}}.')
.then(() => {
return Bluebird.fromCallback((cb) => {
const opts = {
favorite: 'Squirrelly',
}

return render(tmpPath, opts, cb)
})
})
.then((str) => {
expect(str).to.eq('My favorite template engine is Squirrelly.')

expect(fs.readFile).to.be.calledOnce

const compiledFn = cache[tmpPath]

expect(compiledFn).to.be.a('function')

return Bluebird.fromCallback((cb) => {
const opts = {
favorite: 'Squirrelly2',
}

return render(tmpPath, opts, cb)
})
.then((str) => {
expect(str).to.eq('My favorite template engine is Squirrelly2.')

expect(cache[tmpPath]).to.eq(compiledFn)

expect(fs.readFile).to.be.calledOnce
})
})
})
})

4 comments on commit 91a4477

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 91a4477 Oct 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AppVeyor has built the win32 ia32 version of the Test Runner.

You can install this pre-release platform-specific build using instructions at https://on.cypress.io/installing-cypress#Install-pre-release-version.

You will need to use custom CYPRESS_INSTALL_BINARY url and install Cypress using an url instead of the version.

set CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/3.5.1/win32-ia32/appveyor-develop-91a4477ad0d293949e19668f028bcbacc7bba174-28478974/cypress.zip
npm install https://cdn.cypress.io/beta/binary/3.5.1/win32-ia32/appveyor-develop-91a4477ad0d293949e19668f028bcbacc7bba174-28478974/cypress.zip

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 91a4477 Oct 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AppVeyor has built the win32 x64 version of the Test Runner.

You can install this pre-release platform-specific build using instructions at https://on.cypress.io/installing-cypress#Install-pre-release-version.

You will need to use custom CYPRESS_INSTALL_BINARY url and install Cypress using an url instead of the version.

set CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/3.5.1/win32-x64/appveyor-develop-91a4477ad0d293949e19668f028bcbacc7bba174-28478974/cypress.zip
npm install https://cdn.cypress.io/beta/binary/3.5.1/win32-x64/appveyor-develop-91a4477ad0d293949e19668f028bcbacc7bba174-28478974/cypress.zip

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 91a4477 Oct 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux x64 version of the Test Runner.

You can install this pre-release platform-specific build using instructions at https://on.cypress.io/installing-cypress#Install-pre-release-version.

You will need to use custom CYPRESS_INSTALL_BINARY url and install Cypress using an url instead of the version.

export CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/3.5.1/linux-x64/circle-develop-91a4477ad0d293949e19668f028bcbacc7bba174-177950/cypress.zip
npm install https://cdn.cypress.io/beta/npm/3.5.1/circle-develop-91a4477ad0d293949e19668f028bcbacc7bba174-177939/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 91a4477 Oct 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin x64 version of the Test Runner.

You can install this pre-release platform-specific build using instructions at https://on.cypress.io/installing-cypress#Install-pre-release-version.

You will need to use custom CYPRESS_INSTALL_BINARY url and install Cypress using an url instead of the version.

export CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/3.5.1/darwin-x64/circle-develop-91a4477ad0d293949e19668f028bcbacc7bba174-177953/cypress.zip
npm install https://cdn.cypress.io/beta/npm/3.5.1/circle-develop-91a4477ad0d293949e19668f028bcbacc7bba174-177944/cypress.tgz

Please sign in to comment.