sal/process/tests/rhai/03_error_handling.rhai
Mahmoud-Emad 3e3d0a1d45
Some checks are pending
Rhai Tests / Run Rhai Tests (push) Waiting to run
feat: Add process package to monorepo
- Add `sal-process` package for cross-platform process management.
- Update workspace members in `Cargo.toml`.
- Mark process package as complete in MONOREPO_CONVERSION_PLAN.md
- Remove license information from `mycelium` and `os` READMEs.
2025-06-22 11:41:10 +03:00

168 lines
5.4 KiB
Plaintext

// Test script for process error handling functionality
print("=== Process Error Handling Tests ===");
// Test 1: Command execution error handling
print("\n--- Test 1: Command Execution Error Handling ---");
try {
let result = run_command("nonexistent_command_12345");
assert_true(false, "Should have thrown an error for nonexistent command");
} catch(e) {
assert_true(true, "Correctly caught error for nonexistent command");
print("✓ Command execution error handling works");
}
// Test 2: Silent error handling with ignore_error
print("\n--- Test 2: Silent Error Handling with ignore_error ---");
let error_result = run("false").ignore_error().silent().execute();
assert_true(!error_result.success, "Command should fail");
assert_true(error_result.code != 0, "Exit code should be non-zero");
print("✓ Silent error handling with ignore_error works");
// Test 3: Process management error handling
print("\n--- Test 3: Process Management Error Handling ---");
try {
let result = process_get("nonexistent_process_12345");
assert_true(false, "Should have thrown an error for nonexistent process");
} catch(e) {
assert_true(true, "Correctly caught error for nonexistent process");
print("✓ Process management error handling works");
}
// Test 4: Script execution error handling
print("\n--- Test 4: Script Execution Error Handling ---");
let error_script = `
echo "Before error"
false
echo "After error"
`;
try {
let result = run_command(error_script);
assert_true(false, "Should have thrown an error for failing script");
} catch(e) {
assert_true(true, "Correctly caught error for failing script");
print("✓ Script execution error handling works");
}
// Test 5: Error handling with die=false in options
print("\n--- Test 5: Error Handling with die=false in Options ---");
let options = #{
silent: true,
die: false,
log: false
};
let no_die_result = run("false", options);
assert_true(!no_die_result.success, "Command should fail but not throw");
assert_true(no_die_result.code != 0, "Exit code should be non-zero");
print("✓ Error handling with die=false in options works");
// Test 6: Builder pattern error handling
print("\n--- Test 6: Builder Pattern Error Handling ---");
try {
let result = run("nonexistent_command_12345").silent().execute();
assert_true(false, "Should have thrown an error for nonexistent command in builder");
} catch(e) {
assert_true(true, "Correctly caught error for nonexistent command in builder");
print("✓ Builder pattern error handling works");
}
// Test 7: Multiple error conditions
print("\n--- Test 7: Multiple Error Conditions ---");
let error_conditions = [
"nonexistent_command_12345",
"false",
"exit 1"
];
for cmd in error_conditions {
try {
let result = run(cmd).silent().execute();
assert_true(false, `Should have thrown an error for: ${cmd}`);
} catch(e) {
// Expected behavior
}
}
print("✓ Multiple error conditions handled correctly");
// Test 8: Error recovery with ignore_error
print("\n--- Test 8: Error Recovery with ignore_error ---");
let recovery_script = `
echo "Starting script"
false
echo "This should not execute"
`;
let recovery_result = run(recovery_script).ignore_error().silent().execute();
assert_true(!recovery_result.success, "Script should fail");
assert_true(recovery_result.stdout.contains("Starting script"), "Should capture output before error");
print("✓ Error recovery with ignore_error works");
// Test 9: Nested error handling
print("\n--- Test 9: Nested Error Handling ---");
try {
try {
let result = run_command("nonexistent_command_12345");
assert_true(false, "Inner try should fail");
} catch(inner_e) {
// Re-throw to test outer catch
throw inner_e;
}
assert_true(false, "Outer try should fail");
} catch(outer_e) {
assert_true(true, "Nested error handling works");
print("✓ Nested error handling works");
}
// Test 10: Error message content validation
print("\n--- Test 10: Error Message Content Validation ---");
try {
let result = process_get("nonexistent_process_12345");
assert_true(false, "Should have thrown an error");
} catch(e) {
let error_msg = `${e}`;
assert_true(error_msg.len() > 0, "Error message should not be empty");
print(`✓ Error message content: ${error_msg}`);
}
// Test 11: Graceful degradation
print("\n--- Test 11: Graceful Degradation ---");
let graceful_commands = [
"echo 'fallback test'",
"printf 'fallback test'",
"print 'fallback test'"
];
let graceful_success = false;
for cmd in graceful_commands {
try {
let result = run_command(cmd);
if result.success {
graceful_success = true;
break;
}
} catch(e) {
// Try next command
continue;
}
}
assert_true(graceful_success, "Should find at least one working command for graceful degradation");
print("✓ Graceful degradation works");
// Test 12: Error handling performance
print("\n--- Test 12: Error Handling Performance ---");
let error_start = timestamp();
try {
let result = run_command("nonexistent_command_12345");
} catch(e) {
// Expected
}
let error_end = timestamp();
let error_duration = error_end - error_start;
assert_true(error_duration < 5000, "Error handling should be fast (< 5 seconds)");
print(`✓ Error handling completed in ${error_duration}ms`);
print("\n=== All Error Handling Tests Passed! ===");