From 8f66bdc2b807a2a5bd3fe6bff2d1be98d090bb2f Mon Sep 17 00:00:00 2001 From: Minionguyjpro Date: Tue, 2 Jan 2024 21:32:12 +0100 Subject: [PATCH] Update main.test.js --- __tests__/main.test.js | 76 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 67 insertions(+), 9 deletions(-) diff --git a/__tests__/main.test.js b/__tests__/main.test.js index 4dcf313..4e690e6 100644 --- a/__tests__/main.test.js +++ b/__tests__/main.test.js @@ -1,15 +1,73 @@ // main.test.js -const core = require("@actions/core"); -const fs = require("fs"); -const { main } = require("../src/main"); // Adjust the path accordingly +const core = require('@actions/core'); +const fs = require('fs'); +const main = require('../src/main'); // Update the path accordingly +const child_process = require('child_process'); -jest.mock("@actions/core"); +jest.mock('@actions/core'); +jest.mock('fs'); +jest.mock('child_process'); + +describe('Inno Setup Action', () => { + beforeEach(() => { + jest.resetAllMocks(); + }); + + it('should execute Inno Setup command on Windows with existing workspace', () => { + process.platform = 'win32'; + process.env.GITHUB_WORKSPACE = '/path/to/workspace'; + core.getInput.mockReturnValueOnce('options-value').mockReturnValueOnce('path-value'); + fs.existsSync.mockReturnValueOnce(true); + fs.readdirSync.mockReturnValueOnce(['file1', 'file2']); -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(core.getInput).toHaveBeenCalledWith('options'); + expect(core.getInput).toHaveBeenCalledWith('path'); + expect(fs.existsSync).toHaveBeenCalledWith('/path/to/workspace'); + expect(fs.readdirSync).toHaveBeenCalledWith('/path/to/workspace'); + expect(child_process.exec).toHaveBeenCalledWith( + '"%PROGRAMFILES(X86)%\\Inno Setup 6\\iscc.exe" options-value "/path/to/workspace\\path-value"', + { stdio: 'ignore' }, + expect.any(Function) + ); + }); + + it('should handle error when executing Inno Setup command', () => { + process.platform = 'win32'; + process.env.GITHUB_WORKSPACE = '/path/to/workspace'; + core.getInput.mockReturnValueOnce('options-value').mockReturnValueOnce('path-value'); + fs.existsSync.mockReturnValueOnce(true); + fs.readdirSync.mockReturnValueOnce(['file1', 'file2']); + const execError = new Error('Command execution failed'); + child_process.exec.mockImplementationOnce((command, options, callback) => { + callback(execError, 'stdout-content', 'stderr-content'); + }); + + main(); + + expect(core.setFailed).toHaveBeenCalledWith('stderr-content'); + expect(process.exit).toHaveBeenCalledWith(execError.code || 1); + }); + + it('should handle repository not cloned error', () => { + process.platform = 'win32'; + process.env.GITHUB_WORKSPACE = '/path/to/workspace'; + core.getInput.mockReturnValueOnce('options-value').mockReturnValueOnce('path-value'); + fs.existsSync.mockReturnValueOnce(false); + + main(); + + expect(core.setFailed).toHaveBeenCalledWith('The repository was not cloned. Please specify the actions/checkout action before this step.'); + expect(process.exit).toHaveBeenCalledWith(1); + }); + + it('should handle unsupported platform error', () => { + process.platform = 'linux'; + + main(); + + expect(core.setFailed).toHaveBeenCalledWith('This action is only supported on Windows!'); + expect(process.exit).toHaveBeenCalledWith(1); }); });