Skip to content

Commit

Permalink
Fix automatically loading fixtures with more than one . in the name (c…
Browse files Browse the repository at this point in the history
…ypress-io#3606)

Fixes cypress-io#1402

Also fixes a few TODO comments in the fixtures.coffee file along the way
  • Loading branch information
flotwig authored and kuceb committed Mar 14, 2019
1 parent 35b21ab commit d57acef
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 32 deletions.
3 changes: 3 additions & 0 deletions packages/driver/test/cypress/fixtures/foo.bar.baz.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"quux": "quuz"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand All @@ -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()

Expand Down
13 changes: 13 additions & 0 deletions packages/server/lib/errors.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
70 changes: 43 additions & 27 deletions packages/server/lib/fixture.coffee
Original file line number Diff line number Diff line change
@@ -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")

extensions = ".json .js .coffee .html .txt .csv .png .jpg .jpeg .gif .tif .tiff .zip".split(" ")
glob = require("./util/glob")

extensions = [
".json",
".js",
".coffee",
".html",
".txt",
".csv",
".png",
".jpg",
".jpeg",
".gif",
".tif",
".tiff",
".zip"
]

queue = {}

Expand All @@ -23,46 +40,47 @@ 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)

ext = path.extname(fixture)
@parseFile(p, fixture, options)
.catch (e) ->
if e.code != "ENOENT"
throw e

if not ext
throw new Error("No fixture file found with an acceptable extension. Searched in: #{p}")
pattern = "#{p}{#{extensions.join(",")}}"

@fileExists(p + ext)
.catch ->
tryParsingFile(index + 1)
.then ->
@parseFile(p + ext, fixture, ext, options)
glob(pattern, { nosort: true }).bind(@)
.then (matches) ->
if matches.length == 0
relativePath = path.relative('.', p)
errors.throw("FIXTURE_NOT_FOUND", relativePath, extensions)

Promise.resolve tryParsingFile(0)
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

cleanup = ->
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()
Expand All @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions packages/server/test/unit/fixture_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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", ->
Expand Down Expand Up @@ -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", ->
Expand Down
3 changes: 2 additions & 1 deletion packages/server/test/unit/socket_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d57acef

Please sign in to comment.