(args: ComparisonArgs)
| 151 | }; |
| 152 | |
| 153 | export function compareJsonFileToJson(args: ComparisonArgs) { |
| 154 | debug(args); |
| 155 | |
| 156 | const { expectedFile, strict } = args; |
| 157 | const given: any = args.given; |
| 158 | |
| 159 | const jsonString = given.file |
| 160 | ? callAndReportFailure("Could not read JSON output file", () => |
| 161 | fs.readFileSync(given.file, "utf8") |
| 162 | ) |
| 163 | : callAndReportFailure( |
| 164 | "Could not run command for JSON output", |
| 165 | () => exec(given.command).stdout |
| 166 | ); |
| 167 | |
| 168 | const givenJSON = callAndReportFailure("Could not parse output JSON", () => |
| 169 | JSON.parse(jsonString) |
| 170 | ); |
| 171 | const expectedJSON = callAndReportFailure( |
| 172 | "Could not read or parse expected JSON file", |
| 173 | () => JSON.parse(fs.readFileSync(expectedFile, "utf8")) |
| 174 | ); |
| 175 | |
| 176 | const allowMissingNull = !!args.allowMissingNull; |
| 177 | let jsonAreEqual = strict |
| 178 | ? callAndReportFailure("Failed to strictly compare objects", () => |
| 179 | strictDeepEquals(givenJSON, expectedJSON) |
| 180 | ) |
| 181 | : callAndReportFailure("Failed to compare objects.", () => |
| 182 | deepEquals(expectedJSON, givenJSON, allowMissingNull) |
| 183 | ); |
| 184 | |
| 185 | if (!jsonAreEqual) { |
| 186 | failWith("Error: Output is not equivalent to input.", { |
| 187 | expectedFile, |
| 188 | given |
| 189 | }); |
| 190 | } |
| 191 | } |
searching dependent graphs…