sal/rhaiexamples/git_match_test.rhai
2025-04-04 21:21:50 +02:00

136 lines
5.0 KiB
Plaintext

// Test script for Git partial path matching functionality
print("===== Git Path Matching Test =====");
// Test git availability
print("\n=== Checking Git Availability ===");
let git_cmd = which("git");
if git_cmd != false {
print(`Git is available at: ${git_cmd}`);
} else {
print("WARNING: Git is not installed. Tests will be skipped.");
return "Git not available - test skipped";
}
// Helper function for test assertions
fn assert(condition, message) {
if (condition == false) {
print(`FAILED: ${message}`);
throw `Assertion failed: ${message}`;
} else {
print(`PASSED: ${message}`);
}
}
print("\n=== Setting up test repositories ===");
// Get current repos from git_list
let repos = git_list();
print(`Found ${repos.len()} local git repositories for testing`);
if repos.len() == 0 {
print("No repositories found for testing. Creating a test repo...");
// Create a test repo in a temporary directory
let test_dir = "~/tmp_test_repo";
print(`Creating test directory: ${test_dir}`);
// Initialize a git repo with run commands
let result = run_silent(`
mkdir -p ${test_dir}/test1
cd ${test_dir}/test1
git init
echo "Test content" > README.md
git add README.md
git config --global user.email "test@example.com"
git config --global user.name "Test User"
git commit -m "Initial commit"
`);
if result.success {
print("Created test repository successfully");
// Update the repos list
repos = git_list();
print(`Now found ${repos.len()} local git repositories`);
} else {
print("Failed to create test repository");
return "Test setup failed - could not create test repository";
}
}
// Simple function to get just the repo name from the path for easier debugging
fn get_repo_name(path) {
let parts = path.split("/");
return parts[parts.len()-1];
}
print("\n=== Test 1: Testing git_update with exact path ===");
// Using the first repo in the list for testing
let first_repo = repos[0];
print(`Using repository: ${first_repo}`);
print(`Repository name: ${get_repo_name(first_repo)}`);
// Test with exact path
let exact_result = git_update(first_repo);
print(`Result with exact path: ${exact_result}`);
// Check if the result was as expected
// Note: The function might find multiple repos even with exact path
// so we also check for that case
let is_success = exact_result.contains("up to date") ||
exact_result.contains("updated") ||
exact_result.contains("has local changes");
let multiple_match = exact_result.contains("Multiple repositories");
assert(is_success || multiple_match,
"git_update with path should succeed, indicate local changes, or report multiple matches");
print("\n=== Test 2: Testing git_update with partial path ===");
// Extract part of the path (last part of the path)
let repo_name = get_repo_name(first_repo);
print(`Testing partial match with: ${repo_name}`);
let partial_result = git_update(repo_name);
print(`Result with partial path: ${partial_result}`);
// Check if the result was as expected - similar to exact path test
let partial_success = partial_result.contains("up to date") ||
partial_result.contains("updated") ||
partial_result.contains("has local changes");
let partial_multiple = partial_result.contains("Multiple repositories");
assert(partial_success || partial_multiple,
"git_update with partial path should succeed, indicate local changes, or report multiple matches");
print("\n=== Test 3: Testing git_update with non-existent path ===");
let fake_repo = "this_repo_does_not_exist_anywhere";
print(`Testing with non-existent path: ${fake_repo}`);
let nonexist_result = git_update(fake_repo);
print(`Result with non-existent path: ${nonexist_result}`);
// Check that it properly reports an error
assert(nonexist_result.contains("No repositories found"),
"git_update with non-existent path should indicate no matching repos");
print("\n=== Test 4: Testing wildcard matching ===");
// Try to find a common substring in multiple repos
// For this test, we'll use a known path pattern likely to match multiple repos
let common_part = "code";
print(`Testing wildcard match with: ${common_part}*`);
let wildcard_result = git_update(common_part + "*");
print(`Result with wildcard: ${wildcard_result}`);
// For wildcard, we expect at least one match but not an error
// Implementation might return the first match or a success message
let wildcard_success = wildcard_result.contains("up to date") ||
wildcard_result.contains("updated") ||
wildcard_result.contains("has local changes");
// Just check that it didn't report no matches found
assert(!wildcard_result.contains("No repositories found"),
"Wildcard matching should find at least one repository");
print("\n===== All Git path matching tests completed! =====");
"Git path matching functionality works correctly"