Skip to content

Commit

Permalink
Allow %, &, and ? in spec file name. (#6539)
Browse files Browse the repository at this point in the history
* Allow %, & in file name.

* Extract escapeFilenameInUrl

* Fix tests.

* Add test for ? (non-Windows).

* Remove platform condition. And handle when special chars are in the dir.

* Fix failure.

* Fix failure.

* add e2e test for specs with special characters

* minor refactor

Co-authored-by: Chris Breiding <chrisbreiding@users.noreply.github.com>
  • Loading branch information
chrisbreiding committed Mar 5, 2020
1 parent 474b80a commit edb9a98
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 3 deletions.
11 changes: 10 additions & 1 deletion packages/server/lib/controllers/files.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ glob = require("../util/glob")
specsUtil = require("../util/specs")
pathHelpers = require("../util/path_helpers")
CacheBuster = require("../util/cache_buster")
{ escapeFilenameInUrl } = require('../util/escape_filename')

SPEC_URL_PREFIX = "/__cypress/tests?p"

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

@getSpecs(test, config)
.then (specs) =>
specs = specs.map((fileName) =>
fileName = fileName.replace(SPEC_URL_PREFIX, "__CYPRESS_SPEC_URL_PREFIX__")

return escapeFilenameInUrl(fileName).replace("__CYPRESS_SPEC_URL_PREFIX__", SPEC_URL_PREFIX)
)

@getJavascripts(config)
.then (js) =>
res.render iframePath, {
Expand Down Expand Up @@ -63,7 +72,7 @@ module.exports = {

getTestUrl: (file) ->
file += CacheBuster.get()
"/__cypress/tests?p=#{file}"
"#{SPEC_URL_PREFIX}=#{file}"

getTitle: (test) ->
if test is "__all" then "All Tests" else test
Expand Down
3 changes: 2 additions & 1 deletion 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 @@ -522,7 +523,7 @@ class Project extends EE {
return [
browserUrl,
'#/tests',
specUrl,
escapeFilenameInUrl(specUrl),
].join('/').replace(multipleForwardSlashesRe, replacer)
}

Expand Down
11 changes: 11 additions & 0 deletions packages/server/lib/util/escape_filename.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const ampersandRe = /&/g
const percentRe = /%/g
const questionRe = /\?/g

export function escapeFilenameInUrl (url: string) {
// escape valid file name characters that cannot be used in URL
return url
.replace(percentRe, '%25') // %
.replace(ampersandRe, '%26') // &
.replace(questionRe, '%3F') // ? -> it's only valid in Linux
}
22 changes: 22 additions & 0 deletions packages/server/test/e2e/2_controllers_spec.coffee
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
fs = require("fs-extra")
path = require("path")

e2e = require("../support/helpers/e2e")
Fixtures = require("../support/helpers/fixtures")

nonExistentSpec = Fixtures.projectPath("non-existent-spec")
e2eProject = Fixtures.projectPath("e2e")

describe "e2e plugins", ->
e2e.setup()
Expand All @@ -14,3 +18,21 @@ describe "e2e plugins", ->
snapshot: true
expectedExitCode: 1
})

it "handles specs with $, &, ? in file name", ->
relativeSpecPath = path.join("d?ir&1%", "%di?r2&", "s%pec&?.js")
## windows doesn't support ? in file names
if process.platform is "win32"
relativeSpecPath = specPath.replace(/\?/, "")
specPath = path.join(e2eProject, "cypress", "integration", relativeSpecPath)

fs.outputFile(specPath, "it('passes', () => {})")
.then =>
e2e.exec(@, {
spec: specPath
sanitizeScreenshotDimensions: true
})
.then ({ stdout }) ->
expect(stdout).to.include("1 found (#{relativeSpecPath})")
expect(stdout).to.include("Running: #{relativeSpecPath}")
expect(stdout).to.include("Finished processing: /XXX/XXX/XXX/cypress/videos/#{relativeSpecPath}.mp4")
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')
})
})

// ? is invalid in Windows, but it can be tested here
// because it's a unit test and doesn't check the existence of files
it('escapes ?', function () {
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')
})
})

it('escapes %, &, ? in the url dir', function () {
const todosSpec = path.join(this.todosPath, 'tests/s%&?ub/a.spec.js')

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

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

4 comments on commit edb9a98

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on edb9a98 Mar 5, 2020

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/4.2.0/linux-x64/circle-develop-edb9a98268f2aa76b0605014409235e761799d1d-273372/cypress.zip
npm install https://cdn.cypress.io/beta/npm/4.2.0/circle-develop-edb9a98268f2aa76b0605014409235e761799d1d-273315/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on edb9a98 Mar 5, 2020

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/4.2.0/darwin-x64/circle-develop-edb9a98268f2aa76b0605014409235e761799d1d-273424/cypress.zip
npm install https://cdn.cypress.io/beta/npm/4.2.0/circle-develop-edb9a98268f2aa76b0605014409235e761799d1d-273379/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on edb9a98 Mar 5, 2020

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.

Instructions are included below, depending on the shell you are using.

In Command Prompt (cmd.exe):

set CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/4.2.0/win32-ia32/appveyor-develop-edb9a98268f2aa76b0605014409235e761799d1d-31271366/cypress.zip
npm install https://cdn.cypress.io/beta/npm/4.2.0/appveyor-develop-edb9a98268f2aa76b0605014409235e761799d1d-31271366/cypress.tgz

In PowerShell:

$env:CYPRESS_INSTALL_BINARY = https://cdn.cypress.io/beta/binary/4.2.0/win32-ia32/appveyor-develop-edb9a98268f2aa76b0605014409235e761799d1d-31271366/cypress.zip
npm install https://cdn.cypress.io/beta/npm/4.2.0/appveyor-develop-edb9a98268f2aa76b0605014409235e761799d1d-31271366/cypress.tgz

In Git Bash:

export CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/4.2.0/win32-ia32/appveyor-develop-edb9a98268f2aa76b0605014409235e761799d1d-31271366/cypress.zip
npm install https://cdn.cypress.io/beta/npm/4.2.0/appveyor-develop-edb9a98268f2aa76b0605014409235e761799d1d-31271366/cypress.tgz

Using cross-env:

If the above commands do not work for you, you can also try using cross-env:

npm i -g cross-env
cross-env CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/4.2.0/win32-ia32/appveyor-develop-edb9a98268f2aa76b0605014409235e761799d1d-31271366/cypress.zip npm install https://cdn.cypress.io/beta/npm/4.2.0/appveyor-develop-edb9a98268f2aa76b0605014409235e761799d1d-31271366/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on edb9a98 Mar 5, 2020

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.

Instructions are included below, depending on the shell you are using.

In Command Prompt (cmd.exe):

set CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/4.2.0/win32-x64/appveyor-develop-edb9a98268f2aa76b0605014409235e761799d1d-31271366/cypress.zip
npm install https://cdn.cypress.io/beta/npm/4.2.0/appveyor-develop-edb9a98268f2aa76b0605014409235e761799d1d-31271366/cypress.tgz

In PowerShell:

$env:CYPRESS_INSTALL_BINARY = https://cdn.cypress.io/beta/binary/4.2.0/win32-x64/appveyor-develop-edb9a98268f2aa76b0605014409235e761799d1d-31271366/cypress.zip
npm install https://cdn.cypress.io/beta/npm/4.2.0/appveyor-develop-edb9a98268f2aa76b0605014409235e761799d1d-31271366/cypress.tgz

In Git Bash:

export CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/4.2.0/win32-x64/appveyor-develop-edb9a98268f2aa76b0605014409235e761799d1d-31271366/cypress.zip
npm install https://cdn.cypress.io/beta/npm/4.2.0/appveyor-develop-edb9a98268f2aa76b0605014409235e761799d1d-31271366/cypress.tgz

Using cross-env:

If the above commands do not work for you, you can also try using cross-env:

npm i -g cross-env
cross-env CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/4.2.0/win32-x64/appveyor-develop-edb9a98268f2aa76b0605014409235e761799d1d-31271366/cypress.zip npm install https://cdn.cypress.io/beta/npm/4.2.0/appveyor-develop-edb9a98268f2aa76b0605014409235e761799d1d-31271366/cypress.tgz

Please sign in to comment.