From ef12f606bcc3b8f321df59e003b038409253e2a1 Mon Sep 17 00:00:00 2001 From: Minionguyjpro Date: Tue, 2 Jan 2024 20:26:27 +0100 Subject: [PATCH] Update again... --- .github/workflows/test.yml | 2 +- __tests__/main.test.js | 70 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 __tests__/main.test.js diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 0f82f89..408d953 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -9,7 +9,7 @@ on: jobs: build: - runs-on: ubuntu-latest + runs-on: windows-latest steps: - uses: actions/checkout@v4 with: diff --git a/__tests__/main.test.js b/__tests__/main.test.js new file mode 100644 index 0000000..ccdff59 --- /dev/null +++ b/__tests__/main.test.js @@ -0,0 +1,70 @@ +// main.test.js + +const fs = require('fs'); +const childProcess = require('child_process'); +const { testFunction } = require('../src/main'); + +jest.mock('@actions/core', () => ({ + getInput: jest.fn(), + setFailed: jest.fn(), +})); + +jest.mock('fs', () => ({ + existsSync: jest.fn(), + readdirSync: jest.fn(), +})); + +jest.mock('child_process', () => ({ + exec: jest.fn(), +})); + +describe('Test suite for index.js', () => { + beforeEach(() => { + // Clear mock calls before each test + jest.clearAllMocks(); + }); + + test('Handles Windows platform with existing repository', () => { + process.platform = 'win32'; + fs.existsSync.mockReturnValue(true); + fs.readdirSync.mockReturnValue(['file1', 'file2']); + childProcess.exec.mockImplementation((command, options, callback) => { + // Simulate successful execution + callback(null, 'stdout content', 'stderr content'); + }); + + // Call the function you want to test + testFunction(); + + // Assertions + expect(fs.existsSync).toHaveBeenCalledWith(process.env.GITHUB_WORKSPACE); + expect(fs.readdirSync).toHaveBeenCalledWith(process.env.GITHUB_WORKSPACE); + expect(childProcess.exec).toHaveBeenCalled(); + expect(core.setFailed).not.toHaveBeenCalled(); + }); + + test('Handles Windows platform with missing repository', () => { + process.platform = 'win32'; + fs.existsSync.mockReturnValue(false); + + // Call the function you want to test + testFunction(); + + // Assertions + expect(core.setFailed).toHaveBeenCalledWith( + 'The repository was not cloned. Please specify the actions/checkout action before this step.' + ); + expect(process.exit).toHaveBeenCalledWith(1); + }); + + test('Handles non-Windows platform', () => { + process.platform = 'linux'; + + // Call the function you want to test + testFunction(); + + // Assertions + expect(core.setFailed).toHaveBeenCalledWith('This action is only supported on Windows!'); + expect(process.exit).toHaveBeenCalledWith(1); + }); +});