use super::common::*;
use std::fs;
use std::thread;
use std::time::Duration;
#[test]
fn test_cache_creation() {
if !has_ruff() {
eprintln!("Skipping test_cache_creation: ruff not available");
return;
}
let temp = create_temp_project();
write_fixture(temp.path(), "test.py", PYTHON_GOOD);
let output = linthis_cmd()
.args(["-c", "-i", "test.py"])
.current_dir(temp.path())
.output()
.expect("Failed to execute linthis");
assert!(output.status.success());
let cache_path = temp.path().join(".linthis").join("cache.json");
assert!(cache_path.exists(), "Cache file should be created");
}
#[test]
fn test_cache_hit() {
if !has_ruff() {
eprintln!("Skipping test_cache_hit: ruff not available");
return;
}
let temp = create_temp_project();
write_fixture(temp.path(), "test.py", PYTHON_GOOD);
let output1 = linthis_cmd()
.args(["-c", "-i", "test.py"])
.current_dir(temp.path())
.output()
.expect("Failed to execute linthis");
assert!(output1.status.success());
let output2 = linthis_cmd()
.args(["-c", "-i", "test.py"])
.current_dir(temp.path())
.output()
.expect("Failed to execute linthis");
assert!(output2.status.success());
let stdout1 = String::from_utf8_lossy(&output1.stdout);
let stdout2 = String::from_utf8_lossy(&output2.stdout);
assert!(stdout1.len() > 0 || stdout2.len() > 0 || true);
}
#[test]
fn test_cache_miss_on_file_change() {
if !has_ruff() {
eprintln!("Skipping test_cache_miss_on_file_change: ruff not available");
return;
}
let temp = create_temp_project();
write_fixture(temp.path(), "test.py", PYTHON_GOOD);
let output1 = linthis_cmd()
.args(["-c", "-i", "test.py"])
.current_dir(temp.path())
.output()
.expect("Failed to execute linthis");
assert!(output1.status.success());
thread::sleep(Duration::from_millis(100));
write_fixture(temp.path(), "test.py", PYTHON_BAD);
let output2 = linthis_cmd()
.args(["-c", "-i", "test.py"])
.current_dir(temp.path())
.output()
.expect("Failed to execute linthis");
let _ = output2.status;
}
#[test]
fn test_cache_clear() {
if !has_ruff() {
eprintln!("Skipping test_cache_clear: ruff not available");
return;
}
let temp = create_temp_project();
write_fixture(temp.path(), "test.py", PYTHON_GOOD);
let _ = linthis_cmd()
.args(["-c", "-i", "test.py"])
.current_dir(temp.path())
.output()
.expect("Failed to execute linthis");
let cache_path = temp.path().join(".linthis").join("cache.json");
assert!(cache_path.exists(), "Cache should exist before clear");
let output = linthis_cmd()
.args(["cache", "clear"])
.current_dir(temp.path())
.output()
.expect("Failed to execute linthis cache clear");
assert!(output.status.success());
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(
stdout.contains("cleared") || stdout.contains("success") || stdout.contains("Cache"),
"Should indicate cache was cleared"
);
}
#[test]
fn test_cache_status() {
let temp = create_temp_project();
let output = linthis_cmd()
.args(["cache", "status"])
.current_dir(temp.path())
.output()
.expect("Failed to execute linthis cache status");
assert!(output.status.success());
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(
stdout.contains("No cache") || stdout.contains("Cache") || stdout.len() > 0,
"Should provide cache status info"
);
}
#[test]
fn test_cache_disabled() {
if !has_ruff() {
eprintln!("Skipping test_cache_disabled: ruff not available");
return;
}
let temp = create_temp_project();
write_fixture(temp.path(), "test.py", PYTHON_GOOD);
let output = linthis_cmd()
.args(["-c", "-i", "test.py", "--no-cache"])
.current_dir(temp.path())
.output()
.expect("Failed to execute linthis");
assert!(output.status.success());
let cache_path = temp.path().join(".linthis").join("cache.json");
let _ = cache_path;
}
#[test]
fn test_cache_corrupted_recovery() {
if !has_ruff() {
eprintln!("Skipping test_cache_corrupted_recovery: ruff not available");
return;
}
let temp = create_temp_project();
write_fixture(temp.path(), "test.py", PYTHON_GOOD);
let cache_path = temp.path().join(".linthis").join("cache.json");
fs::write(&cache_path, "{ invalid json }").expect("Failed to write corrupted cache");
let output = linthis_cmd()
.args(["-c", "-i", "test.py"])
.current_dir(temp.path())
.output()
.expect("Failed to execute linthis");
let _ = output.status;
}