Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle execution under root more correctly #154

Merged
merged 1 commit into from
Mar 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion lib/util/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 + "/";
}
};

/**
Expand Down
76 changes: 76 additions & 0 deletions test/specs/absolute-root/absolute-root.spec.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
3 changes: 3 additions & 0 deletions test/specs/absolute-root/bundled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"use strict";

module.exports = {};
3 changes: 3 additions & 0 deletions test/specs/absolute-root/dereferenced.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"use strict";

module.exports = {};
1 change: 1 addition & 0 deletions test/specs/absolute-root/empty-object.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
3 changes: 3 additions & 0 deletions test/specs/absolute-root/parsed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"use strict";

module.exports = {};