UNPKG

@summarisation/fileutils

Version:

fileutils for summarisation

168 lines (167 loc) 7.38 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const shell_1 = require("./shell"); const path = __importStar(require("path")); const os = __importStar(require("node:os")); const utils_1 = require("@laoban/utils"); describe('isSuccessfulShellResult', () => { it('should return true for a successful shell result', () => { const result = { code: 0, message: 'Success' }; expect((0, shell_1.isSuccessfulShellResult)(result)).toBe(true); }); it('should return false for a failed shell result with non-zero code', () => { const result = { code: 1, message: 'Failed', error: 'Some error' }; expect((0, shell_1.isSuccessfulShellResult)(result)).toBe(false); }); }); describe('executeScriptInShell', () => { const defaultConfig = { encoding: 'utf8', debug: false }; const currentDir = process.cwd(); const parentDir = path.dirname(currentDir); it('should execute "pwd" command in current directory', async () => { const cmd = os.platform() === 'win32' ? 'cd' : 'pwd'; const result = await (0, shell_1.executeScriptInShell)(currentDir, cmd, defaultConfig); expect(result.code).toBe(0); expect(result.message.trim()).toContain(currentDir); }); it('should execute "pwd" command in parent directory', async () => { const cmd = os.platform() === 'win32' ? 'cd' : 'pwd'; const result = await (0, shell_1.executeScriptInShell)(parentDir, cmd, defaultConfig); expect(result.code).toBe(0); expect(result.message.trim()).toContain(parentDir); expect(result.message.trim()).not.toContain(currentDir); }); it('should execute "echo Hello World"', async () => { const cmd = 'echo Hello World'; const result = await (0, shell_1.executeScriptInShell)(currentDir, cmd, defaultConfig); expect(result.code).toBe(0); expect(result.message.trim()).toBe('Hello World'); }); it('should handle execution errors', async () => { const cmd = 'invalidCommand'; const result = await (0, shell_1.executeScriptInShell)(currentDir, cmd, defaultConfig); expect(result.code).not.toBe(0); expect(result.error).toBeDefined(); }); }); describe('execute', () => { const cwd = process.cwd(); // Use the current working directory const cmd = 'echo hello'; const invalidCmd = 'invalidcommand'; let originalConsoleLog; beforeAll(() => { // Save the original console.log method originalConsoleLog = console.log; }); afterAll(() => { // Restore the original console.log method console.log = originalConsoleLog; }); it('should log the command if debug is enabled', async () => { console.log = jest.fn(); const config = { debug: true }; await (0, shell_1.execute)(cwd, cmd, config); expect(console.log).toHaveBeenCalledWith('execute ', cwd, cmd); }); it('should return an empty array if dryRun is enabled', async () => { const config = { dryRun: true }; const result = await (0, shell_1.execute)(cwd, cmd, config); expect(result).toEqual([]); }); it('should return the message if the command is successful', async () => { const config = {}; const result = await (0, shell_1.execute)(cwd, cmd, config); if ((0, utils_1.hasErrors)(result)) throw new Error(result.join(';')); expect(result.trim()).toEqual('hello'); }); it('should return the error if the command fails', async () => { const config = {}; const result = await (0, shell_1.execute)(cwd, invalidCmd, config); expect((0, utils_1.errors)(result).join(';')).toContain('invalidcommand'); // Adjust this based on your OS error message }); it('should use the custom executeInShell function if provided', async () => { const customExecuteInShell = jest.fn().mockResolvedValue({ code: 0, message: 'Custom Success', }); const config = { executeInShell: customExecuteInShell }; const result = await (0, shell_1.execute)(cwd, cmd, config); expect(result).toEqual('Custom Success'); expect(customExecuteInShell).toHaveBeenCalledWith(cwd, cmd, config); }); }); describe('executeRecursivelyCmdChanges', () => { const cwd = process.cwd(); // Use the current working directory const startDir = path.join(cwd, 'test'); // Assuming 'test' directory exists in the current working directory const normalizePath = (p) => p.replace(/\\/g, '/'); let originalConsoleLog; let consoleOutput = []; beforeAll(() => { // Save the original console.log method originalConsoleLog = console.log; console.log = (message) => { consoleOutput.push(message); }; }); afterAll(() => { // Restore the original console.log method console.log = originalConsoleLog; }); beforeEach(() => { consoleOutput = []; }); //note: really hard to check it actually did this... so just check it ran. However with the next test we're probably ok it('should execute commands recursively for success', async () => { const result = await (0, shell_1.executeRecursivelyCmdChanges)(cwd, startDir, (dir) => `echo ${dir}`); const { executed, failed } = result; // Ensure some commands were executed expect(executed).toEqual(3); // Expecting no failures expect(failed).toEqual([]); }); it('should give failures for invalid commands', async () => { const result = await (0, shell_1.executeRecursivelyCmdChanges)(cwd, startDir, (dir) => `invalidcommand ${dir}`); const { executed, failed } = result; // Ensure some commands were executed expect(executed).toEqual(3); const clean = failed.map(normalizePath); expect(clean.length).toEqual(3); expect(clean[0]).toContain('/test '); expect(clean[0]).toContain('invalidcommand'); expect(clean[1]).toContain('/test/a '); expect(clean[1]).toContain('invalidcommand'); expect(clean[2]).toContain('/test/b '); expect(clean[2]).toContain('invalidcommand'); }); });