From 2c491469e9d4a0d7d73bdf6342b7e5c80d386796 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ro=C5=BCek?= Date: Mon, 24 Feb 2020 23:41:46 +0800 Subject: [PATCH] Handle execution under root more correctly --- lib/util/url.js | 14 +++- .../specs/absolute-root/absolute-root.spec.js | 76 +++++++++++++++++++ test/specs/absolute-root/bundled.js | 3 + test/specs/absolute-root/dereferenced.js | 3 + test/specs/absolute-root/empty-object.json | 1 + test/specs/absolute-root/parsed.js | 3 + 6 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 test/specs/absolute-root/absolute-root.spec.js create mode 100644 test/specs/absolute-root/bundled.js create mode 100644 test/specs/absolute-root/dereferenced.js create mode 100644 test/specs/absolute-root/empty-object.json create mode 100644 test/specs/absolute-root/parsed.js diff --git a/lib/util/url.js b/lib/util/url.js index b7157055..5c925a00 100644 --- a/lib/util/url.js +++ b/lib/util/url.js @@ -29,7 +29,19 @@ exports.resolve = require("url").resolve; * @returns {string} */ exports.cwd = function cwd () { - return process.browser ? location.href : process.cwd() + "/"; + if (process.browser) { + return location.href; + } + + let path = process.cwd(); + + let lastChar = path.slice(-1); + if (lastChar === "/" || lastChar === "\\") { + return path; + } + else { + return path + "/"; + } }; /** diff --git a/test/specs/absolute-root/absolute-root.spec.js b/test/specs/absolute-root/absolute-root.spec.js new file mode 100644 index 00000000..6d2e6bfc --- /dev/null +++ b/test/specs/absolute-root/absolute-root.spec.js @@ -0,0 +1,76 @@ +"use strict"; + +const { expect } = require("chai"); +const $RefParser = require("../../.."); +const path = require("../../utils/path"); +const helper = require("../../utils/helper"); +const url = require("../../../lib/util/url"); +const parsedSchema = require("./parsed"); +const dereferencedSchema = require("./dereferenced"); +const bundledSchema = require("./bundled"); + +describe("When executed in the context of root directory", () => { + const { cwd } = url; + const { cwd: processCwd } = process; + + beforeEach(() => { + url.cwd = function () { + try { + process.cwd = () => "/"; + return cwd.apply(null, arguments); + } + finally { + process.cwd = processCwd; + } + }; + }); + + afterEach(() => { + url.cwd = cwd; + process.cwd = processCwd; // already restored at line 19, but just in case + }); + + + it("should parse successfully from an absolute path", async () => { + let parser = new $RefParser(); + const schema = await parser.parse(path.abs("specs/absolute-root/empty-object.json")); + expect(schema).to.equal(parser.schema); + expect(schema).to.deep.equal(parsedSchema); + expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/absolute-root/empty-object.json")]); + }); + + + it("should parse successfully from a url", async () => { + let parser = new $RefParser(); + const schema = await parser.parse(path.url("specs/absolute-root/empty-object.json")); + expect(schema).to.equal(parser.schema); + expect(schema).to.deep.equal(parsedSchema); + expect(parser.$refs.paths()).to.deep.equal([path.url("specs/absolute-root/empty-object.json")]); + }); + + it("should resolve successfully from an absolute path", helper.testResolve( + path.abs("specs/absolute-root/empty-object.json"), + path.abs("specs/absolute-root/empty-object.json"), parsedSchema, + )); + + it("should resolve successfully from a url", helper.testResolve( + path.url("specs/absolute-root/empty-object.json"), + path.url("specs/absolute-root/empty-object.json"), parsedSchema, + )); + + it("should dereference successfully", async () => { + let parser = new $RefParser(); + const schema = await parser.dereference(path.abs("specs/absolute-root/empty-object.json")); + expect(schema).to.equal(parser.schema); + expect(schema).to.deep.equal(dereferencedSchema); + // The "circular" flag should NOT be set + expect(parser.$refs.circular).to.equal(false); + }); + + it("should bundle successfully", async () => { + let parser = new $RefParser(); + const schema = await parser.bundle(path.abs("specs/absolute-root/empty-object.json")); + expect(schema).to.equal(parser.schema); + expect(schema).to.deep.equal(bundledSchema); + }); +}); diff --git a/test/specs/absolute-root/bundled.js b/test/specs/absolute-root/bundled.js new file mode 100644 index 00000000..1f5824a4 --- /dev/null +++ b/test/specs/absolute-root/bundled.js @@ -0,0 +1,3 @@ +"use strict"; + +module.exports = {}; diff --git a/test/specs/absolute-root/dereferenced.js b/test/specs/absolute-root/dereferenced.js new file mode 100644 index 00000000..1f5824a4 --- /dev/null +++ b/test/specs/absolute-root/dereferenced.js @@ -0,0 +1,3 @@ +"use strict"; + +module.exports = {}; diff --git a/test/specs/absolute-root/empty-object.json b/test/specs/absolute-root/empty-object.json new file mode 100644 index 00000000..0967ef42 --- /dev/null +++ b/test/specs/absolute-root/empty-object.json @@ -0,0 +1 @@ +{} diff --git a/test/specs/absolute-root/parsed.js b/test/specs/absolute-root/parsed.js new file mode 100644 index 00000000..1f5824a4 --- /dev/null +++ b/test/specs/absolute-root/parsed.js @@ -0,0 +1,3 @@ +"use strict"; + +module.exports = {};