From ad769f1e9cd39164efb651421c6c1c79f7c1db77 Mon Sep 17 00:00:00 2001 From: Kir_Antipov Date: Tue, 21 Mar 2023 09:34:46 +0000 Subject: [PATCH] Made a reader for Quilt metadata --- src/loaders/quilt/quilt-metadata-reader.ts | 14 +++++++ .../quilt/quilt-metadata-reader.spec.ts | 41 +++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 src/loaders/quilt/quilt-metadata-reader.ts create mode 100644 tests/unit/loaders/quilt/quilt-metadata-reader.spec.ts diff --git a/src/loaders/quilt/quilt-metadata-reader.ts b/src/loaders/quilt/quilt-metadata-reader.ts new file mode 100644 index 0000000..a03e6f0 --- /dev/null +++ b/src/loaders/quilt/quilt-metadata-reader.ts @@ -0,0 +1,14 @@ +import { ZippedTextLoaderMetadataReader } from "@/loaders/zipped-loader-metadata-reader"; +import { QuiltMetadata } from "./quilt-metadata"; +import { QUILT_MOD_JSON, RawQuiltMetadata } from "./raw-quilt-metadata"; +/** + * A metadata reader that is able to read Quilt mod metadata from a zipped file. + */ +export class QuiltMetadataReader extends ZippedTextLoaderMetadataReader { + /** + * Constructs a new {@link QuiltMetadataReader} instance. + */ + constructor() { + super(QUILT_MOD_JSON, QuiltMetadata.from, JSON.parse); + } +} diff --git a/tests/unit/loaders/quilt/quilt-metadata-reader.spec.ts b/tests/unit/loaders/quilt/quilt-metadata-reader.spec.ts new file mode 100644 index 0000000..ecb6e08 --- /dev/null +++ b/tests/unit/loaders/quilt/quilt-metadata-reader.spec.ts @@ -0,0 +1,41 @@ +import { zipFile } from "@/../tests/utils/zip-utils"; +import mockFs from "mock-fs"; +import { QuiltMetadata } from "@/loaders/quilt/quilt-metadata"; +import { QuiltMetadataReader } from "@/loaders/quilt/quilt-metadata-reader"; + +beforeEach(async () => { + mockFs({ + "quilt.mod.jar": await zipFile([__dirname, "../../../content/quilt/quilt.mod.json"]), + "text.txt": "", + }); +}); + +afterEach(() => { + mockFs.restore(); +}); + +describe("QuiltMetadataReader", () => { + test("successfully reads quilt.mod.json", async () => { + const reader = new QuiltMetadataReader(); + + const metadata = await reader.readMetadataFile("quilt.mod.jar"); + + expect(metadata).toBeInstanceOf(QuiltMetadata); + }); + + test("returns undefined if file is not a Quilt mod", async () => { + const reader = new QuiltMetadataReader(); + + const metadata = await reader.readMetadataFile("text.txt"); + + expect(metadata).toBeUndefined(); + }); + + test("returns undefined if file does not exist", async () => { + const reader = new QuiltMetadataReader(); + + const metadata = await reader.readMetadataFile("text.json"); + + expect(metadata).toBeUndefined(); + }); +});