- Add Rhai scripting integration to the SAL library. - Create documentation for Rhai module usage and available functions. - Implement comprehensive test suite for the Rhai integration. - Add `.gitignore` entries for test related temporary files.
4.0 KiB
OS Module Tests
This document describes the test scripts for the OS module in the SAL library. These tests verify the functionality of the OS module's file system operations, download capabilities, and package management features.
Test Structure
The tests are organized into three main scripts:
- File Operations (
01_file_operations.rhai
): Tests file system operations like creating, reading, writing, and manipulating files and directories. - Download Operations (
02_download_operations.rhai
): Tests downloading files from the internet and related operations. - Package Operations (
03_package_operations.rhai
): Tests package management functionality.
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 on the run_script
function.
Running the Tests
To run all tests, execute the following command from the project root:
# Assume that you have the herodo binary/built into your system
herodo --path src/rhai_tests/os/run_all_tests.rhai
To run individual test scripts:
# Assume that you have the herodo binary/built into your system
herodo --path src/rhai_tests/os/01_file_operations.rhai
Test Details
File Operations Test
The file operations test (01_file_operations.rhai
) verifies the following functions:
mkdir
: Creating directoriesfile_write
: Writing content to filesfile_read
: Reading content from filesfile_size
: Getting file sizefile_write_append
: Appending content to filescopy
: Copying filesmv
: Moving filesfind_file
: Finding a single file matching a patternfind_files
: Finding multiple files matching a patternfind_dir
: Finding a single directory matching a patternfind_dirs
: Finding multiple directories matching a patternchdir
: Changing the current working directoryrsync
: Synchronizing directoriesdelete
: Deleting files and directoriesexist
: Checking if files or directories exist
The test creates a temporary directory structure, performs operations on it, and then cleans up after itself.
Download Operations Test
The download operations test (02_download_operations.rhai
) verifies the following functions:
which
: Checking if a command exists in the system PATHcmd_ensure_exists
: Ensuring commands existdownload_file
: Downloading a file from a URLchmod_exec
: Making a file executable
The test downloads a small file from GitHub, verifies its content, and then cleans up.
Package Operations Test
The package operations test (03_package_operations.rhai
) verifies the following functions:
package_platform
: Getting the current platformpackage_set_debug
: Setting debug mode for package operationspackage_is_installed
: Checking if a package is installedpackage_search
: Searching for packagespackage_list
: Listing installed packages
Note: The test does not verify package_install
, package_remove
, package_update
, or package_upgrade
as these require root privileges and could modify the system state.
Test Runner
The test runner script (run_all_tests.rhai
) provides a framework for executing all tests and reporting results. It:
- Contains simplified versions of each test
- Runs each test in a try/catch block to handle errors
- Catches and reports any errors
- Provides a summary of passed and failed tests
Adding New Tests
To add a new test:
- Create a new Rhai script in the
src/rhai_tests/os
directory - Add a new test section to the
run_all_tests.rhai
script - Update this documentation to include information about the new test
Best Practices for Writing Tests
When writing tests for the OS module:
- Always clean up temporary files and directories
- Use assertions to verify expected behavior
- Print clear messages about what's being tested
- Handle errors gracefully
- Make tests independent of each other
- Avoid tests that require root privileges when possible
- Keep tests focused on specific functionality