Improve desktop runtime execution tests

Test improvements:

- Capture titles for all macOS windows, not just the frontmost.
- Incorporate missing application log files.
- Improve log clarity with enriched context.
- Improve application termination on macOS by reducing grace period.
- Ensure complete application termination on macOS.
- Validate Vue application loading through an initial log.
- Support ignoring environment-specific `stderr` errors.
- Do not fail the test if working directory cannot be deleted.
- Use retry pattern when installing dependencies due to network errors.

Refactorings:

- Migrate the test code to TypeScript.
- Replace deprecated `rmdir` with `rm` for error-resistant directory
  removal.
- Improve sanity checking by shifting from App.vue to Vue bootstrapper.
- Centralize environment variable management with `EnvironmentVariables`
  construct.
- Rename infrastructure/Environment to RuntimeEnvironment for clarity.
- Isolate WindowVariables and SystemOperations from RuntimeEnvironment.
- Inject logging via preloader.
- Correct mislabeled RuntimeSanity tests.

Configuration:

- Introduce `npm run check:desktop` for simplified execution.
- Omit `console.log` override due to `nodeIntegration` restrictions and
  reveal logging functionality using context-bridging.
This commit is contained in:
undergroundwires
2023-08-29 16:30:00 +02:00
parent 35be05df20
commit ad0576a752
146 changed files with 2418 additions and 1186 deletions

View File

@@ -0,0 +1,4 @@
export interface ExtractionResult {
readonly appExecutablePath: string;
readonly cleanup?: () => Promise<void>;
}

View File

@@ -1,9 +1,12 @@
import { access, chmod } from 'fs/promises';
import { constants } from 'fs';
import { findSingleFileByExtension } from '../../utils/io.js';
import { log } from '../../utils/log.js';
import { findSingleFileByExtension } from '../../utils/io';
import { log } from '../../utils/log';
import { ExtractionResult } from './extraction-result';
export async function prepareLinuxApp(desktopDistPath) {
export async function prepareLinuxApp(
desktopDistPath: string,
): Promise<ExtractionResult> {
const { absolutePath: appFile } = await findSingleFileByExtension(
'AppImage',
desktopDistPath,
@@ -14,7 +17,7 @@ export async function prepareLinuxApp(desktopDistPath) {
};
}
async function makeExecutable(appFile) {
async function makeExecutable(appFile: string): Promise<void> {
if (!appFile) { throw new Error('missing file'); }
if (await isExecutable(appFile)) {
log('AppImage is already executable.');
@@ -24,7 +27,7 @@ async function makeExecutable(appFile) {
await chmod(appFile, 0o755);
}
async function isExecutable(file) {
async function isExecutable(file: string): Promise<boolean> {
try {
await access(file, constants.X_OK);
return true;

View File

@@ -1,8 +1,12 @@
import { runCommand } from '../../utils/run-command.js';
import { findSingleFileByExtension, exists } from '../../utils/io.js';
import { log, die, LOG_LEVELS } from '../../utils/log.js';
import { runCommand } from '../../utils/run-command';
import { findSingleFileByExtension, exists } from '../../utils/io';
import { log, die, LogLevel } from '../../utils/log';
import { sleep } from '../../utils/sleep';
import { ExtractionResult } from './extraction-result';
export async function prepareMacOsApp(desktopDistPath) {
export async function prepareMacOsApp(
desktopDistPath: string,
): Promise<ExtractionResult> {
const { absolutePath: dmgPath } = await findSingleFileByExtension('dmg', desktopDistPath);
const { mountPath } = await mountDmg(dmgPath);
const appPath = await findMacAppExecutablePath(mountPath);
@@ -15,8 +19,12 @@ export async function prepareMacOsApp(desktopDistPath) {
};
}
async function mountDmg(dmgFile) {
const { stdout: hdiutilOutput, error } = await runCommand(`hdiutil attach '${dmgFile}'`);
async function mountDmg(
dmgFile: string,
) {
const { stdout: hdiutilOutput, error } = await runCommand(
`hdiutil attach '${dmgFile}'`,
);
if (error) {
die(`Failed to mount DMG file at ${dmgFile}.\n${error}`);
}
@@ -27,12 +35,14 @@ async function mountDmg(dmgFile) {
};
}
async function findMacAppExecutablePath(mountPath) {
async function findMacAppExecutablePath(
mountPath: string,
): Promise<string> {
const { stdout: findOutput, error } = await runCommand(
`find '${mountPath}' -maxdepth 1 -type d -name "*.app"`,
);
if (error) {
die(`Failed to find executable path at mount path ${mountPath}\n${error}`);
return die(`Failed to find executable path at mount path ${mountPath}\n${error}`);
}
const appFolder = findOutput.trim();
const appName = appFolder.split('/').pop().replace('.app', '');
@@ -40,16 +50,19 @@ async function findMacAppExecutablePath(mountPath) {
if (await exists(appPath)) {
log(`Application is located at ${appPath}`);
} else {
die(`Application does not exist at ${appPath}`);
return die(`Application does not exist at ${appPath}`);
}
return appPath;
}
async function detachMount(mountPath, retries = 5) {
async function detachMount(
mountPath: string,
retries = 5,
) {
const { error } = await runCommand(`hdiutil detach '${mountPath}'`);
if (error) {
if (retries <= 0) {
log(`Failed to detach mount after multiple attempts: ${mountPath}\n${error}`, LOG_LEVELS.WARN);
log(`Failed to detach mount after multiple attempts: ${mountPath}\n${error}`, LogLevel.Warn);
return;
}
await sleep(500);
@@ -58,9 +71,3 @@ async function detachMount(mountPath, retries = 5) {
}
log(`Successfully detached from ${mountPath}`);
}
function sleep(milliseconds) {
return new Promise((resolve) => {
setTimeout(resolve, milliseconds);
});
}

View File

@@ -1,38 +1,46 @@
import { mkdtemp, rmdir } from 'fs/promises';
import { mkdtemp, rm } from 'fs/promises';
import { join } from 'path';
import { tmpdir } from 'os';
import { findSingleFileByExtension, exists } from '../../utils/io.js';
import { log, die } from '../../utils/log.js';
import { runCommand } from '../../utils/run-command.js';
import { findSingleFileByExtension, exists } from '../../utils/io';
import { log, die, LogLevel } from '../../utils/log';
import { runCommand } from '../../utils/run-command';
import { ExtractionResult } from './extraction-result';
export async function prepareWindowsApp(desktopDistPath) {
export async function prepareWindowsApp(
desktopDistPath: string,
): Promise<ExtractionResult> {
const workdir = await mkdtemp(join(tmpdir(), 'win-nsis-installation-'));
if (await exists(workdir)) {
log(`Temporary directory ${workdir} already exists, cleaning up...`);
await rmdir(workdir, { recursive: true });
await rm(workdir, { recursive: true });
}
const { appExecutablePath } = await installNsis(workdir, desktopDistPath);
const appExecutablePath = await installNsis(workdir, desktopDistPath);
return {
appExecutablePath,
cleanup: async () => {
log(`Cleaning up working directory ${workdir}...`);
await rmdir(workdir, { recursive: true });
try {
await rm(workdir, { recursive: true, force: true });
} catch (error) {
log(`Could not cleanup the working directory: ${error.message}`, LogLevel.Error);
}
},
};
}
async function installNsis(installationPath, desktopDistPath) {
async function installNsis(
installationPath: string,
desktopDistPath: string,
): Promise<string> {
const { absolutePath: installerPath } = await findSingleFileByExtension('exe', desktopDistPath);
log(`Silently installing contents of ${installerPath} to ${installationPath}...`);
const { error } = await runCommand(`"${installerPath}" /S /D=${installationPath}`);
if (error) {
die(`Failed to install.\n${error}`);
return die(`Failed to install.\n${error}`);
}
const { absolutePath: appExecutablePath } = await findSingleFileByExtension('exe', installationPath);
return {
appExecutablePath,
};
return appExecutablePath;
}