use serial_test::serial;
use std::fs;
use std::path::Path;
use std::process::Command;
use tempfile::TempDir;
mod common;
use common::sqry_bin;
fn init_git_repo(dir: &Path) -> Result<(), Box<dyn std::error::Error>> {
let output = Command::new("git")
.arg("-C")
.arg(dir)
.args(["init"])
.output()?;
if !output.status.success() {
return Err(format!(
"git init failed: {}",
String::from_utf8_lossy(&output.stderr)
)
.into());
}
Command::new("git")
.arg("-C")
.arg(dir)
.args(["config", "user.name", "Test User"])
.output()?;
Command::new("git")
.arg("-C")
.arg(dir)
.args(["config", "user.email", "test@example.com"])
.output()?;
Command::new("git")
.arg("-C")
.arg(dir)
.args(["config", "commit.gpgsign", "false"])
.output()?;
Ok(())
}
fn create_and_commit_file(
dir: &Path,
filename: &str,
content: &str,
) -> Result<(), Box<dyn std::error::Error>> {
fs::write(dir.join(filename), content)?;
Command::new("git")
.arg("-C")
.arg(dir)
.args(["add", filename])
.output()?;
let output = Command::new("git")
.arg("-C")
.arg(dir)
.args(["commit", "-m", &format!("Add {filename}")])
.output()?;
if !output.status.success() {
return Err(format!(
"git commit failed: {}",
String::from_utf8_lossy(&output.stderr)
)
.into());
}
Ok(())
}
#[test]
#[serial]
fn test_git_operations_within_limits() {
let temp_dir = TempDir::new().expect("Failed to create temp dir");
unsafe {
std::env::set_var("SQRY_GIT_MAX_OUTPUT_SIZE", "20971520"); }
init_git_repo(temp_dir.path()).expect("Failed to init git repo");
create_and_commit_file(
temp_dir.path(),
"test.rs",
"fn main() { println!(\"Hello\"); }",
)
.expect("Failed to create test file");
let output = Command::new(sqry_bin())
.arg("index")
.arg(temp_dir.path())
.output()
.expect("Failed to run sqry index");
assert!(
output.status.success(),
"sqry index should succeed with reasonable git output limit. stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
unsafe {
std::env::remove_var("SQRY_GIT_MAX_OUTPUT_SIZE");
}
}
#[test]
#[serial]
fn test_all_limits_independent() {
let temp_dir = TempDir::new().expect("Failed to create temp dir");
unsafe {
std::env::set_var("SQRY_GIT_MAX_OUTPUT_SIZE", "52428800"); std::env::set_var("SQRY_MMAP_THRESHOLD", "5242880"); std::env::set_var("SQRY_READ_BUFFER", "16384"); std::env::set_var("SQRY_WRITE_BUFFER", "16384"); std::env::set_var("SQRY_PARSE_BUFFER", "131072"); }
init_git_repo(temp_dir.path()).expect("Failed to init git repo");
create_and_commit_file(
temp_dir.path(),
"main.rs",
r#"
fn calculate_sum(numbers: &[i32]) -> i32 {
numbers.iter().sum()
}
fn main() {
let nums = vec![1, 2, 3, 4, 5];
println!("Sum: {}", calculate_sum(&nums));
}
"#,
)
.expect("Failed to create test file");
let output = Command::new(sqry_bin())
.arg("index")
.arg(temp_dir.path())
.output()
.expect("Failed to run sqry index");
assert!(
output.status.success(),
"sqry index should succeed with all limits set independently. \
This validates that P1-17 limits (git output, mmap threshold) are \
independent of P1-14 limits (buffers). stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let graph_snapshot = temp_dir.path().join(".sqry/graph/snapshot.sqry");
assert!(
graph_snapshot.exists(),
"Graph snapshot should exist after successful indexing with all limits set"
);
unsafe {
std::env::remove_var("SQRY_GIT_MAX_OUTPUT_SIZE");
std::env::remove_var("SQRY_MMAP_THRESHOLD");
std::env::remove_var("SQRY_READ_BUFFER");
std::env::remove_var("SQRY_WRITE_BUFFER");
std::env::remove_var("SQRY_PARSE_BUFFER");
}
}
#[test]
#[serial]
fn test_git_output_limit_env_var_respected() {
let temp_dir = TempDir::new().expect("Failed to create temp dir");
unsafe {
std::env::set_var("SQRY_GIT_MAX_OUTPUT_SIZE", "104857600"); }
init_git_repo(temp_dir.path()).expect("Failed to init git repo");
create_and_commit_file(
temp_dir.path(),
"lib.rs",
"pub fn add(a: i32, b: i32) -> i32 { a + b }",
)
.expect("Failed to create test file");
let output = Command::new(sqry_bin())
.arg("index")
.arg(temp_dir.path())
.output()
.expect("Failed to run sqry index");
assert!(
output.status.success(),
"Index should succeed with generous git output limit. stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
unsafe {
std::env::remove_var("SQRY_GIT_MAX_OUTPUT_SIZE");
}
}
#[test]
#[serial]
fn test_mmap_threshold_env_var_respected() {
let temp_dir = TempDir::new().expect("Failed to create temp dir");
unsafe {
std::env::set_var("SQRY_MMAP_THRESHOLD", "5242880"); }
let large_content = "// ".to_string() + &"x".repeat(6 * 1024 * 1024); fs::write(temp_dir.path().join("large.rs"), large_content)
.expect("Failed to create large file");
let output = Command::new(sqry_bin())
.arg("index")
.arg(temp_dir.path())
.output()
.expect("Failed to run sqry index");
assert!(
output.status.success(),
"Index should succeed with custom mmap threshold. stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
unsafe {
std::env::remove_var("SQRY_MMAP_THRESHOLD");
}
}