Made a reader for Fabric metadata

This commit is contained in:
Kir_Antipov 2023-03-05 09:37:24 +00:00
parent 8987e70c81
commit ba8817193b
2 changed files with 56 additions and 0 deletions

View file

@ -0,0 +1,15 @@
import { ZippedTextLoaderMetadataReader } from "@/loaders/zipped-loader-metadata-reader";
import { FabricMetadata } from "./fabric-metadata";
import { FABRIC_MOD_JSON, RawFabricMetadata } from "./raw-fabric-metadata";
/**
* A metadata reader that is able to read Fabric mod metadata from a zipped file.
*/
export class FabricMetadataReader extends ZippedTextLoaderMetadataReader<FabricMetadata, RawFabricMetadata> {
/**
* Constructs a new {@link FabricMetadataReader} instance.
*/
constructor() {
super(FABRIC_MOD_JSON, FabricMetadata.from, JSON.parse);
}
}

View file

@ -0,0 +1,41 @@
import { zipFile } from "@/../tests/utils/zip-utils";
import mockFs from "mock-fs";
import { FabricMetadata } from "@/loaders/fabric/fabric-metadata";
import { FabricMetadataReader } from "@/loaders/fabric/fabric-metadata-reader";
beforeEach(async () => {
mockFs({
"fabric.mod.jar": await zipFile([__dirname, "../../../content/fabric/fabric.mod.json"]),
"text.txt": "",
});
});
afterEach(() => {
mockFs.restore();
});
describe("FabricMetadataReader", () => {
test("successfully reads fabric.mod.json", async () => {
const reader = new FabricMetadataReader();
const metadata = await reader.readMetadataFile("fabric.mod.jar");
expect(metadata).toBeInstanceOf(FabricMetadata);
});
test("returns undefined if file is not a Fabric mod", async () => {
const reader = new FabricMetadataReader();
const metadata = await reader.readMetadataFile("text.txt");
expect(metadata).toBeUndefined();
});
test("returns undefined if file does not exist", async () => {
const reader = new FabricMetadataReader();
const metadata = await reader.readMetadataFile("text.json");
expect(metadata).toBeUndefined();
});
});