|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const { expect } = require("chai"); |
| 4 | +const { resolve } = require("path"); |
| 5 | +const $RefParser = require("../../.."); |
| 6 | +const path = require("../../utils/path"); |
| 7 | +const helper = require("../../utils/helper"); |
| 8 | +const url = require("../../../lib/util/url"); |
| 9 | +const parsedSchema = require("./parsed"); |
| 10 | +const dereferencedSchema = require("./dereferenced"); |
| 11 | +const bundledSchema = require("./bundled"); |
| 12 | + |
| 13 | +describe("When executed in the context of root directory", () => { |
| 14 | + // Store the OS root directory |
| 15 | + const root = resolve("/"); |
| 16 | + |
| 17 | + // Store references to the original methods |
| 18 | + const originalProcessCwd = process.cwd; |
| 19 | + const originalUrlCwd = url.cwd; |
| 20 | + |
| 21 | + /** |
| 22 | + * A mock `process.cwd()` implementation that always returns the root diretory |
| 23 | + */ |
| 24 | + function mockProcessCwd () { |
| 25 | + return root; |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Temporarily mocks `process.cwd()` while calling the real `url.cwd()` implemenation |
| 30 | + */ |
| 31 | + function mockUrlCwd () { |
| 32 | + try { |
| 33 | + process.cwd = mockProcessCwd; |
| 34 | + return originalUrlCwd.apply(null, arguments); |
| 35 | + } |
| 36 | + finally { |
| 37 | + process.cwd = originalProcessCwd; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + beforeEach("Mock process.cwd and url.cwd", () => { |
| 42 | + url.cwd = mockUrlCwd; |
| 43 | + }); |
| 44 | + |
| 45 | + afterEach("Restore process.cwd and url.cwd", () => { |
| 46 | + url.cwd = originalUrlCwd; |
| 47 | + process.cwd = originalProcessCwd; // already restored by the finally block above, but just in case |
| 48 | + }); |
| 49 | + |
| 50 | + |
| 51 | + it("should parse successfully from an absolute path", async () => { |
| 52 | + let parser = new $RefParser(); |
| 53 | + const schema = await parser.parse(path.abs("specs/absolute-root/absolute-root.yaml")); |
| 54 | + expect(schema).to.equal(parser.schema); |
| 55 | + expect(schema).to.deep.equal(parsedSchema.schema); |
| 56 | + expect(parser.$refs.paths()).to.deep.equal([ |
| 57 | + path.abs("specs/absolute-root/absolute-root.yaml") |
| 58 | + ]); |
| 59 | + }); |
| 60 | + |
| 61 | + it("should parse successfully from a url", async () => { |
| 62 | + let parser = new $RefParser(); |
| 63 | + const schema = await parser.parse(path.url("specs/absolute-root/absolute-root.yaml")); |
| 64 | + expect(schema).to.equal(parser.schema); |
| 65 | + expect(schema).to.deep.equal(parsedSchema.schema); |
| 66 | + expect(parser.$refs.paths()).to.deep.equal([path.url("specs/absolute-root/absolute-root.yaml")]); |
| 67 | + }); |
| 68 | + |
| 69 | + it("should resolve successfully from an absolute path", helper.testResolve( |
| 70 | + path.abs("specs/absolute-root/absolute-root.yaml"), |
| 71 | + path.abs("specs/absolute-root/absolute-root.yaml"), parsedSchema.schema, |
| 72 | + path.abs("specs/absolute-root/definitions/definitions.json"), parsedSchema.definitions, |
| 73 | + path.abs("specs/absolute-root/definitions/name.yaml"), parsedSchema.name, |
| 74 | + path.abs("specs/absolute-root/definitions/required-string.yaml"), parsedSchema.requiredString |
| 75 | + )); |
| 76 | + |
| 77 | + it("should resolve successfully from a url", helper.testResolve( |
| 78 | + path.url("specs/absolute-root/absolute-root.yaml"), |
| 79 | + path.url("specs/absolute-root/absolute-root.yaml"), parsedSchema.schema, |
| 80 | + path.url("specs/absolute-root/definitions/definitions.json"), parsedSchema.definitions, |
| 81 | + path.url("specs/absolute-root/definitions/name.yaml"), parsedSchema.name, |
| 82 | + path.url("specs/absolute-root/definitions/required-string.yaml"), parsedSchema.requiredString |
| 83 | + )); |
| 84 | + |
| 85 | + it("should dereference successfully", async () => { |
| 86 | + let parser = new $RefParser(); |
| 87 | + const schema = await parser.dereference(path.abs("specs/absolute-root/absolute-root.yaml")); |
| 88 | + expect(schema).to.equal(parser.schema); |
| 89 | + expect(schema).to.deep.equal(dereferencedSchema); |
| 90 | + // Reference equality |
| 91 | + expect(schema.properties.name).to.equal(schema.definitions.name); |
| 92 | + expect(schema.definitions["required string"]) |
| 93 | + .to.equal(schema.definitions.name.properties.first) |
| 94 | + .to.equal(schema.definitions.name.properties.last) |
| 95 | + .to.equal(schema.properties.name.properties.first) |
| 96 | + .to.equal(schema.properties.name.properties.last); |
| 97 | + // The "circular" flag should NOT be set |
| 98 | + expect(parser.$refs.circular).to.equal(false); |
| 99 | + }); |
| 100 | + |
| 101 | + it("should bundle successfully", async () => { |
| 102 | + let parser = new $RefParser(); |
| 103 | + const schema = await parser.bundle(path.abs("specs/absolute-root/absolute-root.yaml")); |
| 104 | + expect(schema).to.equal(parser.schema); |
| 105 | + expect(schema).to.deep.equal(bundledSchema); |
| 106 | + }); |
| 107 | +}); |
0 commit comments