mirror of
https://github.com/Minionguyjpro/Inno-Setup-Action
synced 2026-02-18 17:41:18 +01:00
Update main.test.js
This commit is contained in:
@@ -1,71 +1,70 @@
|
|||||||
// main.test.js
|
// main.test.js
|
||||||
|
|
||||||
const core = require('@actions/core')
|
const core = require('@actions/core');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const childProcess = require('child_process');
|
const { exec } = require('child_process');
|
||||||
const { testFunction } = require('../src/main');
|
|
||||||
|
|
||||||
jest.mock('@actions/core', () => ({
|
// Mock the @actions/core module
|
||||||
getInput: jest.fn(),
|
jest.mock('@actions/core');
|
||||||
setFailed: jest.fn(),
|
|
||||||
}));
|
|
||||||
|
|
||||||
jest.mock('fs', () => ({
|
// ... other imports and setup ...
|
||||||
existsSync: jest.fn(),
|
|
||||||
readdirSync: jest.fn(),
|
|
||||||
}));
|
|
||||||
|
|
||||||
jest.mock('child_process', () => ({
|
describe('Test suite for main.js', () => {
|
||||||
exec: jest.fn(),
|
|
||||||
}));
|
|
||||||
|
|
||||||
describe('Test suite for index.js', () => {
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
// Clear mock calls before each test
|
// Clear mock calls before each test
|
||||||
jest.clearAllMocks();
|
jest.clearAllMocks();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Handles Windows platform with existing repository', () => {
|
it('should handle repository not cloned error', async () => {
|
||||||
process.platform = 'win32';
|
// Mock fs.existsSync to simulate repository not cloned scenario
|
||||||
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);
|
fs.existsSync.mockReturnValue(false);
|
||||||
|
|
||||||
// Call the function you want to test
|
// Run your function that checks the repository
|
||||||
testFunction();
|
await require('../src/main');
|
||||||
|
|
||||||
// Assertions
|
// Verify that the error is set and the process is exited
|
||||||
expect(core.setFailed).toHaveBeenCalledWith(
|
expect(core.setFailed).toHaveBeenCalledWith('The repository was not cloned. Please specify the actions/checkout action before this step.');
|
||||||
'The repository was not cloned. Please specify the actions/checkout action before this step.'
|
|
||||||
);
|
|
||||||
expect(process.exit).toHaveBeenCalledWith(1);
|
expect(process.exit).toHaveBeenCalledWith(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Handles non-Windows platform', () => {
|
it('should handle Windows platform error', async () => {
|
||||||
process.platform = 'linux';
|
// Mock process.platform to simulate non-Windows scenario
|
||||||
|
Object.defineProperty(process, 'platform', {
|
||||||
|
value: 'linux',
|
||||||
|
});
|
||||||
|
|
||||||
// Call the function you want to test
|
// Run your function that checks the platform
|
||||||
testFunction();
|
await require('../src/main');
|
||||||
|
|
||||||
// Assertions
|
// Verify that the error is set and the process is exited
|
||||||
expect(core.setFailed).toHaveBeenCalledWith('This action is only supported on Windows!');
|
expect(core.setFailed).toHaveBeenCalledWith('This action is only supported on Windows!');
|
||||||
expect(process.exit).toHaveBeenCalledWith(1);
|
expect(process.exit).toHaveBeenCalledWith(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should execute Inno Setup compiler command on Windows', async () => {
|
||||||
|
// Mock fs.existsSync to simulate repository cloned scenario
|
||||||
|
fs.existsSync.mockReturnValue(true);
|
||||||
|
|
||||||
|
// Mock process.platform to simulate Windows scenario
|
||||||
|
Object.defineProperty(process, 'platform', {
|
||||||
|
value: 'win32',
|
||||||
|
});
|
||||||
|
|
||||||
|
// Mock child_process.exec to simulate a successful execution
|
||||||
|
exec.mockImplementation((command, options, callback) => {
|
||||||
|
callback(null, 'stdout', 'stderr');
|
||||||
|
});
|
||||||
|
|
||||||
|
// Run your function that executes the Inno Setup compiler command
|
||||||
|
await require('../src/main');
|
||||||
|
|
||||||
|
// Verify that the process is not exited and no error is set
|
||||||
|
expect(core.setFailed).not.toHaveBeenCalled();
|
||||||
|
expect(process.exit).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
// Restore the original environment variables after each test
|
||||||
|
jest.resetModules();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user