Skip to content

Commit

Permalink
✅ test(config): cover --init overwrite prompts
Browse files Browse the repository at this point in the history
  • Loading branch information
welpo committed Feb 6, 2024
1 parent 4b09485 commit 3562f23
Showing 1 changed file with 113 additions and 0 deletions.
113 changes: 113 additions & 0 deletions tests/lint/test_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,64 @@ fn success_init_config() {
assert_eq!(file_contents.trim(), get_default_config().trim());
}

#[test]
fn success_config_overwrite_no() {
let tmp_dir = tempfile::tempdir().unwrap();
let tmp_dir_path = tmp_dir.path();

// Create an initial sumi.toml with custom content
let initial_config_content = r#"# Initial configuration
quiet = true
"#;
let config_path = tmp_dir_path.join("sumi.toml");
std::fs::write(&config_path, initial_config_content).unwrap();

// Attempt to initialize the default config, simulating 'no' to the overwrite prompt.
let mut cmd = Command::cargo_bin("git-sumi").unwrap();
cmd.current_dir(tmp_dir_path)
.arg("--init")
.write_stdin("n\n")
.assert()
.stdout(contains("Overwrite? (y/n) [n] "))
.success();

let file_contents = std::fs::read_to_string(config_path).unwrap();
assert_eq!(
file_contents, initial_config_content,
"The configuration file should not have been overwritten."
);
}

#[test]
fn success_config_overwrite_yes() {
let tmp_dir = tempfile::tempdir().unwrap();
let tmp_dir_path = tmp_dir.path();

let initial_config_content = r#"# Initial configuration
quiet = true
"#;
let config_path = tmp_dir_path.join("sumi.toml");
std::fs::write(&config_path, initial_config_content).unwrap();

// Attempt to initialize the default config, simulating 'yes' to the overwrite prompt.
let mut cmd = Command::cargo_bin("git-sumi").unwrap();
cmd.current_dir(tmp_dir_path)
.arg("--init")
.write_stdin("y\n")
.assert()
.stdout(contains("Overwrite? (y/n) [n] "))
.success();

let file_contents = std::fs::read_to_string(config_path).unwrap();
let expected_default_config = get_default_config().trim();

assert_eq!(
file_contents.trim(),
expected_default_config,
"The configuration file should have been overwritten with the default configuration."
);
}

fn init_tmp_git_repo() -> tempfile::TempDir {
let tmp_dir = tempfile::tempdir().unwrap();
let tmp_dir_path = tmp_dir.path();
Expand Down Expand Up @@ -480,3 +538,58 @@ fn error_init_hook_in_non_git_repo() {
.failure()
.stderr(contains("No .git directory found"));
}

#[test]
fn success_init_hook_overwrite_yes() {
let tmp_dir = init_tmp_git_repo();
let tmp_dir_path = tmp_dir.path();
let hooks_dir = tmp_dir_path.join(".git/hooks");
fs::create_dir_all(&hooks_dir).unwrap(); // Ensure the hooks directory exists.
let hook_path = hooks_dir.join("commit-msg");
fs::write(&hook_path, "#!/bin/sh\necho \"Existing hook\"").unwrap(); // Create a dummy hook.

let mut cmd = Command::cargo_bin("git-sumi").unwrap();
cmd.current_dir(tmp_dir_path)
.arg("--init")
.arg("hook")
.write_stdin("y\n") // Simulate user typing 'y' and pressing Enter.
.assert()
.success()
.stdout(contains("Overwrite? (y/n) [n]")); // Verify the prompt is displayed

let hook_content = fs::read_to_string(hook_path).expect("Failed to read hook file");
assert!(
hook_content.contains("Commit-msg hook generated by git-sumi"),
"The commit-msg hook does not contain the expected content."
);
}

#[test]
fn success_init_hook_overwrite_no() {
let tmp_dir = init_tmp_git_repo();
let tmp_dir_path = tmp_dir.path();
let hooks_dir = tmp_dir_path.join(".git/hooks");
fs::create_dir_all(&hooks_dir).unwrap();
let original_hook_content = "#!/bin/sh\necho \"Existing hook\"";
let hook_path = hooks_dir.join("commit-msg");
fs::write(&hook_path, original_hook_content).unwrap(); // Create a dummy hook.

let mut cmd = Command::cargo_bin("git-sumi").unwrap();
cmd.current_dir(tmp_dir_path)
.arg("--init")
.arg("hook")
.write_stdin("n\n") // Simulate user typing 'n' and pressing Enter.
.assert()
.success()
.stdout(contains("Overwrite? (y/n) [n] "));

let hook_content = fs::read_to_string(&hook_path).expect("Failed to read hook file");
assert_eq!(
hook_content, original_hook_content,
"The commit-msg hook content should remain unchanged."
);
assert!(
!hook_content.contains("Commit-msg hook generated by git-sumi"),
"The commit-msg hook should not contain the default content generated by git-sumi."
);
}

0 comments on commit 3562f23

Please sign in to comment.