|
| 1 | +import anyTest, {TestFn} from "ava"; |
| 2 | +import * as sinon from "sinon"; |
| 3 | +import esmock from "esmock"; |
| 4 | +import {readFile} from "fs/promises"; |
| 5 | + |
| 6 | +const test = anyTest as TestFn<{ |
| 7 | + sinon: sinon.SinonSandbox; |
| 8 | + readFileStub: sinon.SinonStub; |
| 9 | + manifestFileContent: string; |
| 10 | + getManifestSchemaStub: sinon.SinonStub; |
| 11 | + runValidation: typeof import("../../../../src/tools/run_manifest_validation/runValidation.js").default; |
| 12 | +}>; |
| 13 | + |
| 14 | +test.beforeEach(async (t) => { |
| 15 | + t.context.sinon = sinon.createSandbox(); |
| 16 | + t.context.manifestFileContent = ""; |
| 17 | + |
| 18 | + // Create a stub that only intercepts specific manifest paths, otherwise calls real readFile |
| 19 | + t.context.readFileStub = t.context.sinon.stub().callsFake(async ( |
| 20 | + path: string, |
| 21 | + encoding?: BufferEncoding | null |
| 22 | + ) => { |
| 23 | + // Only handle specific manifest paths that we explicitly stub |
| 24 | + if (path === "/path/to/manifest.json") { |
| 25 | + // These will be handled by withArgs() stubs below |
| 26 | + return t.context.manifestFileContent; |
| 27 | + } |
| 28 | + // For all other files (including AJV schema files), call the real readFile |
| 29 | + return readFile(path, encoding ?? "utf-8"); |
| 30 | + }); |
| 31 | + |
| 32 | + t.context.getManifestSchemaStub = t.context.sinon.stub(); |
| 33 | + |
| 34 | + // Import the runValidation function |
| 35 | + t.context.runValidation = (await esmock( |
| 36 | + "../../../../src/tools/run_manifest_validation/runValidation.js", { |
| 37 | + "fs/promises": { |
| 38 | + readFile: t.context.readFileStub, |
| 39 | + }, |
| 40 | + "../../../../src/utils/ui5Manifest.js": { |
| 41 | + getManifestSchema: t.context.getManifestSchemaStub, |
| 42 | + }, |
| 43 | + } |
| 44 | + )).default; |
| 45 | +}); |
| 46 | + |
| 47 | +test.afterEach.always((t) => { |
| 48 | + t.context.sinon.restore(); |
| 49 | +}); |
| 50 | + |
| 51 | +test("runValidation successfully validates valid manifest", async (t) => { |
| 52 | + const {runValidation, getManifestSchemaStub} = t.context; |
| 53 | + |
| 54 | + // Stub the readFile function to return a valid manifest |
| 55 | + const validManifest = { |
| 56 | + "sap.app": { |
| 57 | + id: "my.app.id", |
| 58 | + type: "application", |
| 59 | + }, |
| 60 | + }; |
| 61 | + t.context.manifestFileContent = JSON.stringify(validManifest); |
| 62 | + |
| 63 | + getManifestSchemaStub.resolves({ |
| 64 | + type: "object", |
| 65 | + properties: { |
| 66 | + "sap.app": { |
| 67 | + type: "object", |
| 68 | + properties: { |
| 69 | + id: {type: "string"}, |
| 70 | + type: {type: "string"}, |
| 71 | + }, |
| 72 | + required: ["id", "type"], |
| 73 | + }, |
| 74 | + }, |
| 75 | + required: ["sap.app"], |
| 76 | + }); |
| 77 | + |
| 78 | + const result = await runValidation("/path/to/manifest.json"); |
| 79 | + |
| 80 | + t.deepEqual(result, { |
| 81 | + isValid: true, |
| 82 | + errors: [], |
| 83 | + }); |
| 84 | +}); |
| 85 | + |
| 86 | +test("runValidation successfully validates invalid manifest", async (t) => { |
| 87 | + const {runValidation, getManifestSchemaStub} = t.context; |
| 88 | + |
| 89 | + // Stub the readFile function to return an invalid manifest |
| 90 | + const invalidManifest = { |
| 91 | + "sap.app": { |
| 92 | + id: "my.app.id", |
| 93 | + // Missing required field "type" |
| 94 | + }, |
| 95 | + }; |
| 96 | + t.context.manifestFileContent = JSON.stringify(invalidManifest); |
| 97 | + |
| 98 | + getManifestSchemaStub.resolves({ |
| 99 | + type: "object", |
| 100 | + properties: { |
| 101 | + "sap.app": { |
| 102 | + type: "object", |
| 103 | + properties: { |
| 104 | + id: {type: "string"}, |
| 105 | + type: {type: "string"}, |
| 106 | + }, |
| 107 | + required: ["id", "type"], |
| 108 | + }, |
| 109 | + }, |
| 110 | + required: ["sap.app"], |
| 111 | + additionalProperties: false, |
| 112 | + }); |
| 113 | + |
| 114 | + const result = await runValidation("/path/to/manifest.json"); |
| 115 | + |
| 116 | + t.deepEqual(result, { |
| 117 | + isValid: false, |
| 118 | + errors: [ |
| 119 | + { |
| 120 | + params: {missingProperty: "type"}, |
| 121 | + keyword: "required", |
| 122 | + instancePath: "/sap.app", |
| 123 | + schemaPath: "#/properties/sap.app/required", |
| 124 | + message: "must have required property 'type'", |
| 125 | + propertyName: undefined, |
| 126 | + schema: undefined, |
| 127 | + parentSchema: undefined, |
| 128 | + data: undefined, |
| 129 | + }, |
| 130 | + ], |
| 131 | + }); |
| 132 | +}); |
| 133 | + |
| 134 | +test("runValidation throws error when manifest file path is not correct", async (t) => { |
| 135 | + const {runValidation, readFileStub} = t.context; |
| 136 | + |
| 137 | + // Stub the readFile function to throw an error |
| 138 | + readFileStub.rejects(new Error("File not found")); |
| 139 | + |
| 140 | + await t.throwsAsync(async () => { |
| 141 | + const result = await runValidation("/nonexistent/path"); |
| 142 | + return result; |
| 143 | + }, { |
| 144 | + instanceOf: Error, |
| 145 | + message: /Failed to read manifest file at .+: .+/, |
| 146 | + }); |
| 147 | +}); |
| 148 | + |
| 149 | +test("runValidation throws error when manifest file content is invalid JSON", async (t) => { |
| 150 | + const {runValidation} = t.context; |
| 151 | + |
| 152 | + t.context.manifestFileContent = "Invalid JSON Content"; |
| 153 | + |
| 154 | + await t.throwsAsync(async () => { |
| 155 | + const result = await runValidation("/path/to/manifest.json"); |
| 156 | + return result; |
| 157 | + }, { |
| 158 | + instanceOf: Error, |
| 159 | + message: /Failed to parse manifest file at .+ as JSON: .+/, |
| 160 | + }); |
| 161 | +}); |
| 162 | + |
| 163 | +test("runValidation throws error when schema validation function cannot be compiled", async (t) => { |
| 164 | + const {runValidation, getManifestSchemaStub} = t.context; |
| 165 | + |
| 166 | + t.context.manifestFileContent = JSON.stringify({}); |
| 167 | + getManifestSchemaStub.resolves(null); // Simulate invalid schema |
| 168 | + |
| 169 | + await t.throwsAsync(async () => { |
| 170 | + const result = await runValidation("/path/to/manifest.json"); |
| 171 | + return result; |
| 172 | + }, { |
| 173 | + instanceOf: Error, |
| 174 | + message: /Failed to create UI5 manifest validate function: .+/, |
| 175 | + }); |
| 176 | +}); |
0 commit comments