|
| 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 | +}); |
0 commit comments