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

Allow %, &, and ? in spec file name. #6539

Merged
merged 11 commits into from
Mar 5, 2020
5 changes: 5 additions & 0 deletions packages/server/lib/controllers/files.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ glob = require("../util/glob")
specsUtil = require("../util/specs")
pathHelpers = require("../util/path_helpers")
CacheBuster = require("../util/cache_buster")
{ escapeFilenameInUrl } = require('../util/escape_filename')

module.exports = {
handleFiles: (req, res, config) ->
Expand All @@ -22,6 +23,10 @@ module.exports = {

@getSpecs(test, config)
.then (specs) =>
specs = specs.map((fileName) =>
return escapeFilenameInUrl(fileName)
)

@getJavascripts(config)
.then (js) =>
res.render iframePath, {
Expand Down
5 changes: 3 additions & 2 deletions packages/server/lib/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const fs = require('./util/fs')
const keys = require('./util/keys')
const settings = require('./util/settings')
const specsUtil = require('./util/specs')
const { escapeFilenameInUrl } = require('./util/escape_filename')

const localCwd = cwd()

Expand Down Expand Up @@ -519,11 +520,11 @@ class Project extends EE {
normalizeSpecUrl (browserUrl, specUrl) {
const replacer = (match) => match.replace('//', '/')

return [
return escapeFilenameInUrl([
browserUrl,
'#/tests',
specUrl,
].join('/').replace(multipleForwardSlashesRe, replacer)
].join('/').replace(multipleForwardSlashesRe, replacer))
}

scaffold (cfg) {
Expand Down
17 changes: 17 additions & 0 deletions packages/server/lib/util/escape_filename.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import _ from 'lodash'

const ampersandRe = /&/g
const percentRe = /%/g
const questionRe = /\?/g

export function escapeFilenameInUrl (url: string) {
chrisbreiding marked this conversation as resolved.
Show resolved Hide resolved
let paths = url.split('/')

// escape valid file name characters that cannot be used in URL
paths[paths.length - 1] = (_.last<string>(paths) || '')
sainthkh marked this conversation as resolved.
Show resolved Hide resolved
.replace(percentRe, '%25') // %
.replace(ampersandRe, '%26') // &
.replace(questionRe, '%3F') // ? -> it's only valid in Linux

return paths.join('/')
}
6 changes: 5 additions & 1 deletion packages/server/test/integration/http_requests_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -356,14 +356,18 @@ describe "Routes", ->

body = res.body

expect(body.integration).to.have.length(3)
expect(body.integration).to.have.length(4)

## remove the absolute path key
body.integration = _.map body.integration, (obj) ->
_.pick(obj, "name", "relative")

expect(res.body).to.deep.eq({
integration: [
{
"name": "sub/a&b%c.js"
"relative": "tests/sub/a&b%c.js"
}
{
name: "sub/sub_test.coffee"
relative: "tests/sub/sub_test.coffee"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* eslint-disable mocha/no-global-tests */
it('is truthy', function () {
expect(true).to.be.true
})
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
</script>
<script type="text/javascript" src="/__cypress/tests?p=tests/_support/spec_helper.js-123"></script>
<script type="text/javascript" src="/__cypress/tests?p=tests/etc/etc.js-123"></script>
<script type="text/javascript" src="/__cypress/tests?p=tests/sub/a%26b%25c.js-123"></script>
<script type="text/javascript" src="/__cypress/tests?p=tests/sub/sub_test.coffee-123"></script>
<script type="text/javascript" src="/__cypress/tests?p=tests/test1.js-123"></script>
<script type="text/javascript" src="/__cypress/tests?p=tests/test2.coffee-123"></script>
Expand Down
29 changes: 29 additions & 0 deletions packages/server/test/unit/project_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,35 @@ This option will not have an effect in Some-other-name. Tests that rely on web s
})
})

it('escapses %, &', function () {
const todosSpec = path.join(this.todosPath, 'tests/sub/a&b%c.js')

return this.project.getSpecUrl(todosSpec)
.then((str) => {
expect(str).to.eq('http://localhost:8888/__/#/tests/integration/sub/a%26b%25c.js')
})
})

if (process.platform !== 'win32') {
// ? is tested here because it's valid in Unix/Linux, but invalid in Windows
// https://gist.github.com/doctaphred/d01d05291546186941e1b7ddc02034d3
it('escapes ?', function () {
const filePath = path.join(__dirname, '../support/fixtures/projects/todos/tests/sub/a?.spec.js')

fs.writeFileSync(filePath, '')
sainthkh marked this conversation as resolved.
Show resolved Hide resolved

const todosSpec = path.join(this.todosPath, 'tests/sub/a?.spec.js')

return this.project.getSpecUrl(todosSpec)
.then((str) => {
expect(str).to.eq('http://localhost:8888/__/#/tests/integration/sub/a%3F.spec.js')
})
.then(() => {
fs.unlinkSync(filePath)
})
})
}

it('returns __all spec url', function () {
return this.project.getSpecUrl()
.then((str) => {
Expand Down