Files
privacy.sexy/tests/integration/infrastructure/CodeRunner/ScriptFileCodeRunner.spec.ts
undergroundwires a721e82a4f Bump TypeScript to 5.3 with verbatimModuleSyntax
This commit upgrades TypeScript to the latest version 5.3 and introduces
`verbatimModuleSyntax` in line with the official Vue guide
recommendatinos (vuejs/docs#2592).

By enforcing `import type` for type-only imports, this commit improves
code clarity and supports tooling optimization, ensuring imports are
only bundled when necessary for runtime.

Changes:

- Bump TypeScript to 5.3.3 across the project.
- Adjust import statements to utilize `import type` where applicable,
  promoting cleaner and more efficient code.
2024-02-27 04:20:22 +01:00

101 lines
3.4 KiB
TypeScript

import { mkdtemp } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { exec } from 'node:child_process';
import { describe, it } from 'vitest';
import type { ScriptDirectoryProvider } from '@/infrastructure/CodeRunner/Creation/Directory/ScriptDirectoryProvider';
import { ScriptFileCreationOrchestrator } from '@/infrastructure/CodeRunner/Creation/ScriptFileCreationOrchestrator';
import { ScriptFileCodeRunner } from '@/infrastructure/CodeRunner/ScriptFileCodeRunner';
import { CurrentEnvironment } from '@/infrastructure/RuntimeEnvironment/RuntimeEnvironmentFactory';
import { OperatingSystem } from '@/domain/OperatingSystem';
import { LinuxTerminalEmulator } from '@/infrastructure/CodeRunner/Execution/VisibleTerminalScriptFileExecutor';
import { formatAssertionMessage } from '@tests/shared/FormatAssertionMessage';
describe('ScriptFileCodeRunner', () => {
it('executes simple script correctly', async ({ skip }) => {
// arrange
const currentOperatingSystem = CurrentEnvironment.os;
if (await shouldSkipTest(currentOperatingSystem)) {
skip();
return;
}
const temporaryDirectoryProvider = createTemporaryDirectoryProvider();
const codeRunner = createCodeRunner(temporaryDirectoryProvider);
const args = getPlatformSpecificArguments(currentOperatingSystem);
// act
const { success, error } = await codeRunner.runCode(...args);
// assert
expect(success).to.equal(true, formatAssertionMessage([
'Failed to successfully execute the script.',
'Details:', JSON.stringify(error),
]));
});
});
function getPlatformSpecificArguments(
os: OperatingSystem | undefined,
): Parameters<ScriptFileCodeRunner['runCode']> {
switch (os) {
case undefined:
throw new Error('Operating system detection failed: Unable to identify the current platform.');
case OperatingSystem.Windows:
return [
[
'@echo off',
'echo Hello, World!',
'exit /b 0',
].join('\n'),
'bat',
];
case OperatingSystem.macOS:
case OperatingSystem.Linux:
return [
[
'#!/bin/bash',
'echo "Hello, World!"',
'exit 0',
].join('\n'),
'sh',
];
default:
throw new Error(`Platform not supported: The current operating system (${os}) is not compatible with this script execution.`);
}
}
function shouldSkipTest(
os: OperatingSystem | undefined,
): Promise<boolean> {
if (os !== OperatingSystem.Linux) {
return Promise.resolve(false);
}
return isLinuxTerminalEmulatorSupported();
}
function isLinuxTerminalEmulatorSupported(): Promise<boolean> {
return new Promise((resolve) => {
exec(`which ${LinuxTerminalEmulator}`).on('close', (exitCode) => {
resolve(exitCode !== 0);
});
});
}
function createCodeRunner(directoryProvider: ScriptDirectoryProvider): ScriptFileCodeRunner {
return new ScriptFileCodeRunner(
undefined,
new ScriptFileCreationOrchestrator(undefined, undefined, directoryProvider),
);
}
function createTemporaryDirectoryProvider(): ScriptDirectoryProvider {
return {
provideScriptDirectory: async () => {
const temporaryDirectoryPathPrefix = join(tmpdir(), 'privacy-sexy-tests-');
const temporaryDirectoryFullPath = await mkdtemp(temporaryDirectoryPathPrefix);
return {
success: true,
directoryAbsolutePath: temporaryDirectoryFullPath,
};
},
};
}