cuddle-please/crates/cuddle-please/tests/git.rs
kjuulh 34417f77bc
Some checks failed
continuous-integration/drone/push Build is failing
feat: with working main
Signed-off-by: kjuulh <contact@kjuulh.io>
2023-08-03 23:31:53 +02:00

192 lines
5.3 KiB
Rust

use std::path::Path;
use cuddle_please_misc::VcsClient;
use pretty_assertions::assert_eq;
use tracing_test::traced_test;
#[test]
#[traced_test]
fn exec_git_into_branch() {
let tempdir = tempdir::TempDir::new("exec_git_into_branch").unwrap();
setup_git(tempdir.path()).unwrap();
add_commit(tempdir.path(), "first").unwrap();
add_tag(tempdir.path(), "1.0.0").unwrap();
add_commit(tempdir.path(), "second").unwrap();
add_tag(tempdir.path(), "1.0.1").unwrap();
let vcs =
VcsClient::new_git(tempdir.path(), None::<String>, None::<String>, "".into()).unwrap();
vcs.checkout_branch().unwrap();
let output = std::process::Command::new("git")
.arg("branch")
.current_dir(tempdir.path())
.output()
.unwrap();
let stdout = std::str::from_utf8(&output.stdout).unwrap();
let stderr = std::str::from_utf8(&output.stderr).unwrap();
assert_eq!(
"* cuddle-please/release
main
",
stdout
);
assert_eq!("", stderr);
}
#[test]
#[traced_test]
fn add_files_to_commit() {
let tempdir = tempdir::TempDir::new("add_files_to_commit").unwrap();
setup_git(tempdir.path()).unwrap();
add_commit(tempdir.path(), "first").unwrap();
add_tag(tempdir.path(), "1.0.0").unwrap();
let vcs =
VcsClient::new_git(tempdir.path(), None::<String>, None::<String>, "".into()).unwrap();
vcs.checkout_branch().unwrap();
std::fs::File::create(tempdir.path().join("changelog")).unwrap();
vcs.commit_and_push("with some file", true).unwrap();
let output = std::process::Command::new("git")
.arg("log")
.current_dir(tempdir.path())
.output()
.unwrap();
let stdout = std::str::from_utf8(&output.stdout).unwrap();
let stderr = std::str::from_utf8(&output.stderr).unwrap();
assert!(stdout.contains("chore(release): with some file"));
assert_eq!("", stderr);
}
#[test]
#[traced_test]
fn reset_branch() {
let tempdir = tempdir::TempDir::new("add_files_to_commit").unwrap();
setup_git(tempdir.path()).unwrap();
add_commit(tempdir.path(), "first").unwrap();
add_tag(tempdir.path(), "1.0.0").unwrap();
let vcs =
VcsClient::new_git(tempdir.path(), None::<String>, None::<String>, "".into()).unwrap();
vcs.checkout_branch().unwrap();
std::fs::File::create(tempdir.path().join("changelog-first")).unwrap();
vcs.commit_and_push("v1.0.0", true).unwrap();
exec_git(tempdir.path(), &["checkout", "main"]).unwrap();
vcs.checkout_branch().unwrap();
std::fs::File::create(tempdir.path().join("changelog-second")).unwrap();
vcs.commit_and_push("v1.0.1", true).unwrap();
let output = std::process::Command::new("git")
.arg("log")
.current_dir(tempdir.path())
.output()
.unwrap();
let stdout = std::str::from_utf8(&output.stdout).unwrap();
let stderr = std::str::from_utf8(&output.stderr).unwrap();
assert!(stdout.contains("chore(release): v1.0.1"));
assert!(!stdout.contains("chore(release): v1.0.0"));
assert_eq!("", stderr);
}
fn add_tag(path: &Path, arg: &str) -> anyhow::Result<()> {
add_everything(path)?;
let output = std::process::Command::new("git")
.arg("tag")
.arg("-a")
.arg(arg)
.arg("-m")
.arg(arg)
.current_dir(path)
.output()?;
let stdout = std::str::from_utf8(&output.stdout)?;
let stderr = std::str::from_utf8(&output.stderr)?;
tracing::debug!(stdout = stdout, stderr = stderr, "git init");
Ok(())
}
fn add_commit(path: &Path, arg: &str) -> anyhow::Result<()> {
std::fs::File::create(path.join(arg))?;
add_everything(path)?;
let output = std::process::Command::new("git")
.arg("commit")
.arg("-m")
.arg(arg)
.current_dir(path)
.output()?;
let stdout = std::str::from_utf8(&output.stdout)?;
let stderr = std::str::from_utf8(&output.stderr)?;
tracing::debug!(stdout = stdout, stderr = stderr, "git init");
Ok(())
}
fn add_everything(path: &Path) -> anyhow::Result<()> {
let output = std::process::Command::new("git")
.arg("add")
.arg(".")
.current_dir(path)
.output()?;
let stdout = std::str::from_utf8(&output.stdout)?;
let stderr = std::str::from_utf8(&output.stderr)?;
tracing::debug!(stdout = stdout, stderr = stderr, "git add .");
Ok(())
}
fn setup_git(path: &Path) -> anyhow::Result<()> {
let output = std::process::Command::new("git")
.arg("init")
.arg(".")
.current_dir(path)
.output()?;
let stdout = std::str::from_utf8(&output.stdout)?;
let stderr = std::str::from_utf8(&output.stderr)?;
tracing::debug!(stdout = stdout, stderr = stderr, "git init");
exec_git(path, &["checkout", "-b", "main"])?;
exec_git(path, &["config", "user.name", "test"])?;
exec_git(path, &["config", "user.email", "test@test.com"])?;
exec_git(path, &["config", "init.defaultBranch", "main"])?;
Ok(())
}
fn exec_git(path: &Path, args: &[&str]) -> anyhow::Result<()> {
let checkout_branch = std::process::Command::new("git")
.current_dir(path)
.args(args)
.output()?;
let stdout = std::str::from_utf8(&checkout_branch.stdout)?;
let stderr = std::str::from_utf8(&checkout_branch.stderr)?;
tracing::debug!(stdout = stdout, stderr = stderr, "git {}", args.join(" "));
Ok(())
}