From cbdb9473caf9b4f036ceb411560f59cf38cb7315 Mon Sep 17 00:00:00 2001 From: Case Wylie Date: Tue, 17 Dec 2024 15:27:57 -0500 Subject: [PATCH] chore: statements in format Signed-off-by: Case Wylie --- src/cli/format.helpers.ts | 27 ++++++++++++++ src/cli/format.test.ts | 75 +++++++++++++++++++++++++++++++++++++++ src/cli/format.ts | 18 ++-------- 3 files changed, 104 insertions(+), 16 deletions(-) create mode 100644 src/cli/format.helpers.ts create mode 100644 src/cli/format.test.ts diff --git a/src/cli/format.helpers.ts b/src/cli/format.helpers.ts new file mode 100644 index 000000000..1b2c4bae6 --- /dev/null +++ b/src/cli/format.helpers.ts @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023-Present The Pepr Authors + +import { ESLint } from "eslint"; +import { promises as fs } from "fs"; +import { format, resolveConfig } from "prettier"; + +export async function formatWithPrettier( + results: ESLint.LintResult[], + validateOnly: boolean, +): Promise { + let hasFailure = false; + for (const { filePath } of results) { + const content = await fs.readFile(filePath, "utf8"); + const cfg = await resolveConfig(filePath); + const formatted = await format(content, { filepath: filePath, ...cfg }); + + // If in validate-only mode, check if the file is formatted correctly + if (validateOnly && formatted !== content) { + hasFailure = true; + console.error(`File ${filePath} is not formatted correctly`); + } else { + await fs.writeFile(filePath, formatted); + } + } + return hasFailure; +} diff --git a/src/cli/format.test.ts b/src/cli/format.test.ts new file mode 100644 index 000000000..6c9edfed9 --- /dev/null +++ b/src/cli/format.test.ts @@ -0,0 +1,75 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023-Present The Pepr Authors + +import { jest, describe, it, expect, beforeEach } from "@jest/globals"; +import { formatWithPrettier } from "./format.helpers"; +import { promises as fs } from "fs"; +import { resolveConfig, format } from "prettier"; +import { ESLint } from "eslint"; + +jest.mock("fs", () => ({ + promises: { + readFile: jest.fn(), + writeFile: jest.fn(), + }, +})); + +jest.mock("prettier", () => ({ + resolveConfig: jest.fn(), + format: jest.fn(), +})); + +describe("formatWithPrettier", () => { + const mockReadFile = fs.readFile as jest.MockedFunction; + const mockWriteFile = fs.writeFile as jest.MockedFunction; + const mockResolveConfig = resolveConfig as jest.MockedFunction; + const mockFormat = format as jest.MockedFunction; + + const mockResults: ESLint.LintResult[] = [ + { filePath: "package.json" } as ESLint.LintResult, + { filePath: "hello-pepr.ts" } as ESLint.LintResult, + ]; + + beforeEach(() => { + jest.clearAllMocks(); + }); + + it("should format files and write back to the file system", async () => { + mockReadFile.mockResolvedValue("const x =1;"); + mockResolveConfig.mockResolvedValue({ semi: true }); + mockFormat.mockResolvedValue("const x = 1;"); + + const validateOnly = false; + + const result = await formatWithPrettier(mockResults, validateOnly); + + expect(result).toBe(false); + expect(mockReadFile).toHaveBeenCalledTimes(2); + expect(mockWriteFile).toHaveBeenCalledTimes(2); + expect(mockFormat).toHaveBeenCalledTimes(2); + + expect(mockWriteFile).toHaveBeenCalledWith("package.json", "const x = 1;"); + expect(mockWriteFile).toHaveBeenCalledWith("hello-pepr.ts", "const x = 1;"); + }); + + it("should report failures in validate-only mode if files are not formatted", async () => { + mockReadFile.mockResolvedValue("const x =1;"); + mockResolveConfig.mockResolvedValue({ semi: true }); + mockFormat.mockResolvedValue("const x = 1;"); + + const validateOnly = true; + + const mockError = jest.spyOn(console, "error").mockImplementation(() => { + return undefined as never; + }); + const result = await formatWithPrettier(mockResults, validateOnly); + + expect(result).toBe(true); + expect(mockReadFile).toHaveBeenCalledTimes(2); + expect(mockWriteFile).not.toHaveBeenCalled(); + expect(mockError).toHaveBeenCalledWith("File package.json is not formatted correctly"); + expect(mockError).toHaveBeenCalledWith("File hello-pepr.ts is not formatted correctly"); + + mockError.mockRestore(); + }); +}); diff --git a/src/cli/format.ts b/src/cli/format.ts index 268921159..8e6f5da47 100644 --- a/src/cli/format.ts +++ b/src/cli/format.ts @@ -2,8 +2,7 @@ // SPDX-FileCopyrightText: 2023-Present The Pepr Authors import { ESLint } from "eslint"; -import { promises as fs } from "fs"; -import { format, resolveConfig } from "prettier"; +import { formatWithPrettier } from "./format.helpers"; import { RootCmd } from "./root"; @@ -56,20 +55,7 @@ export async function peprFormat(validateOnly: boolean) { await ESLint.outputFixes(results); } - // Format with Prettier - for (const { filePath } of results) { - const content = await fs.readFile(filePath, "utf8"); - const cfg = await resolveConfig(filePath); - const formatted = await format(content, { filepath: filePath, ...cfg }); - - // If in validate-only mode, check if the file is formatted correctly - if (validateOnly && formatted !== content) { - hasFailure = true; - console.error(`File ${filePath} is not formatted correctly`); - } else { - await fs.writeFile(filePath, formatted); - } - } + hasFailure = await formatWithPrettier(results, validateOnly); return !hasFailure; } catch (e) {