Skip to content

Commit

Permalink
refactor: code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
brenoepics committed May 16, 2024
1 parent ad6bc9c commit 3b8818c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 54 deletions.
70 changes: 23 additions & 47 deletions __tests__/utils/FileProcessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,16 @@ jest.mock("simple-git");
const mockedCore = core as jest.Mocked<typeof core>;
const mockedSimpleGit = simpleGit as jest.MockedFunction<typeof simpleGit>;
let gitMock: jest.Mocked<SimpleGit>;

let params: FilesParams;
describe("FileProcessor", () => {
beforeEach(() => {
jest.clearAllMocks();
gitMock = {
add: jest.fn().mockImplementation(() => Promise.resolve()),
addConfig: jest.fn().mockImplementation(() => gitMock),
commit: jest.fn().mockImplementation(() => Promise.resolve()),
push: jest.fn().mockImplementation(() => Promise.resolve())
} as unknown as jest.Mocked<SimpleGit>;
mockedSimpleGit.mockReturnValue(gitMock);
params = {
files: ["test.txt"],
variables: new Map([["name", "Breno"]]),
encodings: "utf-8",
extension: ".txt"
}
});
describe("processFiles", () => {
it("processes files correctly", () => {
Expand All @@ -34,31 +33,14 @@ describe("FileProcessor", () => {
mockReadFileSync.mockReturnValue("Hello, {_name_}!");
mockWriteFileSync.mockImplementation(() => {
});

const params: FilesParams = {
files: ["test.txt"],
variables: new Map([["name", "Breno"]]),
encodings: "utf-8",
extension: ".txt"
};

processFiles(params);

expect(mockReadFileSync).toHaveBeenCalledWith("test.txt", { encoding: "utf-8" });
expect(mockWriteFileSync).toHaveBeenCalledWith("test", "Hello, Breno!", { encoding: "utf-8" });
});

it("throws error when file cannot be read", async () => {
const mockReadFileSync = jest.spyOn(fs, "readFileSync");
mockReadFileSync.mockReturnValue("");

const params: FilesParams = {
files: ["test.txt"],
variables: new Map([["name", "Breno"]]),
encodings: "utf-8",
extension: ".txt"
};

await expect(processFiles(params)).rejects.toThrow("Error reading file: test.txt");
});
});
Expand All @@ -67,40 +49,43 @@ describe("FileProcessor", () => {
it("replaces variables correctly", () => {
const variables = new Map([["name", "Breno"]]);
const content = "Hello, {_name_}!";

const result = replaceVariables(variables, content);

expect(result).toBe("Hello, Breno!");
});

it("returns original content when no variables match", () => {
const variables = new Map([["name", "Breno"]]);
const content = "Hello, world!";

const result = replaceVariables(variables, content);

expect(result).toBe("Hello, world!");
});
});

describe("gitAdd", () => {
it("git add and commit", async () => {
const mockReadFileSync = jest.spyOn(fs, "readFileSync");
const mockWriteFileSync = jest.spyOn(fs, "writeFileSync");
mockReadFileSync.mockReturnValue("Hello, {_name_}!");
mockWriteFileSync.mockImplementation(() => {
});

const params: FilesParams = {
beforeEach(() => {
gitMock = {
add: jest.fn().mockImplementation(() => Promise.resolve()),
addConfig: jest.fn().mockImplementation(() => gitMock),
commit: jest.fn().mockImplementation(() => Promise.resolve()),
push: jest.fn().mockImplementation(() => Promise.resolve())
} as unknown as jest.Mocked<SimpleGit>;
mockedSimpleGit.mockReturnValue(gitMock);
params = {
files: ["test.prp.txt"],
variables: new Map([["name", "Breno"]]),
encodings: "utf-8",
extension: ".prp",
git: gitMock
};
});

it("git add and commit", async () => {
const mockReadFileSync = jest.spyOn(fs, "readFileSync");
const mockWriteFileSync = jest.spyOn(fs, "writeFileSync");
mockReadFileSync.mockReturnValue("Hello, {_name_}!");
mockWriteFileSync.mockImplementation(() => {
});
await processFiles(params);

expect(mockReadFileSync).toHaveBeenCalledWith("test.prp.txt", { encoding: "utf-8" });
expect(mockWriteFileSync).toHaveBeenCalledWith("test.txt", "Hello, Breno!", { encoding: "utf-8" });
expect(gitMock.add).toHaveBeenCalledWith("test.txt");
Expand All @@ -113,15 +98,6 @@ describe("FileProcessor", () => {
});

gitMock.add.mockRejectedValue(new GitError(undefined, "Error adding file"));

const params: FilesParams = {
files: ["test.prp.txt"],
variables: new Map([["name", "Breno"]]),
encodings: "utf-8",
extension: ".prp",
git: gitMock
};

await processFiles(params);

expect(mockReadFileSync).toHaveBeenCalledWith("test.prp.txt", { encoding: "utf-8" });
Expand Down
5 changes: 3 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as core from "@actions/core";
import { getFilesByExtension } from "./utils/ExtensionFilter";
import { InputParams } from "./utils/VariableManager";
import { SEARCH_TEXT } from "./utils/texts";
import { processFiles, replaceVariables } from "./utils/FileProcessor";
import path from "path";
import simpleGit, { GitError, SimpleGit } from "simple-git";
Expand All @@ -13,7 +12,9 @@ import simpleGit, { GitError, SimpleGit } from "simple-git";
*/
export async function run(inputParams: InputParams): Promise<void> {
const { rootDir, extension, ignoredDir, includeSubDir } = inputParams;
core.debug(SEARCH_TEXT(extension, rootDir));
core.debug(
`Searching for files with extension: ${extension} in directory: ${rootDir}...`
);
const files: string[] = getFilesByExtension({
dir: path.join(rootDir),
extension,
Expand Down
5 changes: 0 additions & 5 deletions src/utils/texts.ts

This file was deleted.

0 comments on commit 3b8818c

Please sign in to comment.