Skip to content

Commit

Permalink
Merge branch 'main' into 1584_complex_filternomatchreason
Browse files Browse the repository at this point in the history
  • Loading branch information
btlghrants committed Dec 17, 2024
2 parents a76efa3 + cd255d5 commit 546d09d
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 56 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:

- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

- uses: docker/setup-buildx-action@c47758b77c9736f4b2ef4073d4d51994fabfe349 # v3.7.1
- uses: docker/setup-buildx-action@6524bf65af31da8d45b59e8c27de4bd072b392f5 # v3.8.0

- name: Use Node.js 20
uses: actions/setup-node@39370e3970a6d050c480ffad4ff0ed4d3fdee5af # v4.1.0
Expand Down
92 changes: 40 additions & 52 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"sigstore": "3.0.0"
},
"devDependencies": {
"@commitlint/cli": "19.6.0",
"@commitlint/cli": "19.6.1",
"@commitlint/config-conventional": "19.6.0",
"@fast-check/jest": "^2.0.1",
"@jest/globals": "29.7.0",
Expand Down
67 changes: 65 additions & 2 deletions src/cli/init/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023-Present The Pepr Authors

import { expect, it } from "@jest/globals";
import { expect, it, jest, describe, afterEach } from "@jest/globals";
import { promises as fs } from "fs";
import { sanitizeName, createDir, write } from "./utils";

import { sanitizeName } from "./utils";
jest.mock("fs", () => ({
promises: {
mkdir: jest.fn(),
writeFile: jest.fn(),
},
}));

it.each([
//Test sanitizeName() with ["$BAD_INPUT", "$SANITIZED_INPUT"]
Expand All @@ -17,3 +24,59 @@ it.each([
it("sanitizeName() should throw TypeError when given a non-string", () => {
expect(() => sanitizeName({ input: 0 } as unknown as string)).toThrow(TypeError);
});

describe("createDir", () => {
const mockedMkdir = fs.mkdir as jest.Mock;

afterEach(() => {
jest.clearAllMocks(); // Clear mocks between tests
});

it("should create the pepr-test-module dir", async () => {
await createDir("pepr-test-module");
expect(mockedMkdir).toHaveBeenCalledWith("pepr-test-module");
});

it("should throw an error if the directory already exists", async () => {
const error = new Error("Directory already exists") as NodeJS.ErrnoException;
error.code = "EEXIST";
mockedMkdir.mockRejectedValueOnce(error as unknown as never);

await expect(createDir("pepr-test-module")).rejects.toThrow(
"Directory pepr-test-module already exists",
);
expect(mockedMkdir).toHaveBeenCalledTimes(1);
expect(mockedMkdir).toHaveBeenCalledWith("pepr-test-module");
});

it("should throw the error if there is another error other than already exists", async () => {
const error = new Error("Directory already exists") as NodeJS.ErrnoException;
error.code = "ENOENT";
mockedMkdir.mockRejectedValueOnce(error as unknown as never);

await expect(createDir("pepr-test-module")).rejects.toThrow(error);
expect(mockedMkdir).toHaveBeenCalledTimes(1);
expect(mockedMkdir).toHaveBeenCalledWith("pepr-test-module");
});
});

describe("write", () => {
const mockedWriteFile = fs.writeFile as jest.Mock;

afterEach(() => {
jest.clearAllMocks();
});

it("should write data to a file", async () => {
await write("package.json", "pepr-test-module");
expect(mockedWriteFile).toHaveBeenCalledWith("package.json", "pepr-test-module");
});

it("should stringify data if it is not a string", async () => {
await write("package.json", { name: "pepr-test-module" });
expect(mockedWriteFile).toHaveBeenCalledWith(
"package.json",
JSON.stringify({ name: "pepr-test-module" }, null, 2),
);
});
});

0 comments on commit 546d09d

Please sign in to comment.