herolib_rust/src/rhaiexamples/git_test.rhai
2025-04-05 05:46:30 +02:00

164 lines
4.6 KiB
Plaintext

// Simplified test script for Git module functions
// Ensure test directory exists using a bash script
fn ensure_test_dir() {
print("Ensuring test directory exists at /tmp/code");
// Create a bash script to set up the test environment
let setup_script = `#!/bin/bash -ex
rm -rf /tmp/code
mkdir -p /tmp/code
cd /tmp/code
mkdir -p myserver.com/myaccount/repogreen
mkdir -p myserver.com/myaccount/repored
cd myserver.com/myaccount/repogreen
git init
echo 'Initial test file' > test.txt
git add test.txt
git config --local user.email 'test@example.com'
git config --local user.name 'Test User'
git commit -m 'Initial commit'
cd myserver.com/myaccount/repored
git init
echo 'Initial test file' > test2.txt
git add test2.txt
git config --local user.email 'test@example.com'
git config --local user.name 'Test User'
git commit -m 'Initial commit'
//now we have 2 repos
`;
// Run the setup script
let result = run(setup_script);
if !result.success {
print("Failed to set up test directory");
print(`Error: ${result.stderr}`);
throw "Test setup failed";
}
}
// Test GitTree creation
fn test_git_tree_creation() {
print("\n=== Testing GitTree creation ===");
let git_tree = gittree_new("/tmp/code");
print(`Created GitTree with base path: /tmp/code`);
}
// Test GitTree list method
fn test_git_tree_list() {
print("\n=== Testing GitTree list method ===");
let git_tree = gittree_new("/tmp/code");
let repos = git_tree.list();
print(`Found ${repos.len()} repositories`);
// Print repositories
for repo in repos {
print(` - ${repo}`);
}
if repos.len() == 0 {
print("No repositories found, which is unexpected");
throw "No repositories found";
}
if repos.len() != 2 {
print("No enough repositories found, needs to be 2");
throw "No enough repositories found";
}
}
// Test GitTree find method
fn test_git_tree_find() {
print("\n=== Testing GitTree find method ===");
let git_tree = gittree_new("/tmp/code");
// Search for repositories with "code" in the name
let search_pattern = "myaccount/repo"; //we need to check if we need *, would be better not
print(`Searching for repositories matching pattern: ${search_pattern}`);
let matching = git_tree.find(search_pattern);
print(`Found ${matching.len()} matching repositories`);
for repo in matching {
print(` - ${repo}`);
}
if matching.len() == 0 {
print("No matching repositories found, which is unexpected");
throw "No matching repositories found";
}
if repos.len() != 2 {
print("No enough repositories found, needs to be 2");
throw "No enough repositories found";
}
}
// Test GitRepo operations
fn test_git_repo_operations() {
print("\n=== Testing GitRepo operations ===");
let git_tree = gittree_new("/tmp/code");
let repos = git_tree.list();
if repos.len() == 0 {
print("No repositories found, which is unexpected");
throw "No repositories found";
}
// Get the first repo
let repo_path = repos[0];
print(`Testing operations on repository: ${repo_path}`);
// Get GitRepo object
let git_repos = git_tree.get(repo_path);
if git_repos.len() == 0 {
print("Failed to get GitRepo object");
throw "Failed to get GitRepo object";
}
let git_repo = git_repos[0];
// Test has_changes method
print("Testing has_changes method");
let has_changes = git_repo.has_changes();
print(`Repository has changes: ${has_changes}`);
// Create a change to test
print("Creating a change to test");
file_write("/tmp/code/test2.txt", "Another test file");
// Check if changes are detected
let has_changes_after = git_repo.has_changes();
print(`Repository has changes after modification: ${has_changes_after}`);
if !has_changes_after {
print("Changes not detected, which is unexpected");
throw "Changes not detected";
}
// Clean up the change
delete("/tmp/code/test2.txt");
}
// Run all tests
fn run_all_tests() {
print("Starting Git module tests...");
// Ensure test directory exists
ensure_test_dir();
// Run tests
test_git_tree_creation();
test_git_tree_list();
test_git_tree_find();
test_git_repo_operations();
print("\nAll tests completed successfully!");
}
// Run all tests
run_all_tests();