From 77f0d92e1e019894bff7a4b376c118cd95118d20 Mon Sep 17 00:00:00 2001 From: Minionguyjpro Date: Tue, 2 Jan 2024 21:02:00 +0100 Subject: [PATCH] Update main.test.js --- __tests__/main.test.js | 43 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/__tests__/main.test.js b/__tests__/main.test.js index 9a732a8..7af667d 100644 --- a/__tests__/main.test.js +++ b/__tests__/main.test.js @@ -1,15 +1,18 @@ const core = require("@actions/core"); +const fs = require("fs"); +const childProcess = require("child_process"); // Mocking specific methods of @actions/core core.getInput = jest.fn().mockReturnValueOnce("options").mockReturnValueOnce("path"); core.setFailed = jest.fn(); +// Mocking fs and child_process modules jest.mock("fs", () => ({ existsSync: jest.fn(() => true), readdirSync: jest.fn(() => ['file1', 'file2']), })); -jest.mock('child_process', () => ({ +jest.mock("child_process", () => ({ exec: jest.fn((command, options, callback) => { // Simulate a successful execution callback(null, 'stdout', 'stderr'); @@ -19,4 +22,40 @@ jest.mock('child_process', () => ({ // Importing the main module after the mocks are set const main = require("../src/main"); -// Add your test cases here +describe("Inno Setup Action", () => { + it("should execute Inno Setup command on Windows with existing workspace", () => { + process.platform = "win32"; + main(); + expect(core.getInput).toHaveBeenCalledTimes(2); + expect(fs.existsSync).toHaveBeenCalled(); + expect(childProcess.exec).toHaveBeenCalledWith( + expect.stringContaining("iscc.exe"), + expect.any(Object), + expect.any(Function) + ); + expect(core.setFailed).not.toHaveBeenCalled(); + }); + + it("should set failed if workspace doesn't exist", () => { + process.platform = "win32"; + fs.existsSync.mockReturnValueOnce(false); + main(); + expect(core.getInput).toHaveBeenCalledTimes(2); + expect(fs.existsSync).toHaveBeenCalled(); + expect(childProcess.exec).not.toHaveBeenCalled(); + expect(core.setFailed).toHaveBeenCalledWith( + "The repository was not cloned. Please specify the actions/checkout action before this step." + ); + }); + + it("should set failed if platform is not Windows", () => { + process.platform = "linux"; + main(); + expect(core.getInput).toHaveBeenCalledTimes(2); + expect(fs.existsSync).not.toHaveBeenCalled(); + expect(childProcess.exec).not.toHaveBeenCalled(); + expect(core.setFailed).toHaveBeenCalledWith("This action is only supported on Windows!"); + }); + + // Add more test cases as needed +});