Skip to content

Commit 0dd90ca

Browse files
Merge pull request #154 from stoplightio/fix/root-execution
Handle execution under root more correctly
2 parents ee4498e + 2c49146 commit 0dd90ca

File tree

6 files changed

+99
-1
lines changed

6 files changed

+99
-1
lines changed

lib/util/url.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,19 @@ exports.resolve = require("url").resolve;
2929
* @returns {string}
3030
*/
3131
exports.cwd = function cwd () {
32-
return process.browser ? location.href : process.cwd() + "/";
32+
if (process.browser) {
33+
return location.href;
34+
}
35+
36+
let path = process.cwd();
37+
38+
let lastChar = path.slice(-1);
39+
if (lastChar === "/" || lastChar === "\\") {
40+
return path;
41+
}
42+
else {
43+
return path + "/";
44+
}
3345
};
3446

3547
/**
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
"use strict";
2+
3+
const { expect } = require("chai");
4+
const $RefParser = require("../../..");
5+
const path = require("../../utils/path");
6+
const helper = require("../../utils/helper");
7+
const url = require("../../../lib/util/url");
8+
const parsedSchema = require("./parsed");
9+
const dereferencedSchema = require("./dereferenced");
10+
const bundledSchema = require("./bundled");
11+
12+
describe("When executed in the context of root directory", () => {
13+
const { cwd } = url;
14+
const { cwd: processCwd } = process;
15+
16+
beforeEach(() => {
17+
url.cwd = function () {
18+
try {
19+
process.cwd = () => "/";
20+
return cwd.apply(null, arguments);
21+
}
22+
finally {
23+
process.cwd = processCwd;
24+
}
25+
};
26+
});
27+
28+
afterEach(() => {
29+
url.cwd = cwd;
30+
process.cwd = processCwd; // already restored at line 19, but just in case
31+
});
32+
33+
34+
it("should parse successfully from an absolute path", async () => {
35+
let parser = new $RefParser();
36+
const schema = await parser.parse(path.abs("specs/absolute-root/empty-object.json"));
37+
expect(schema).to.equal(parser.schema);
38+
expect(schema).to.deep.equal(parsedSchema);
39+
expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/absolute-root/empty-object.json")]);
40+
});
41+
42+
43+
it("should parse successfully from a url", async () => {
44+
let parser = new $RefParser();
45+
const schema = await parser.parse(path.url("specs/absolute-root/empty-object.json"));
46+
expect(schema).to.equal(parser.schema);
47+
expect(schema).to.deep.equal(parsedSchema);
48+
expect(parser.$refs.paths()).to.deep.equal([path.url("specs/absolute-root/empty-object.json")]);
49+
});
50+
51+
it("should resolve successfully from an absolute path", helper.testResolve(
52+
path.abs("specs/absolute-root/empty-object.json"),
53+
path.abs("specs/absolute-root/empty-object.json"), parsedSchema,
54+
));
55+
56+
it("should resolve successfully from a url", helper.testResolve(
57+
path.url("specs/absolute-root/empty-object.json"),
58+
path.url("specs/absolute-root/empty-object.json"), parsedSchema,
59+
));
60+
61+
it("should dereference successfully", async () => {
62+
let parser = new $RefParser();
63+
const schema = await parser.dereference(path.abs("specs/absolute-root/empty-object.json"));
64+
expect(schema).to.equal(parser.schema);
65+
expect(schema).to.deep.equal(dereferencedSchema);
66+
// The "circular" flag should NOT be set
67+
expect(parser.$refs.circular).to.equal(false);
68+
});
69+
70+
it("should bundle successfully", async () => {
71+
let parser = new $RefParser();
72+
const schema = await parser.bundle(path.abs("specs/absolute-root/empty-object.json"));
73+
expect(schema).to.equal(parser.schema);
74+
expect(schema).to.deep.equal(bundledSchema);
75+
});
76+
});

test/specs/absolute-root/bundled.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"use strict";
2+
3+
module.exports = {};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"use strict";
2+
3+
module.exports = {};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

test/specs/absolute-root/parsed.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"use strict";
2+
3+
module.exports = {};

0 commit comments

Comments
 (0)