Fix script cancellation with new dialog on Linux

This commit improves the management of script execution process by
enhancing the way terminal commands are handled, paving the way for
easier future modifications and providing clearer feedback to users when
scripts are cancelled.

Previously, the UI displayed a generic error message which could lead to
confusion if the user intentionally cancelled the script execution. Now,
a specific error dialog will appear, improving the user experience by
accurately reflecting the action taken by the user.

This change affects code execution on Linux where closing GNOME terminal
returns exit code `137` which is then treated by script cancellation by
privacy.sexy to show the accurate error dialog. It does not affect macOS
and Windows as curret commands result in success (`0`) exit code on
cancellation.

Additionally, this update encapsulates OS-specific logic into dedicated
classes, promoting better separation of concerns and increasing the
modularity of the codebase. This makes it simpler to maintain and extend
the application.

Key changes:

- Display a specific error message for script cancellations.
- Refactor command execution into dedicated classes.
- Improve file permission setting flexibility and avoid setting file
  permissions on Windows as it's not required to execute files.
- Introduce more granular error types for script execution.
- Increase logging for shell commands to aid in debugging.
- Expand test coverage to ensure reliability.
- Fix error dialogs not showing the error messages due to incorrect
  propagation of errors.

Other supported changes:

- Update `SECURITY.md` with details on script readback and verification.
- Fix a typo in `IpcRegistration.spec.ts`.
- Document antivirus scans in `desktop-vs-web-features.md`.
This commit is contained in:
undergroundwires
2024-04-30 15:04:59 +02:00
parent 694bf1a74d
commit 8c17396285
49 changed files with 2097 additions and 606 deletions

View File

@@ -0,0 +1,74 @@
import { describe, it, expect } from 'vitest';
import { OsSpecificTerminalLaunchCommandFactory } from '@/infrastructure/CodeRunner/Execution/CommandDefinition/Factory/OsSpecificTerminalLaunchCommandFactory';
import type { RuntimeEnvironment } from '@/infrastructure/RuntimeEnvironment/RuntimeEnvironment';
import { RuntimeEnvironmentStub } from '@tests/unit/shared/Stubs/RuntimeEnvironmentStub';
import { AllSupportedOperatingSystems, type SupportedOperatingSystem } from '@tests/shared/TestCases/SupportedOperatingSystems';
import type { CommandDefinition } from '@/infrastructure/CodeRunner/Execution/CommandDefinition/CommandDefinition';
import { OperatingSystem } from '@/domain/OperatingSystem';
import { WindowsVisibleTerminalCommand } from '@/infrastructure/CodeRunner/Execution/CommandDefinition/Commands/WindowsVisibleTerminalCommand';
import type { Constructible } from '@/TypeHelpers';
import { LinuxVisibleTerminalCommand } from '@/infrastructure/CodeRunner/Execution/CommandDefinition/Commands/LinuxVisibleTerminalCommand';
import { MacOsVisibleTerminalCommand } from '@/infrastructure/CodeRunner/Execution/CommandDefinition/Commands/MacOsVisibleTerminalCommand';
describe('OsSpecificTerminalLaunchCommandFactory', () => {
describe('returns expected definitions for supported operating systems', () => {
const testScenarios: Record<SupportedOperatingSystem, Constructible<CommandDefinition>> = {
[OperatingSystem.Windows]: WindowsVisibleTerminalCommand,
[OperatingSystem.Linux]: LinuxVisibleTerminalCommand,
[OperatingSystem.macOS]: MacOsVisibleTerminalCommand,
};
AllSupportedOperatingSystems.forEach((operatingSystem) => {
it(`${OperatingSystem[operatingSystem]}`, () => {
// arrange
const expectedDefinitionType = testScenarios[operatingSystem];
const context = new TestContext()
.withOperatingSystem(operatingSystem);
// act
const actualDefinition = context.provideCommandDefinition();
// assert
expect(actualDefinition).to.be.instanceOf(expectedDefinitionType);
});
});
});
it('throws if the current operating system is undefined', () => {
// arrange
const expectedError = 'Operating system could not be identified from environment.';
const operatingSystem = undefined;
const context = new TestContext()
.withOperatingSystem(operatingSystem);
// act
const act = () => context.provideCommandDefinition();
// assert
expect(act).to.throw(expectedError);
});
it('throws for an unsupported operating system', () => {
// arrange
const unsupportedOperatingSystem = OperatingSystem.BlackBerryOS;
const expectedError = `Unsupported operating system: ${OperatingSystem[unsupportedOperatingSystem]}`;
const context = new TestContext()
.withOperatingSystem(unsupportedOperatingSystem);
// act
const act = () => context.provideCommandDefinition();
// assert
expect(act).to.throw(expectedError);
});
});
class TestContext {
private environment: RuntimeEnvironment = new RuntimeEnvironmentStub();
public withOperatingSystem(os: OperatingSystem | undefined): this {
this.environment = new RuntimeEnvironmentStub()
.withOs(os);
return this;
}
public provideCommandDefinition(): ReturnType<
OsSpecificTerminalLaunchCommandFactory['provideCommandDefinition']
> {
const sut = new OsSpecificTerminalLaunchCommandFactory(this.environment);
return sut.provideCommandDefinition();
}
}