...
Some checks are pending
Rhai Tests / Run Rhai Tests (push) Waiting to run

This commit is contained in:
2025-05-12 06:09:25 +03:00
parent 8285fdb7b9
commit 516d0177e7
73 changed files with 38 additions and 37 deletions

View File

@@ -0,0 +1,152 @@
// 01_mount_operations.rhai
// Tests for RFS mount operations
// Custom assert function
fn assert_true(condition, message) {
if !condition {
print(`ASSERTION FAILED: ${message}`);
throw message;
}
}
// Custom assert_eq function
fn assert_eq(actual, expected, message) {
if actual != expected {
print(`ASSERTION FAILED: ${message}`);
print(`Expected: "${expected}"`);
print(`Actual: "${actual}"`);
throw message;
}
}
// Helper function to check if rfs is available
fn is_rfs_available() {
try {
let result = run("which rfs");
return result.success;
} catch(err) {
return false;
}
}
// Helper function to clean up mounts
fn cleanup_mounts() {
try {
rfs_unmount_all();
print("All mounts cleaned up");
} catch(err) {
print(`Error cleaning up mounts: ${err}`);
}
}
print("=== Testing RFS Mount Operations ===");
// Check if rfs is available
let rfs_available = is_rfs_available();
if !rfs_available {
print("rfs is not available. Skipping RFS tests.");
// Exit gracefully without error
return;
}
print("✓ rfs is available");
// Clean up any existing mounts
cleanup_mounts();
// Create test directories
let source_dir = "rhai_test_rfs_source";
let target_dir = "rhai_test_rfs_target";
mkdir(source_dir);
mkdir(target_dir);
// Create a test file in the source directory
let test_file = `${source_dir}/test.txt`;
file_write(test_file, "Hello from RFS test");
try {
// Test mounting a local directory
print("Testing rfs_mount() with local directory...");
let options = #{
"readonly": "true"
};
let mount = rfs_mount(source_dir, target_dir, "local", options);
// Verify mount properties
assert_true(mount.id != "", "Mount ID should not be empty");
assert_eq(mount.source, source_dir, "Mount source should match");
assert_eq(mount.target, target_dir, "Mount target should match");
assert_eq(mount.fs_type, "local", "Mount type should be local");
print(`✓ rfs_mount(): Mounted ${mount.source} to ${mount.target} with ID: ${mount.id}`);
// Test listing mounts
print("Testing rfs_list_mounts()...");
let mounts = rfs_list_mounts();
assert_true(mounts.len() > 0, "There should be at least one mount");
// Find our mount in the list
let found = false;
for m in mounts {
if m.target == target_dir {
found = true;
break;
}
}
assert_true(found, "Our mount should be in the list");
print(`✓ rfs_list_mounts(): Found ${mounts.len()} mounts`);
// Test getting mount info
print("Testing rfs_get_mount_info()...");
let mount_info = rfs_get_mount_info(target_dir);
assert_eq(mount_info.target, target_dir, "Mount info target should match");
assert_eq(mount_info.source, source_dir, "Mount info source should match");
print(`✓ rfs_get_mount_info(): Got info for mount at ${mount_info.target}`);
// Verify the mounted file is accessible
let mounted_file = `${target_dir}/test.txt`;
assert_true(exist(mounted_file), "Mounted file should exist");
let mounted_content = file_read(mounted_file);
assert_eq(mounted_content, "Hello from RFS test", "Mounted file content should match");
print("✓ Mounted file is accessible and content matches");
// Test unmounting a specific mount
print("Testing rfs_unmount()...");
rfs_unmount(target_dir);
// Verify the mount is gone
try {
rfs_get_mount_info(target_dir);
assert_true(false, "Mount should not exist after unmounting");
} catch(err) {
print("✓ rfs_unmount(): Mount successfully unmounted");
}
// Mount again to test unmount_all
print("Testing mounting again for unmount_all...");
let mount2 = rfs_mount(source_dir, target_dir, "local", options);
assert_true(mount2.id != "", "Mount ID should not be empty");
// Test unmounting all mounts
print("Testing rfs_unmount_all()...");
rfs_unmount_all();
// Verify all mounts are gone
let mounts_after = rfs_list_mounts();
assert_true(mounts_after.len() == 0, "There should be no mounts after unmount_all");
print("✓ rfs_unmount_all(): All mounts successfully unmounted");
print("All mount operations tests completed successfully!");
} catch(err) {
print(`Error: ${err}`);
// Clean up in case of error
cleanup_mounts();
throw err;
} finally {
// Clean up test directories
delete(source_dir);
delete(target_dir);
print("✓ Cleanup: Test directories removed");
}

View File

@@ -0,0 +1,117 @@
// 02_filesystem_layer_operations.rhai
// Tests for RFS filesystem layer operations
// Custom assert function
fn assert_true(condition, message) {
if !condition {
print(`ASSERTION FAILED: ${message}`);
throw message;
}
}
// Custom assert_eq function
fn assert_eq(actual, expected, message) {
if actual != expected {
print(`ASSERTION FAILED: ${message}`);
print(`Expected: "${expected}"`);
print(`Actual: "${actual}"`);
throw message;
}
}
// Helper function to check if rfs is available
fn is_rfs_available() {
try {
let result = run("which rfs");
return result.success;
} catch(err) {
return false;
}
}
print("=== Testing RFS Filesystem Layer Operations ===");
// Check if rfs is available
let rfs_available = is_rfs_available();
if !rfs_available {
print("rfs is not available. Skipping RFS tests.");
// Exit gracefully without error
return;
}
print("✓ rfs is available");
// Create test directories
let source_dir = "rhai_test_rfs_source";
let unpack_dir = "rhai_test_rfs_unpack";
mkdir(source_dir);
mkdir(unpack_dir);
// Create test files in the source directory
file_write(`${source_dir}/file1.txt`, "Content of file 1");
file_write(`${source_dir}/file2.txt`, "Content of file 2");
// Create a subdirectory with files
mkdir(`${source_dir}/subdir`);
file_write(`${source_dir}/subdir/file3.txt`, "Content of file 3");
// Output file for the filesystem layer
let output_file = "rhai_test_rfs_layer.fl";
try {
// Test packing a directory
print("Testing rfs_pack()...");
// Use a file store spec for testing
let store_specs = "file:path=.";
rfs_pack(source_dir, output_file, store_specs);
// Verify the output file exists
assert_true(exist(output_file), "Output file should exist");
print(`✓ rfs_pack(): Directory packed to ${output_file}`);
// Test listing contents of the filesystem layer
print("Testing rfs_list_contents()...");
let contents = rfs_list_contents(output_file);
// Verify the contents include our files
assert_true(contents.contains("file1.txt"), "Contents should include file1.txt");
assert_true(contents.contains("file2.txt"), "Contents should include file2.txt");
assert_true(contents.contains("subdir/file3.txt"), "Contents should include subdir/file3.txt");
print("✓ rfs_list_contents(): Layer contents listed successfully");
// Test verifying the filesystem layer
print("Testing rfs_verify()...");
let is_valid = rfs_verify(output_file);
assert_true(is_valid, "Filesystem layer should be valid");
print("✓ rfs_verify(): Layer verified successfully");
// Test unpacking the filesystem layer
print("Testing rfs_unpack()...");
rfs_unpack(output_file, unpack_dir);
// Verify the unpacked files exist and have the correct content
assert_true(exist(`${unpack_dir}/file1.txt`), "Unpacked file1.txt should exist");
assert_true(exist(`${unpack_dir}/file2.txt`), "Unpacked file2.txt should exist");
assert_true(exist(`${unpack_dir}/subdir/file3.txt`), "Unpacked subdir/file3.txt should exist");
let content1 = file_read(`${unpack_dir}/file1.txt`);
let content2 = file_read(`${unpack_dir}/file2.txt`);
let content3 = file_read(`${unpack_dir}/subdir/file3.txt`);
assert_eq(content1, "Content of file 1", "Content of file1.txt should match");
assert_eq(content2, "Content of file 2", "Content of file2.txt should match");
assert_eq(content3, "Content of file 3", "Content of file3.txt should match");
print("✓ rfs_unpack(): Layer unpacked successfully");
print("All filesystem layer operations tests completed successfully!");
} catch(err) {
print(`Error: ${err}`);
throw err;
} finally {
// Clean up test directories and files
delete(source_dir);
delete(unpack_dir);
delete(output_file);
print("✓ Cleanup: Test directories and files removed");
}

View File

@@ -0,0 +1,168 @@
// run_all_tests.rhai
// Runs all RFS module tests
print("=== Running RFS Module Tests ===");
// Custom assert function
fn assert_true(condition, message) {
if !condition {
print(`ASSERTION FAILED: ${message}`);
throw message;
}
}
// Helper function to check if rfs is available
fn is_rfs_available() {
try {
let result = run("which rfs");
return result.success;
} catch(e) {
return false;
}
}
// Helper function to clean up mounts
fn cleanup_mounts() {
try {
rfs_unmount_all();
} catch(e) {
// Ignore errors during cleanup
}
}
// Run each test directly
let passed = 0;
let failed = 0;
let skipped = 0;
let total = 0;
// Check if rfs is available
let rfs_available = is_rfs_available();
if !rfs_available {
print("rfs is not available. Skipping all RFS tests.");
skipped = 2; // Skip both tests
total = 2;
} else {
// Test 1: Mount Operations
print("\n--- Running Mount Operations Tests ---");
try {
// Clean up any existing mounts
cleanup_mounts();
// Create test directories
let source_dir = "rhai_test_rfs_source";
let target_dir = "rhai_test_rfs_target";
mkdir(source_dir);
mkdir(target_dir);
// Create a test file in the source directory
let test_file = `${source_dir}/test.txt`;
file_write(test_file, "Hello from RFS test");
// Mount the directory
let options = #{
"readonly": "true"
};
let mount = rfs_mount(source_dir, target_dir, "local", options);
assert_true(mount.id != "", "Mount ID should not be empty");
// List mounts
let mounts = rfs_list_mounts();
assert_true(mounts.len() > 0, "There should be at least one mount");
// Unmount
rfs_unmount(target_dir);
// Clean up
delete(source_dir);
delete(target_dir);
print("--- Mount Operations Tests completed successfully ---");
passed += 1;
} catch(err) {
print(`!!! Error in Mount Operations Tests: ${err}`);
failed += 1;
// Clean up in case of error
cleanup_mounts();
try {
delete("rhai_test_rfs_source");
delete("rhai_test_rfs_target");
} catch(e) {
// Ignore errors during cleanup
}
}
total += 1;
// Test 2: Filesystem Layer Operations
print("\n--- Running Filesystem Layer Operations Tests ---");
try {
// Create test directories
let source_dir = "rhai_test_rfs_source";
let unpack_dir = "rhai_test_rfs_unpack";
mkdir(source_dir);
mkdir(unpack_dir);
// Create test files in the source directory
file_write(`${source_dir}/file1.txt`, "Content of file 1");
// Output file for the filesystem layer
let output_file = "rhai_test_rfs_layer.fl";
// Pack the directory
let store_specs = "file:path=.";
rfs_pack(source_dir, output_file, store_specs);
// List contents
let contents = rfs_list_contents(output_file);
assert_true(contents.contains("file1.txt"), "Contents should include file1.txt");
// Verify the layer
let is_valid = rfs_verify(output_file);
assert_true(is_valid, "Filesystem layer should be valid");
// Unpack the layer
rfs_unpack(output_file, unpack_dir);
// Clean up
delete(source_dir);
delete(unpack_dir);
delete(output_file);
print("--- Filesystem Layer Operations Tests completed successfully ---");
passed += 1;
} catch(err) {
print(`!!! Error in Filesystem Layer Operations Tests: ${err}`);
failed += 1;
// Clean up in case of error
try {
delete("rhai_test_rfs_source");
delete("rhai_test_rfs_unpack");
delete("rhai_test_rfs_layer.fl");
} catch(e) {
// Ignore errors during cleanup
}
}
total += 1;
}
print("\n=== Test Summary ===");
print(`Passed: ${passed}`);
print(`Failed: ${failed}`);
print(`Skipped: ${skipped}`);
print(`Total: ${total}`);
if failed == 0 {
if skipped > 0 {
print("\n⚠ All tests skipped or passed!");
} else {
print("\n✅ All tests passed!");
}
} else {
print("\n❌ Some tests failed!");
}
// Return the number of failed tests (0 means success)
failed;