diff --git a/packages/driver/test/cypress/fixtures/foo.bar.baz.json b/packages/driver/test/cypress/fixtures/foo.bar.baz.json new file mode 100644 index 000000000000..9f00d3b8adcc --- /dev/null +++ b/packages/driver/test/cypress/fixtures/foo.bar.baz.json @@ -0,0 +1,3 @@ +{ + "quux": "quuz" +} diff --git a/packages/driver/test/cypress/integration/commands/fixtures_spec.coffee b/packages/driver/test/cypress/integration/commands/fixtures_spec.coffee index 1b2e510f0171..21fc29dabdd5 100644 --- a/packages/driver/test/cypress/integration/commands/fixtures_spec.coffee +++ b/packages/driver/test/cypress/integration/commands/fixtures_spec.coffee @@ -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() diff --git a/packages/server/lib/errors.coffee b/packages/server/lib/errors.coffee index 7154218bf779..77ee9c9609f8 100644 --- a/packages/server/lib/errors.coffee +++ b/packages/server/lib/errors.coffee @@ -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) diff --git a/packages/server/lib/fixture.coffee b/packages/server/lib/fixture.coffee index 442205ed4f09..5c015f88dc79 100644 --- a/packages/server/lib/fixture.coffee +++ b/packages/server/lib/fixture.coffee @@ -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 = {} @@ -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) + + 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 @@ -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) diff --git a/packages/server/test/unit/fixture_spec.coffee b/packages/server/test/unit/fixture_spec.coffee index 2bf082cf1c9c..2ad0cfae8a93 100644 --- a/packages/server/test/unit/fixture_spec.coffee +++ b/packages/server/test/unit/fixture_spec.coffee @@ -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", -> diff --git a/packages/server/test/unit/socket_spec.coffee b/packages/server/test/unit/socket_spec.coffee index 9503720e859e..cc07d6f713d6 100644 --- a/packages/server/test/unit/socket_spec.coffee +++ b/packages/server/test/unit/socket_spec.coffee @@ -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)