Add testing script and vitest as a dev dependency in package.json
This commit is contained in:
117
__tests__/index.test.js
Normal file
117
__tests__/index.test.js
Normal file
@@ -0,0 +1,117 @@
|
||||
const { validateAuthentication, replacePlaceholders } = require("../index");
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const os = require("os");
|
||||
const { glob } = require("glob");
|
||||
|
||||
describe("validateAuthentication", () => {
|
||||
it("should return null when only token is provided", () => {
|
||||
expect(validateAuthentication("token", null, null)).toBeNull();
|
||||
});
|
||||
|
||||
it("should return an error message when token and username are provided", () => {
|
||||
expect(validateAuthentication("token", "user", null)).toBe(
|
||||
"Token is defined along with username and/or password.",
|
||||
);
|
||||
});
|
||||
|
||||
it("should return an error message when token and password are provided", () => {
|
||||
expect(validateAuthentication("token", null, "pass")).toBe(
|
||||
"Token is defined along with username and/or password.",
|
||||
);
|
||||
});
|
||||
|
||||
it("should return an error message when token, username, and password are provided", () => {
|
||||
expect(validateAuthentication("token", "user", "pass")).toBe(
|
||||
"Token is defined along with username and/or password.",
|
||||
);
|
||||
});
|
||||
|
||||
it("should return an error message when no credentials are provided", () => {
|
||||
expect(validateAuthentication(null, null, null)).toBe(
|
||||
"Neither token nor password and username are defined.",
|
||||
);
|
||||
});
|
||||
|
||||
it("should return an error message when only username is provided", () => {
|
||||
expect(validateAuthentication(null, "user", null)).toBe(
|
||||
"Username is defined but no password is provided.",
|
||||
);
|
||||
});
|
||||
|
||||
it("should return an error message when only password is provided", () => {
|
||||
expect(validateAuthentication(null, null, "pass")).toBe(
|
||||
"Password is defined but no username is provided.",
|
||||
);
|
||||
});
|
||||
|
||||
it("should return null when username and password are provided", () => {
|
||||
expect(validateAuthentication(null, "user", "pass")).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("replacePlaceholders", () => {
|
||||
beforeEach(() => {
|
||||
process.env.GITHUB_SHA = "test-sha";
|
||||
process.env.GITHUB_REF = "test-ref";
|
||||
});
|
||||
|
||||
it("should replace all placeholders", () => {
|
||||
const content =
|
||||
"hash: {{gitHash}}, ref: {{gitRef}}, time: {{deployTime}}, host: {{hostname}}";
|
||||
const replacedContent = replacePlaceholders(content, "test-host");
|
||||
expect(replacedContent).toMatch(/hash: test-sha/);
|
||||
expect(replacedContent).toMatch(/ref: test-ref/);
|
||||
expect(replacedContent).toMatch(/time: .*/);
|
||||
expect(replacedContent).toMatch(/host: test-host/);
|
||||
});
|
||||
});
|
||||
|
||||
describe("glob functionality", () => {
|
||||
let tempDir;
|
||||
|
||||
beforeEach(async () => {
|
||||
tempDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "glob-test-"));
|
||||
await fs.promises.mkdir(path.join(tempDir, "lib"), { recursive: true });
|
||||
await fs.promises.mkdir(path.join(tempDir, "deep", "folder"), {
|
||||
recursive: true,
|
||||
});
|
||||
await fs.promises.writeFile(path.join(tempDir, "main.js"), "content");
|
||||
await fs.promises.writeFile(path.join(tempDir, "utils.js"), "content");
|
||||
await fs.promises.writeFile(
|
||||
path.join(tempDir, "lib", "helper.js"),
|
||||
"content",
|
||||
);
|
||||
await fs.promises.writeFile(
|
||||
path.join(tempDir, "lib", "data.json"),
|
||||
"content",
|
||||
);
|
||||
await fs.promises.writeFile(
|
||||
path.join(tempDir, "deep", "folder", "main.js"),
|
||||
"content",
|
||||
);
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
if (tempDir) {
|
||||
await fs.promises.rm(tempDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it("should find all javascript files in the directory", async () => {
|
||||
// Ensure pattern uses forward slashes for glob
|
||||
const pattern = path.join(tempDir, "**", "*.js").split(path.sep).join("/");
|
||||
const files = await glob(pattern);
|
||||
|
||||
// Normalize file paths to system separator (backslashes on Windows)
|
||||
const normalizedFiles = files.map((f) => path.normalize(f));
|
||||
|
||||
const expectedFiles = [
|
||||
path.join(tempDir, "deep", "folder", "main.js"),
|
||||
path.join(tempDir, "lib", "helper.js"),
|
||||
path.join(tempDir, "main.js"),
|
||||
path.join(tempDir, "utils.js"),
|
||||
].sort();
|
||||
expect(normalizedFiles.sort()).toEqual(expectedFiles);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user