Skip to content
This repository was archived by the owner on Jun 19, 2025. It is now read-only.

Commit a235532

Browse files
committed
test: split for readability and use mkdtemp & os.tmpdir
1 parent 404eaa3 commit a235532

File tree

4 files changed

+117
-78
lines changed

4 files changed

+117
-78
lines changed

test/functions/download.spec.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Import Node.js Dependencies
2+
import path from "node:path";
3+
import fs from "node:fs";
4+
import assert from "node:assert";
5+
import { describe, test } from "node:test";
6+
7+
// Import Third-party Dependencies
8+
import is from "@slimio/is";
9+
10+
// Import Internal Dependency
11+
import * as gitlab from "../../src/index.js";
12+
13+
describe("download", () => {
14+
test("should be an asyncFunction", () => {
15+
assert.ok(is.func(gitlab.download));
16+
assert.ok(is.asyncFunction(gitlab.download));
17+
});
18+
19+
test("must throw: repository must be a string!", async() => {
20+
await assert.rejects(
21+
async() => await gitlab.download(10 as any),
22+
{
23+
name: "TypeError",
24+
message: "repository must be a string!"
25+
}
26+
);
27+
});
28+
29+
test("extract tar.gz at in the current working dir", async() => {
30+
try {
31+
const { location, ...resultRest } = await gitlab.download("polychromatic.plombier-chauffagiste");
32+
33+
fs.accessSync(location);
34+
35+
assert.strictEqual(path.extname(location), ".gz");
36+
assert.deepEqual(resultRest, {
37+
branch: "master",
38+
organization: "polychromatic",
39+
repository: "plombier-chauffagiste"
40+
});
41+
}
42+
finally {
43+
const tarGzLocation = path.join(process.cwd(), "plombier-chauffagiste-master.tar.gz");
44+
if (fs.existsSync(tarGzLocation)) {
45+
fs.unlinkSync(tarGzLocation);
46+
}
47+
}
48+
});
49+
});
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Import Node.js Dependencies
2+
import os from "node:os";
3+
import path from "node:path";
4+
import fs from "node:fs";
5+
import assert from "node:assert";
6+
import { describe, test, it, beforeEach, afterEach } from "node:test";
7+
8+
// Import Third-party Dependencies
9+
import is from "@slimio/is";
10+
11+
// Import Internal Dependency
12+
import * as gitlab from "../../src/index.js";
13+
14+
describe("downloadAndExtract", () => {
15+
let tempDownloadDir: string;
16+
17+
beforeEach(() => {
18+
tempDownloadDir = fs.mkdtempSync(
19+
path.join(os.tmpdir(), "nsecure-gitlab-")
20+
);
21+
});
22+
23+
afterEach(() => {
24+
fs.rmSync(tempDownloadDir, { recursive: true, force: true });
25+
});
26+
27+
test("gitlab.downloadAndExtract should be an asyncFunction", () => {
28+
assert.ok(is.func(gitlab.downloadAndExtract));
29+
assert.ok(is.asyncFunction(gitlab.downloadAndExtract));
30+
});
31+
32+
it("should download and extract the given gitlab repository (and not delete the tar.gz archive)", async() => {
33+
const { location } = await gitlab.downloadAndExtract("polychromatic.plombier-chauffagiste", {
34+
dest: tempDownloadDir,
35+
removeArchive: false
36+
});
37+
38+
assert.ok(
39+
fs.existsSync(path.join(tempDownloadDir, "plombier-chauffagiste-master.tar.gz"))
40+
);
41+
assert.doesNotThrow(
42+
() => fs.accessSync(location)
43+
);
44+
});
45+
46+
it("should download and extract the given gitlab repository (and delete the tar.gz archive)", async() => {
47+
const { location } = await gitlab.downloadAndExtract("polychromatic.plombier-chauffagiste", {
48+
dest: tempDownloadDir,
49+
removeArchive: true
50+
});
51+
52+
assert.strictEqual(
53+
fs.existsSync(path.join(tempDownloadDir, "plombier-chauffagiste-master.tar.gz")),
54+
false
55+
);
56+
assert.doesNotThrow(
57+
() => fs.accessSync(location)
58+
);
59+
});
60+
});

test/test.spec.ts

Lines changed: 0 additions & 72 deletions
This file was deleted.

test/utils.spec.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
// Import Node.js Dependencies
2-
import { test } from "node:test";
2+
import { describe, test } from "node:test";
33
import assert from "node:assert";
44

55
// Import Internal Dependency
66
import { getRepositoryPath } from "../src/utils.js";
77

8-
test("getRepositoryPath must return id", () => {
9-
assert.equal(getRepositoryPath("10"), "10");
10-
});
8+
describe("getRepositoryPath", () => {
9+
test("must return id", () => {
10+
assert.strictEqual(getRepositoryPath("10"), "10");
11+
});
1112

12-
test("getRepositoryPath must return gitlab path", () => {
13-
assert.equal(getRepositoryPath("nodesecure.boo"), "nodesecure%2Fboo");
13+
test("must return GitLab path (encoded for URL)", () => {
14+
assert.strictEqual(getRepositoryPath("nodesecure.boo"), "nodesecure%2Fboo");
15+
});
1416
});

0 commit comments

Comments
 (0)