mirror of
https://github.com/cypress-io/cypress.git
synced 2026-02-06 15:20:25 -06:00
Fix automatically loading fixtures with more than one . in the name (#3606)
Fixes #1402 Also fixes a few TODO comments in the fixtures.coffee file along the way
This commit is contained in:
3
packages/driver/test/cypress/fixtures/foo.bar.baz.json
Normal file
3
packages/driver/test/cypress/fixtures/foo.bar.baz.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"quux": "quuz"
|
||||
}
|
||||
@@ -45,6 +45,9 @@ describe "src/cy/commands/fixtures", ->
|
||||
it "really works", ->
|
||||
cy.fixture("example").should("deep.eq", { example: true })
|
||||
|
||||
it "can read a fixture without extension with multiple dots in the name", ->
|
||||
cy.fixture("foo.bar.baz").should("deep.eq", { quux: "quuz" })
|
||||
|
||||
it "looks for csv without extension", ->
|
||||
cy.fixture("comma-separated").should "equal", """
|
||||
One,Two,Three
|
||||
@@ -97,7 +100,7 @@ describe "src/cy/commands/fixtures", ->
|
||||
expect(lastLog.get("name")).to.eq "fixture"
|
||||
expect(lastLog.get("message")).to.eq "err"
|
||||
|
||||
expect(err.message).to.include "No fixture file found with an acceptable extension. Searched in:"
|
||||
expect(err.message).to.include "A fixture file could not be found"
|
||||
expect(err.message).to.include "cypress/fixtures/err"
|
||||
done()
|
||||
|
||||
@@ -113,7 +116,7 @@ describe "src/cy/commands/fixtures", ->
|
||||
expect(lastLog.get("name")).to.eq "fixture"
|
||||
expect(lastLog.get("message")).to.eq "err.txt"
|
||||
|
||||
expect(err.message).to.include "No fixture exists at:"
|
||||
expect(err.message).to.include "A fixture file could not be found"
|
||||
expect(err.message).to.include "cypress/fixtures/err.txt"
|
||||
done()
|
||||
|
||||
|
||||
@@ -719,6 +719,19 @@ getMsgByType = (type, arg1 = {}, arg2) ->
|
||||
|
||||
#{arg1.link}
|
||||
"""
|
||||
when "FIXTURE_NOT_FOUND"
|
||||
"""
|
||||
A fixture file could not be found at any of the following paths:
|
||||
|
||||
> #{arg1}
|
||||
> #{arg1}{{extension}}
|
||||
|
||||
Cypress looked for these file extensions at the provided path:
|
||||
|
||||
> #{arg2.join(', ')}
|
||||
|
||||
Provide a path to an existing fixture file.
|
||||
"""
|
||||
|
||||
get = (type, arg1, arg2) ->
|
||||
msg = getMsgByType(type, arg1, arg2)
|
||||
|
||||
@@ -1,13 +1,30 @@
|
||||
_ = require("lodash")
|
||||
path = require("path")
|
||||
check = require("syntax-error")
|
||||
debug = require("debug")("cypress:server:fixture")
|
||||
coffee = require("../../../packages/coffee")
|
||||
Promise = require("bluebird")
|
||||
jsonlint = require("jsonlint")
|
||||
cwd = require("./cwd")
|
||||
errors = require("./errors")
|
||||
fs = require("./util/fs")
|
||||
glob = require("./util/glob")
|
||||
|
||||
extensions = ".json .js .coffee .html .txt .csv .png .jpg .jpeg .gif .tif .tiff .zip".split(" ")
|
||||
extensions = [
|
||||
".json",
|
||||
".js",
|
||||
".coffee",
|
||||
".html",
|
||||
".txt",
|
||||
".csv",
|
||||
".png",
|
||||
".jpg",
|
||||
".jpeg",
|
||||
".gif",
|
||||
".tif",
|
||||
".tiff",
|
||||
".zip"
|
||||
]
|
||||
|
||||
queue = {}
|
||||
|
||||
@@ -23,35 +40,38 @@ module.exports = {
|
||||
p = path.join(fixturesFolder, filePath)
|
||||
fixture = path.basename(p)
|
||||
|
||||
## if we have an extension go
|
||||
## ahead and read in the file
|
||||
if ext = path.extname(p)
|
||||
@parseFile(p, fixture, ext, options)
|
||||
else
|
||||
## change this to first glob for
|
||||
## the files, and if nothing is found
|
||||
## throw a better error message
|
||||
tryParsingFile = (index) =>
|
||||
ext = extensions[index]
|
||||
## if the file exists, go ahead and parse it
|
||||
## otherwise, glob for potential extensions
|
||||
@fileExists(p)
|
||||
.then ->
|
||||
debug("fixture exact name exists", p)
|
||||
|
||||
if not ext
|
||||
throw new Error("No fixture file found with an acceptable extension. Searched in: #{p}")
|
||||
ext = path.extname(fixture)
|
||||
@parseFile(p, fixture, options)
|
||||
.catch (e) ->
|
||||
if e.code != "ENOENT"
|
||||
throw e
|
||||
|
||||
@fileExists(p + ext)
|
||||
.catch ->
|
||||
tryParsingFile(index + 1)
|
||||
.then ->
|
||||
@parseFile(p + ext, fixture, ext, options)
|
||||
pattern = "#{p}{#{extensions.join(",")}}"
|
||||
|
||||
Promise.resolve tryParsingFile(0)
|
||||
glob(pattern, { nosort: true }).bind(@)
|
||||
.then (matches) ->
|
||||
if matches.length == 0
|
||||
relativePath = path.relative('.', p)
|
||||
errors.throw("FIXTURE_NOT_FOUND", relativePath, extensions)
|
||||
|
||||
debug("fixture matches found, using the first", matches)
|
||||
|
||||
ext = path.extname(matches[0])
|
||||
@parseFile(p + ext, fixture, options)
|
||||
|
||||
fileExists: (p) ->
|
||||
fs.statAsync(p).bind(@)
|
||||
|
||||
parseFile: (p, fixture, ext, options) ->
|
||||
parseFile: (p, fixture, options) ->
|
||||
if queue[p]
|
||||
Promise.delay(1).then =>
|
||||
@parseFile(p, fixture, ext)
|
||||
@parseFile(p, fixture, options)
|
||||
else
|
||||
queue[p] = true
|
||||
|
||||
@@ -59,10 +79,8 @@ module.exports = {
|
||||
delete queue[p]
|
||||
|
||||
@fileExists(p)
|
||||
.catch (err) ->
|
||||
## TODO: move this to lib/errors
|
||||
throw new Error("No fixture exists at: #{p}")
|
||||
.then ->
|
||||
ext = path.extname(p)
|
||||
@parseFileByExtension(p, fixture, ext, options)
|
||||
.then (ret) ->
|
||||
cleanup()
|
||||
@@ -74,8 +92,6 @@ module.exports = {
|
||||
throw err
|
||||
|
||||
parseFileByExtension: (p, fixture, ext, options = {}) ->
|
||||
ext ?= path.extname(fixture)
|
||||
|
||||
switch ext
|
||||
when ".json" then @parseJson(p, fixture)
|
||||
when ".js" then @parseJs(p, fixture)
|
||||
|
||||
@@ -32,7 +32,7 @@ describe "lib/fixture", ->
|
||||
.then ->
|
||||
throw new Error("should have failed but did not")
|
||||
.catch (err) =>
|
||||
expect(err.message).to.include "No fixture exists at:"
|
||||
expect(err.message).to.include "A fixture file could not be found"
|
||||
expect(err.message).to.include p
|
||||
|
||||
context "unicode escape syntax", ->
|
||||
@@ -315,7 +315,8 @@ describe "lib/fixture", ->
|
||||
throw new Error("should have failed but did not")
|
||||
.catch (err) =>
|
||||
p = @fixturesFolder + "/does-not-exist"
|
||||
expect(err.message).to.eq "No fixture file found with an acceptable extension. Searched in: #{p}"
|
||||
expect(err.message).to.include "A fixture file could not be found"
|
||||
expect(err.message).to.include "/does-not-exist"
|
||||
|
||||
context "new lines", ->
|
||||
it "does not remove trailing new lines on .txt", ->
|
||||
|
||||
@@ -332,7 +332,8 @@ describe "lib/socket", ->
|
||||
|
||||
it "errors when fixtures fails", (done) ->
|
||||
cb = (resp) ->
|
||||
expect(resp.error.message).to.include "No fixture exists at:"
|
||||
expect(resp.error.message).to.include "A fixture file could not be found"
|
||||
expect(resp.error.message).to.include "does-not-exist.txt"
|
||||
done()
|
||||
|
||||
@client.emit("backend:request", "get:fixture", "does-not-exist.txt", {}, cb)
|
||||
|
||||
Reference in New Issue
Block a user