docs: Add documentation for Process module tests
Some checks failed
Rhai Tests / Run Rhai Tests (push) Waiting to run
Rhai Tests / Run Rhai Tests (pull_request) Has been cancelled

- Added documentation for the new Process module tests, including
  details on test structure, execution, and individual test
  descriptions.
- Created new documentation files for Process module tests.
- Updated the main Rhai documentation index to include the new
  Process module tests.
This commit is contained in:
Mahmoud Emad 2025-05-08 15:54:39 +03:00
parent 4578b10acb
commit 32217b6545
6 changed files with 402 additions and 0 deletions

View File

@ -32,6 +32,7 @@ SAL includes test scripts for verifying the functionality of its Rhai integratio
- [OS Module Tests](os_module_tests.md): Tests for file system, download, and package management operations
- [Git Module Tests](git_module_tests.md): Tests for Git repository management and operations
- [Process Module Tests](process_module_tests.md): Tests for command execution and process management
- [Running Tests](running_tests.md): Instructions for running all Rhai tests
- [CI Workflow](ci_workflow.md): Continuous integration workflow for Rhai tests

View File

@ -0,0 +1,79 @@
# Process Module Tests
This document describes the test scripts for the Process module in the SAL library. These tests verify the functionality of the Process module's command execution and process management features.
## Test Structure
The tests are organized into two main scripts:
1. **Command Execution** (`01_command_execution.rhai`): Tests command execution functions like `run()` and `which()`.
2. **Process Management** (`02_process_management.rhai`): Tests process management functions like `process_list()` and `process_get()`.
Additionally, there's a runner script (`run_all_tests.rhai`) that executes all tests and reports results. The runner script contains simplified versions of the individual tests to avoid dependency issues.
## Running the Tests
To run all tests, execute the following command from the project root:
```bash
herodo --path src/rhai_tests/process/run_all_tests.rhai
```
To run individual test scripts:
```bash
herodo --path src/rhai_tests/process/01_command_execution.rhai
```
## Test Details
### Command Execution Test
The command execution test (`01_command_execution.rhai`) verifies the following functions:
- `run()`: Running shell commands
- `run().do()`: Executing commands and capturing output
- `run().silent()`: Running commands without displaying output
- `run().ignore_error()`: Running commands that might fail without throwing errors
- `which()`: Finding the path of an executable
The test runs various commands and verifies their output and exit status.
### Process Management Test
The process management test (`02_process_management.rhai`) verifies the following functions:
- `process_list()`: Listing running processes
- `process_get()`: Getting information about a specific process
- Process properties: Accessing process information like PID, name, CPU usage, and memory usage
The test lists running processes and verifies that their properties are accessible.
## Test Runner
The test runner script (`run_all_tests.rhai`) provides a framework for executing all tests and reporting results. It:
1. Contains simplified versions of each test
2. Runs each test in a try/catch block to handle errors
3. Catches and reports any errors
4. Provides a summary of passed and failed tests
## Adding New Tests
To add a new test:
1. Create a new Rhai script in the `src/rhai_tests/process` directory
2. Add a new test section to the `run_all_tests.rhai` script
3. Update this documentation to include information about the new test
## Best Practices for Writing Tests
When writing tests for the Process module:
1. Use assertions to verify expected behavior
2. Print clear messages about what's being tested
3. Handle errors gracefully
4. Make tests independent of each other
5. Avoid tests that could disrupt the system (e.g., killing important processes)
6. Keep tests focused on specific functionality
7. Clean up any resources created during testing

131
run_rhai_tests.log Normal file
View File

@ -0,0 +1,131 @@
=======================================
 Running All Rhai Tests 
=======================================
Running tests for module: git
-------------------------------------
Found 1 Rhai script to execute:
Executing: src/rhai_tests/git/run_all_tests.rhai
=== Running Git Module Tests ===
--- Running Basic Git Operations Tests ---
Created test directory: rhai_test_git
Testing GitTree constructor...
✓ GitTree created successfully
Testing GitTree.list() with empty directory...
✓ GitTree.list(): Found 0 repositories (expected 0)
Testing GitTree.find() with empty directory...
✓ GitTree.find(): Found 0 repositories (expected 0)
Cleaning up...
✓ Cleanup: Directory rhai_test_git removed
--- Basic Git Operations Tests completed successfully ---
--- Running Git Repository Operations Tests ---
Created test directory: rhai_test_git_ops
Creating GitTree...
✓ GitTree created successfully
Cleaning up...
✓ Cleanup: Directory rhai_test_git_ops removed
--- Git Repository Operations Tests completed successfully ---
=== Test Summary ===
Passed: 2
Failed: 0
Total: 2
✅ All tests passed!
Script executed successfully
Result: 0
All scripts executed
✓ Module git tests passed
Running tests for module: os
-------------------------------------
Found 1 Rhai script to execute:
Executing: src/rhai_tests/os/run_all_tests.rhai
=== Running OS Module Tests ===
--- Running File Operations Tests ---
Testing mkdir...
✓ mkdir: Successfully created directory 'rhai_test_fs'
✓ mkdir (nested): Successfully created directory 'rhai_test_fs/subdir'
✓ file_write: Successfully wrote to file 'rhai_test_fs/test.txt'
✓ file_read: Content matches
✓ file_size: 48 bytes
✓ delete: Directory cleaned up
--- File Operations Tests completed successfully ---
--- Running Download Operations Tests ---
Created test directory: rhai_test_download
✓ which: curl found at /usr/bin/curl
✓ cmd_ensure_exists: Command 'curl' exists
Downloading https://raw.githubusercontent.com/rust-lang/rust/master/LICENSE-MIT...
Downloading https://raw.githubusercontent.com/rust-lang/rust/master/LICENSE-MIT to rhai_test_download/license.txt
Download complete! File size: 1.04 KB
✓ download_file: rhai_test_download/license.txt
✓ Downloaded file content verified
✓ Cleanup: Directory rhai_test_download removed
--- Download Operations Tests completed successfully ---
--- Running Package Operations Tests ---
Current platform: Ubuntu
✓ package_set_debug: Debug mode enabled
--- Package Operations Tests completed successfully ---
=== Test Summary ===
Passed: 3
Failed: 0
Total: 3
✅ All tests passed!
Script executed successfully
Result: 0
All scripts executed
✓ Module os tests passed
Running tests for module: process
-------------------------------------
Found 1 Rhai script to execute:
Executing: src/rhai_tests/process/run_all_tests.rhai
=== Running Process Module Tests ===
--- Running Command Execution Tests ---
Testing run() with a simple command...
Hello, World!
✓ run().execute(): Command executed successfully
Testing which() function...
✓ which(): Found bash at /usr/bin/bash
--- Command Execution Tests completed successfully ---
--- Running Process Management Tests ---
Testing process_list() function...
✓ process_list(): Found 344 processes
Testing process properties...
✓ Process properties: PID=1, Name=systemd
--- Process Management Tests completed successfully ---
=== Test Summary ===
Passed: 2
Failed: 0
Total: 2
✅ All tests passed!
Script executed successfully
Result: 0
All scripts executed
✓ Module process tests passed
=======================================
 Test Summary 
=======================================
Total modules tested: 3
Passed: 3
Failed: 0
All tests passed!

View File

@ -0,0 +1,61 @@
// 01_command_execution.rhai
// Tests for command execution in the Process module
// Custom assert function
fn assert_true(condition, message) {
if !condition {
print(`ASSERTION FAILED: ${message}`);
throw message;
}
}
print("=== Testing Basic Command Execution ===");
// Test running a simple command
print("Testing run() with a simple command...");
let result = run("echo Hello, World!").execute();
assert_true(result.success, "Command should succeed");
assert_true(result.stdout.contains("Hello, World!"), "Command output should contain the expected text");
print(`✓ run().execute(): Command executed successfully`);
// Test running a command with arguments
print("Testing run() with command arguments...");
let result_with_args = run("echo Hello from Rhai tests").execute();
assert_true(result_with_args.success, "Command with arguments should succeed");
assert_true(result_with_args.stdout.contains("Hello from Rhai tests"), "Command output should contain the expected text");
print(`✓ run().execute(): Command with arguments executed successfully`);
// Test running a command with environment variables
print("Testing run() with environment variables...");
let env_result = run("echo $HOME").execute();
assert_true(env_result.success, "Command with environment variables should succeed");
assert_true(env_result.stdout.trim() != "", "Environment variable should be expanded");
print(`✓ run().execute(): Command with environment variables executed successfully`);
// Test running a multiline script
print("Testing run() with a multiline script...");
let script_result = run(`
echo "Line 1"
echo "Line 2"
echo "Line 3"
`).execute();
assert_true(script_result.success, "Multiline script should succeed");
assert_true(script_result.stdout.contains("Line 1") && script_result.stdout.contains("Line 2") && script_result.stdout.contains("Line 3"),
"Script output should contain all lines");
print(`✓ run().execute(): Multiline script executed successfully`);
// Test which function
print("Testing which() function...");
let bash_path = which("bash");
assert_true(bash_path != "", "bash should be found in PATH");
print(`✓ which(): Found bash at ${bash_path}`);
// Test a command that doesn't exist
let nonexistent_cmd = which("this_command_does_not_exist_12345");
if nonexistent_cmd == "" {
print(`✓ which(): Correctly reported that nonexistent command was not found`);
} else {
print(`Note: Unexpectedly found command at ${nonexistent_cmd}`);
}
print("All command execution tests completed successfully!");

View File

@ -0,0 +1,54 @@
// 02_process_management.rhai
// Tests for process management functions in the Process module
// Custom assert function
fn assert_true(condition, message) {
if !condition {
print(`ASSERTION FAILED: ${message}`);
throw message;
}
}
print("=== Testing Process Management Functions ===");
// Test process_list function
print("Testing process_list() function...");
let all_processes = process_list("");
assert_true(all_processes.len() > 0, "There should be at least one running process");
print(`✓ process_list(): Found ${all_processes.len()} processes`);
// Test process properties
print("Testing process properties...");
let first_process = all_processes[0];
assert_true(first_process.pid > 0, "Process PID should be a positive number");
assert_true(first_process.name.len() > 0, "Process name should not be empty");
print(`✓ Process properties: PID=${first_process.pid}, Name=${first_process.name}, CPU=${first_process.cpu}%, Memory=${first_process.memory}`);
// Test process_list with a pattern
print("Testing process_list() with a pattern...");
// Use a pattern that's likely to match at least one process on most systems
let pattern = "sh";
let matching_processes = process_list(pattern);
print(`Found ${matching_processes.len()} processes matching '${pattern}'`);
if (matching_processes.len() > 0) {
let matched_process = matching_processes[0];
print(`✓ process_list(pattern): Found process ${matched_process.name} with PID ${matched_process.pid}`);
} else {
print(`Note: No processes found matching '${pattern}'. This is not necessarily an error.`);
}
// Test process_get function
// Note: We'll only test this if we found matching processes above
if (matching_processes.len() == 1) {
print("Testing process_get() function...");
let process = process_get(pattern);
assert_true(process.pid > 0, "Process PID should be a positive number");
assert_true(process.name.contains(pattern), "Process name should contain the pattern");
print(`✓ process_get(): Found process ${process.name} with PID ${process.pid}`);
} else {
print("Skipping process_get() test as it requires exactly one matching process");
}
// Note: We won't test the kill function as it could disrupt the system
print("All process management tests completed successfully!");

View File

@ -0,0 +1,76 @@
// run_all_tests.rhai
// Runs all Process module tests
print("=== Running Process Module Tests ===");
// Custom assert function
fn assert_true(condition, message) {
if !condition {
print(`ASSERTION FAILED: ${message}`);
throw message;
}
}
// Run each test directly
let passed = 0;
let failed = 0;
// Test 1: Command Execution
print("\n--- Running Command Execution Tests ---");
try {
// Test running a simple command
print("Testing run() with a simple command...");
let result = run("echo Hello, World!").execute();
assert_true(result.success, "Command should succeed");
assert_true(result.stdout.contains("Hello, World!"), "Command output should contain the expected text");
print(`✓ run().execute(): Command executed successfully`);
// Test which function
print("Testing which() function...");
let bash_path = which("bash");
assert_true(bash_path != "", "bash should be found in PATH");
print(`✓ which(): Found bash at ${bash_path}`);
print("--- Command Execution Tests completed successfully ---");
passed += 1;
} catch(err) {
print(`!!! Error in Command Execution Tests: ${err}`);
failed += 1;
}
// Test 2: Process Management
print("\n--- Running Process Management Tests ---");
try {
// Test process_list function
print("Testing process_list() function...");
let all_processes = process_list("");
assert_true(all_processes.len() > 0, "There should be at least one running process");
print(`✓ process_list(): Found ${all_processes.len()} processes`);
// Test process properties
print("Testing process properties...");
let first_process = all_processes[0];
assert_true(first_process.pid > 0, "Process PID should be a positive number");
assert_true(first_process.name.len() > 0, "Process name should not be empty");
print(`✓ Process properties: PID=${first_process.pid}, Name=${first_process.name}`);
print("--- Process Management Tests completed successfully ---");
passed += 1;
} catch(err) {
print(`!!! Error in Process Management Tests: ${err}`);
failed += 1;
}
print("\n=== Test Summary ===");
print(`Passed: ${passed}`);
print(`Failed: ${failed}`);
print(`Total: ${passed + failed}`);
if failed == 0 {
print("\n✅ All tests passed!");
} else {
print("\n❌ Some tests failed!");
}
// Return the number of failed tests (0 means success)
failed;