use super::common::*;
use std::fs;
#[test]
fn test_config_list() {
let temp = create_temp_project();
let config_content = r#"# linthis configuration
"#;
fs::write(temp.path().join(".linthis/config.toml"), config_content)
.expect("Failed to write config file");
let output = linthis_cmd()
.args(["config", "list"])
.current_dir(temp.path())
.output()
.expect("Failed to run linthis");
assert_exit_code(&output, 0);
}
#[test]
fn test_config_get() {
let temp = create_temp_project();
fs::write(temp.path().join(".linthis/config.toml"), "# config\n")
.expect("Failed to write config file");
let output = linthis_cmd()
.args(["config", "get", "excludes"])
.current_dir(temp.path())
.output()
.expect("Failed to run linthis");
let _ = output.status;
}
#[test]
fn test_config_set() {
let temp = create_temp_project();
let output = linthis_cmd()
.args(["config", "set", "test_key", "test_value"])
.current_dir(temp.path())
.output()
.expect("Failed to run linthis");
assert_exit_code(&output, 0);
}
#[test]
fn test_config_set_get_workflow() {
let temp = create_temp_project();
let set_output = linthis_cmd()
.args(["config", "set", "workflow_test", "my_value"])
.current_dir(temp.path())
.output()
.expect("Failed to run linthis");
assert_exit_code(&set_output, 0);
let get_output = linthis_cmd()
.args(["config", "get", "workflow_test"])
.current_dir(temp.path())
.output()
.expect("Failed to run linthis");
assert_exit_code(&get_output, 0);
let stdout = String::from_utf8_lossy(&get_output.stdout);
assert!(stdout.contains("my_value"), "Should retrieve the set value");
}
#[test]
fn test_init_creates_config() {
let temp = create_temp_project();
let _ = fs::remove_dir_all(temp.path().join(".linthis"));
let output = linthis_cmd()
.args(["init"])
.current_dir(temp.path())
.output()
.expect("Failed to run linthis");
assert_exit_code(&output, 0);
assert!(
temp.path().join(".linthis").exists(),
".linthis directory should be created"
);
}
#[test]
fn test_init_force() {
let temp = create_temp_project();
let first_init = linthis_cmd()
.args(["init"])
.current_dir(temp.path())
.output()
.expect("Failed to run linthis");
assert_exit_code(&first_init, 0);
let force_init = linthis_cmd()
.args(["init", "--force"])
.current_dir(temp.path())
.output()
.expect("Failed to run linthis");
assert_exit_code(&force_init, 0);
}
#[test]
fn test_init_with_hook() {
if !has_git() {
eprintln!("Skipping test_init_with_hook: git not available");
return;
}
let temp = create_temp_project();
std::process::Command::new("git")
.args(["init"])
.current_dir(temp.path())
.output()
.expect("Failed to init git");
let output = linthis_cmd()
.args(["init", "--with-hook"])
.current_dir(temp.path())
.output()
.expect("Failed to run linthis");
assert_exit_code(&output, 0);
let hook_path = temp.path().join(".git/hooks/pre-commit");
assert!(hook_path.exists(), "Pre-commit hook should be created");
}
#[test]
fn test_config_project_file() {
let temp = create_temp_project();
let config_content = r#"[excludes]
patterns = ["vendor/**", "third_party/**"]
[languages]
enabled = ["python", "rust"]
"#;
fs::write(temp.path().join(".linthis/config.toml"), config_content)
.expect("Failed to write config file");
let output = linthis_cmd()
.args(["config", "list"])
.current_dir(temp.path())
.output()
.expect("Failed to run linthis");
assert_exit_code(&output, 0);
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(
stdout.len() > 0 || !stdout.is_empty(),
"Config list should produce output"
);
}
#[test]
fn test_hook_install() {
if !has_git() {
eprintln!("Skipping test_hook_install: git not available");
return;
}
let temp = create_temp_project();
std::process::Command::new("git")
.args(["init"])
.current_dir(temp.path())
.output()
.expect("Failed to init git");
let output = linthis_cmd()
.args(["hook", "install"])
.current_dir(temp.path())
.output()
.expect("Failed to run linthis");
assert_exit_code(&output, 0);
}
#[test]
fn test_hook_uninstall() {
if !has_git() {
eprintln!("Skipping test_hook_uninstall: git not available");
return;
}
let temp = create_temp_project();
std::process::Command::new("git")
.args(["init"])
.current_dir(temp.path())
.output()
.expect("Failed to init git");
let _ = linthis_cmd()
.args(["hook", "install"])
.current_dir(temp.path())
.output();
let output = linthis_cmd()
.args(["hook", "uninstall"])
.current_dir(temp.path())
.output()
.expect("Failed to run linthis");
assert_exit_code(&output, 0);
}