Skip to content

Commit

Permalink
test(integration): add coverage for hook
Browse files Browse the repository at this point in the history
  • Loading branch information
cococonscious committed Nov 29, 2024
1 parent ba47427 commit 49c525b
Showing 1 changed file with 105 additions and 9 deletions.
114 changes: 105 additions & 9 deletions tests/integration.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use git2::{Commit, Repository, RepositoryInitOptions};
use rexpect::session::PtySession;
#[cfg(not(target_os = "windows"))]
use rexpect::{process::wait, session::spawn_command};
use std::{error::Error, fs, path::PathBuf, process::Command};
Expand All @@ -22,9 +23,54 @@ fn get_first_commit(repo: &Repository) -> Result<Commit, git2::Error> {
repo.find_commit(oid)
}

trait ExpectPromps {
fn expect_commit_type(&mut self) -> Result<String, rexpect::error::Error>;
fn expect_scope(&mut self) -> Result<String, rexpect::error::Error>;
fn expect_summary(&mut self) -> Result<String, rexpect::error::Error>;
fn expect_body(&mut self) -> Result<String, rexpect::error::Error>;
fn expect_breaking(&mut self) -> Result<String, rexpect::error::Error>;
fn expect_breaking_details(&mut self) -> Result<String, rexpect::error::Error>;
fn expect_issues(&mut self) -> Result<String, rexpect::error::Error>;
fn expect_issues_details(&mut self) -> Result<String, rexpect::error::Error>;
}

impl ExpectPromps for PtySession {
fn expect_commit_type(&mut self) -> Result<String, rexpect::error::Error> {
self.exp_string("are you committing?")
}

fn expect_scope(&mut self) -> Result<String, rexpect::error::Error> {
self.exp_string("scope of this change?")
}

fn expect_summary(&mut self) -> Result<String, rexpect::error::Error> {
self.exp_string("description of the change")
}

fn expect_body(&mut self) -> Result<String, rexpect::error::Error> {
self.exp_string("longer description of the change")
}

fn expect_breaking(&mut self) -> Result<String, rexpect::error::Error> {
self.exp_string("breaking changes?")
}

fn expect_breaking_details(&mut self) -> Result<String, rexpect::error::Error> {
self.exp_string("in detail:")
}

fn expect_issues(&mut self) -> Result<String, rexpect::error::Error> {
self.exp_string("any open issues?")
}

fn expect_issues_details(&mut self) -> Result<String, rexpect::error::Error> {
self.exp_string("issue reference:")
}
}

#[test]
#[cfg(not(target_os = "windows"))]
fn test_type_scope_summary_body_breaking_issue_add_files_correct() -> Result<(), Box<dyn Error>> {
fn test_everything_correct() -> Result<(), Box<dyn Error>> {
let (bin_path, temp_dir, repo) = setup_test_dir()?;

fs::write(temp_dir.path().join("config.json"), "abc")?;
Expand All @@ -33,26 +79,27 @@ fn test_type_scope_summary_body_breaking_issue_add_files_correct() -> Result<(),
cmd.env("NO_COLOR", "1")
.arg("-C")
.arg(temp_dir.path())
.arg("-a");
.arg("-a")
.arg("--autocomplete=true");

let mut process = spawn_command(cmd, Some(5000))?;

process.exp_string("are you committing?")?;
process.expect_commit_type()?;
process.send_line("feat")?;
process.exp_string("scope of this change?")?;
process.expect_scope()?;
process.send_line("config")?;
process.exp_string("description of the change")?;
process.send_line("refactor config pairs")?;
process.exp_string("longer description of the change")?;
process.expect_body()?;
process
.send_line("Removed and added a config pair each\\nNecessary for future compatibility.")?;
process.exp_string("breaking changes?")?;
process.expect_breaking()?;
process.send_line("Y")?;
process.exp_string("in detail:")?;
process.expect_breaking_details()?;
process.send_line("Something can't be configured anymore")?;
process.exp_string("any open issues?")?;
process.expect_issues()?;
process.send_line("Y")?;
process.exp_string("issue reference:")?;
process.expect_issues_details()?;
process.send_line("closes #1")?;
let eof_output = process.exp_eof();

Expand All @@ -76,3 +123,52 @@ fn test_type_scope_summary_body_breaking_issue_add_files_correct() -> Result<(),
temp_dir.close()?;
Ok(())
}

#[test]
#[cfg(not(target_os = "windows"))]
fn test_hook_correct() -> Result<(), Box<dyn Error>> {
let (bin_path, temp_dir, _) = setup_test_dir()?;

fs::write(temp_dir.path().join("config.json"), "abc")?;
fs::remove_file(temp_dir.path().join(".git").join("COMMIT_EDITMSG")).unwrap_or(());

let mut cmd = Command::new(bin_path);
cmd.env("NO_COLOR", "1")
.arg("-C")
.arg(temp_dir.path())
.arg("-a")
.arg("--autocomplete=true");

let mut process = spawn_command(cmd, Some(5000))?;

process.expect_commit_type()?;
process.send_line("fix")?;
process.expect_scope()?;
process.send_line("")?;
process.expect_summary()?;
process.send_line("some weird error")?;
process.expect_body()?;
process.send_line("")?;
process.expect_breaking()?;
process.send_line("N")?;
process.expect_issues()?;
process.send_line("N")?;
let eof_output = process.exp_eof();

let exitcode = process.process.wait()?;
let success = matches!(exitcode, wait::WaitStatus::Exited(_, 0));

if !success {
panic!("Command exited non-zero, end of output: {:?}", eof_output);
}

let editmsg = temp_dir.path().join(".git").join("COMMIT_EDITMSG");
assert!(editmsg.exists());
assert_eq!(
fs::read(editmsg)?,
"fix: some weird error".as_bytes().to_vec()
);

temp_dir.close()?;
Ok(())
}

0 comments on commit 49c525b

Please sign in to comment.