|
1 | 1 | import path from "node:path";
|
2 | 2 | import core from "@actions/core";
|
3 | 3 | import { assert } from "chai";
|
| 4 | +import { YAMLException } from "js-yaml"; |
4 | 5 | import Config from "../src/config.js";
|
5 | 6 | import Content from "../src/content.js";
|
6 | 7 | import SlackError from "../src/errors.js";
|
@@ -190,6 +191,11 @@ describe("content", () => {
|
190 | 191 | err.message,
|
191 | 192 | "Invalid input! Failed to parse contents of the provided payload",
|
192 | 193 | );
|
| 194 | + assert.isDefined(err.cause?.values); |
| 195 | + assert.equal(err.cause.values.length, 2); |
| 196 | + const [jsonError, yamlError] = err.cause.values; |
| 197 | + assert.isTrue(jsonError instanceof SyntaxError); |
| 198 | + assert.isTrue(yamlError instanceof YAMLException); |
193 | 199 | } else {
|
194 | 200 | assert.fail("Failed to throw a SlackError", err);
|
195 | 201 | }
|
@@ -334,14 +340,63 @@ describe("content", () => {
|
334 | 340 | err.message,
|
335 | 341 | "Invalid input! Failed to parse contents of the provided payload file",
|
336 | 342 | );
|
| 343 | + assert.isDefined(err.cause?.values); |
| 344 | + assert.equal(err.cause.values.length, 1); |
337 | 345 | assert.include(
|
338 |
| - err.cause.message, |
| 346 | + err.cause.values[0].message, |
339 | 347 | "Invalid input! Failed to parse file extension unknown.md",
|
340 | 348 | );
|
341 | 349 | } else {
|
342 | 350 | assert.fail("Failed to throw a SlackError", err);
|
343 | 351 | }
|
344 | 352 | }
|
345 | 353 | });
|
| 354 | + |
| 355 | + it("fails if invalid JSON exists in the input payload", async () => { |
| 356 | + mocks.core.getInput.withArgs("payload-file-path").returns("example.json"); |
| 357 | + mocks.fs.readFileSync |
| 358 | + .withArgs(path.resolve("example.json"), "utf-8") |
| 359 | + .returns(`{ |
| 360 | + "message": "a truncated file without an end`); |
| 361 | + try { |
| 362 | + await send(mocks.core); |
| 363 | + assert.fail("Failed to throw for invalid JSON"); |
| 364 | + } catch (err) { |
| 365 | + if (err instanceof SlackError) { |
| 366 | + assert.include( |
| 367 | + err.message, |
| 368 | + "Invalid input! Failed to parse contents of the provided payload file", |
| 369 | + ); |
| 370 | + assert.isDefined(err.cause?.values); |
| 371 | + assert.equal(err.cause.values.length, 1); |
| 372 | + assert.isTrue(err.cause.values[0] instanceof SyntaxError); |
| 373 | + } else { |
| 374 | + assert.fail("Failed to throw a SlackError", err); |
| 375 | + } |
| 376 | + } |
| 377 | + }); |
| 378 | + |
| 379 | + it("fails if invalid YAML exists in the input payload", async () => { |
| 380 | + mocks.core.getInput.withArgs("payload-file-path").returns("example.yaml"); |
| 381 | + mocks.fs.readFileSync |
| 382 | + .withArgs(path.resolve("example.yaml"), "utf-8") |
| 383 | + .returns(`- "message": "assigned": "values"`); |
| 384 | + try { |
| 385 | + await send(mocks.core); |
| 386 | + assert.fail("Failed to throw for invalid YAML"); |
| 387 | + } catch (err) { |
| 388 | + if (err instanceof SlackError) { |
| 389 | + assert.include( |
| 390 | + err.message, |
| 391 | + "Invalid input! Failed to parse contents of the provided payload file", |
| 392 | + ); |
| 393 | + assert.isDefined(err.cause?.values); |
| 394 | + assert.equal(err.cause.values.length, 1); |
| 395 | + assert.isTrue(err.cause.values[0] instanceof YAMLException); |
| 396 | + } else { |
| 397 | + assert.fail("Failed to throw a SlackError", err); |
| 398 | + } |
| 399 | + } |
| 400 | + }); |
346 | 401 | });
|
347 | 402 | });
|
0 commit comments