keycloak_theme/test/bin/jar.spec.ts

126 lines
4.6 KiB
TypeScript
Raw Normal View History

2023-04-06 22:02:45 +02:00
import jar, { jarStream, type ZipEntryGenerator } from "keycloakify/bin/tools/jar";
import { fromBuffer, Entry, ZipFile } from "yauzl";
2023-04-06 22:02:45 +02:00
import { it, describe, assert, afterAll } from "vitest";
import { Readable } from "stream";
2023-04-06 22:02:45 +02:00
import { tmpdir } from "os";
import { mkdtemp, cp, mkdir, rm } from "fs/promises";
import path from "path";
import { createReadStream } from "fs";
import walk from "keycloakify/bin/tools/walk";
type AsyncIterable<T> = {
[Symbol.asyncIterator](): AsyncIterableIterator<T>;
2023-04-02 22:49:12 +02:00
};
async function arrayFromAsync<T>(asyncIterable: AsyncIterable<T>) {
2023-04-02 22:49:12 +02:00
const chunks: T[] = [];
for await (const chunk of asyncIterable) chunks.push(chunk);
return chunks;
}
async function readToBuffer(stream: NodeJS.ReadableStream) {
2023-04-02 22:49:12 +02:00
return Buffer.concat(await arrayFromAsync(stream as AsyncIterable<Buffer>));
}
2023-04-06 22:02:45 +02:00
function unzipBuffer(buffer: Buffer) {
return new Promise<ZipFile>((resolve, reject) =>
fromBuffer(buffer, { lazyEntries: true }, (err, zipFile) => {
2023-04-02 22:49:12 +02:00
if (err !== null) {
reject(err);
} else {
resolve(zipFile);
}
})
);
}
function readEntry(zipFile: ZipFile, entry: Entry): Promise<Readable> {
return new Promise<Readable>((resolve, reject) => {
zipFile.openReadStream(entry, (err, stream) => {
2023-04-02 22:49:12 +02:00
if (err !== null) {
reject(err);
} else {
resolve(stream);
}
});
});
}
function readAll(zipFile: ZipFile): Promise<Map<string, Buffer>> {
return new Promise<Map<string, Buffer>>((resolve, reject) => {
2023-04-02 22:49:12 +02:00
const entries1: Map<string, Buffer> = new Map();
zipFile.on("entry", async (entry: Entry) => {
2023-04-02 22:49:12 +02:00
const stream = await readEntry(zipFile, entry);
const buffer = await readToBuffer(stream);
entries1.set(entry.fileName, buffer);
zipFile.readEntry();
});
zipFile.on("end", () => resolve(entries1));
zipFile.on("error", e => reject(e));
zipFile.readEntry();
});
}
2023-03-30 02:46:44 -06:00
describe("jar", () => {
2023-04-06 22:06:14 +02:00
const coords = { artifactId: "someArtifactId", groupId: "someGroupId", version: "1.2.3" };
2023-04-06 22:02:45 +02:00
2023-04-06 22:06:14 +02:00
const tmpDirs: string[] = [];
2023-04-06 22:02:45 +02:00
// afterAll(async () => {
// await Promise.all(tmpDirs.map(dir => rm(dir, { force: true, recursive: true })));
// });
it("creates jar artifacts without error", async () => {
async function* mockFiles(): ZipEntryGenerator {
2023-04-02 22:49:12 +02:00
yield { zipPath: "foo", buffer: Buffer.from("foo") };
}
2023-04-06 22:02:45 +02:00
const zipped = await jarStream({ ...coords, asyncPathGeneratorFn: mockFiles });
2023-04-02 22:49:12 +02:00
const buffered = await readToBuffer(zipped.outputStream);
2023-04-06 22:02:45 +02:00
const unzipped = await unzipBuffer(buffered);
2023-04-02 22:49:12 +02:00
const entries = await readAll(unzipped);
2023-04-02 22:49:12 +02:00
assert.equal(entries.size, 3);
assert.isOk(entries.has("foo"));
assert.isOk(entries.has("META-INF/MANIFEST.MF"));
assert.isOk(entries.has("META-INF/maven/someGroupId/someArtifactId/pom.properties"));
2023-04-02 22:49:12 +02:00
assert.equal("foo", entries.get("foo")?.toString("utf-8"));
2023-04-02 22:49:12 +02:00
const manifest = entries.get("META-INF/MANIFEST.MF")?.toString("utf-8");
const pomProperties = entries.get("META-INF/maven/someGroupId/someArtifactId/pom.properties")?.toString("utf-8");
2023-04-02 22:49:12 +02:00
assert.isOk(manifest?.includes("Created-By: Keycloakify"));
assert.isOk(pomProperties?.includes("1.2.3"));
assert.isOk(pomProperties?.includes("someGroupId"));
assert.isOk(pomProperties?.includes("someArtifactId"));
2023-03-30 02:46:44 -06:00
});
2023-04-06 22:02:45 +02:00
it("creates a jar from _real_ files without error", async () => {
2023-04-06 22:06:14 +02:00
const tmp = await mkdtemp(path.join(tmpdir(), "kc-jar-test-"));
tmpDirs.push(tmp);
const rootPath = path.join(tmp, "src");
const targetPath = path.join(tmp, "jar.jar");
await mkdir(rootPath);
2023-04-06 22:02:45 +02:00
2023-04-06 22:06:14 +02:00
await cp(path.dirname(__dirname), rootPath, { recursive: true });
2023-04-06 22:02:45 +02:00
2023-04-06 22:06:14 +02:00
await jar({ ...coords, rootPath, targetPath });
2023-04-06 22:02:45 +02:00
const buffered = await readToBuffer(createReadStream(targetPath));
const unzipped = await unzipBuffer(buffered);
const entries = await readAll(unzipped);
2023-04-06 22:06:14 +02:00
const zipPaths = Array.from(entries.keys());
2023-04-06 22:02:45 +02:00
assert.isOk(entries.has("META-INF/MANIFEST.MF"));
assert.isOk(entries.has("META-INF/maven/someGroupId/someArtifactId/pom.properties"));
for await (const fsPath of walk(rootPath)) {
if (!fsPath.endsWith(path.sep)) {
2023-04-06 22:06:14 +02:00
const rel = path.relative(rootPath, fsPath).replace(path.sep === "/" ? /\//g : /\\/g, "/");
assert.isOk(zipPaths.includes(rel), `missing ${rel} (${rel}, ${zipPaths.join(", ")})`);
2023-04-06 22:02:45 +02:00
}
}
2023-04-06 22:06:14 +02:00
});
2023-03-30 02:46:44 -06:00
});