From 9d6d2ce8cf34af6e787dae901ee3e5914c4c0fa4 Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 29 Jul 2021 15:54:04 +0200 Subject: Added tests & API tests uses Axios Added a unit test for creating a archive & added a int test for downloading a tag tarball --- test/unit/tree.unit.test.ts | 60 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 2 deletions(-) (limited to 'test/unit/tree.unit.test.ts') diff --git a/test/unit/tree.unit.test.ts b/test/unit/tree.unit.test.ts index 47a716d..d34c93e 100644 --- a/test/unit/tree.unit.test.ts +++ b/test/unit/tree.unit.test.ts @@ -1,8 +1,9 @@ import { Repository } from "server/src/git/repository"; import { Tree } from "server/src/git/tree"; -import { TreeEntry } from "server/src/git/tree_entry"; +import { BaseTreeEntry, BlobTreeEntry, TreeEntry } from "server/src/git/tree_entry"; import { BaseError } from "server/src/git/error"; import { EnvironmentVariables } from "../util"; +import { extract, Headers } from "tar-stream"; const env = process.env as EnvironmentVariables; @@ -34,7 +35,7 @@ describe("Tree", () => { for(const entry of entries) { expect(entry).toBeDefined(); - expect(entry).toBeInstanceOf(TreeEntry); + expect(entry).toBeInstanceOf(BaseTreeEntry); } }); @@ -64,5 +65,60 @@ describe("Tree", () => { await expect(tree.findExists("packages/core/main.js")).resolves.toBeFalsy(); }); + + it("Should create an archive", async() => { + const archive = await tree.createArchive(); + + expect(archive).toBeDefined(); + + const extract_archive = extract(); + + archive.pipe(extract_archive); + + type Entry = { + header: Headers, + content: Buffer[] + }; + + // Extract the archive entries to an array of entries + const entries = await new Promise((resolve: (value: Entry[]) => void) => { + const entries = []; + + extract_archive.on("finish", () => { + resolve(entries); + }); + + extract_archive.on("entry", (header, stream, next) => { + const content: Buffer[] = []; + + stream.on("data", (chunk: Buffer) => { + content.push(chunk); + }); + + stream.on("end", async() => { + entries.push({ header, content }); + next(); + }); + + stream.resume(); + }); + }); + + expect(entries).toBeDefined(); + expect(entries.length).toBeGreaterThan(1); + + for(const entry of entries) { + expect(entry).toBeDefined(); + expect(entry.content).toBeDefined(); + + const content = entry.content.join().toString(); + + // Get the file content directly + const tree_entry = await tree.find(entry.header.name.split("/").slice(1).join("/")) as BlobTreeEntry; + const file_content = await (await tree_entry.blob()).content(); + + expect(content).toEqual(file_content); + } + }); }); }); \ No newline at end of file -- cgit v1.2.3-18-g5258