From cc5fd57780595d3ec73dc4e0e884d4099e319878 Mon Sep 17 00:00:00 2001 From: Justin Wernick Date: Mon, 10 Apr 2023 20:25:45 +0200 Subject: Tests for setting the repo's branch --- tests/cli.rs | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/tests/cli.rs b/tests/cli.rs index 0e3ee58..320b4d6 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -98,6 +98,16 @@ fn verify_repo_exists(repo_dir: &Path) { .stdout(""); } +fn verify_current_branch(repo_dir: &Path, expected_ref: &str) { + Command::new("git") + .arg("symbolic-ref") + .arg("HEAD") + .current_dir(repo_dir) + .assert() + .success() + .stdout(format!("{expected_ref}\n")); +} + fn verify_repo_config_value(repo_dir: &Path, config_key: &str, config_value: Option<&str>) { let assert = Command::new("git") .args(["config", "--local", config_key]) @@ -105,7 +115,7 @@ fn verify_repo_config_value(repo_dir: &Path, config_key: &str, config_value: Opt .assert(); match config_value { Some(value) => { - assert.success().stdout(format!("{}\n", value)); + assert.success().stdout(format!("{value}\n")); } None => { assert.failure().code(1); @@ -132,6 +142,7 @@ fn can_init_a_new_git_repo() -> Result<()> { .join(username) .join(&format!("{}.git", repo_name)); verify_repo_exists(&repo_dir); + verify_current_branch(&repo_dir, "refs/heads/main"); verify_repo_config_value(&repo_dir, "core.sharedrepository", None); Ok(()) @@ -346,3 +357,54 @@ fn can_change_the_description_on_a_repo() -> Result<()> { Ok(()) } + +#[test] +fn can_set_the_main_branch_of_a_new_git_repo() -> Result<()> { + let mut c = spawn_interactive_process()?; + let username = get_username().unwrap(); + let repo_name = "my-new-repo"; + let main_branch = "foobar"; + c.p.send_line(&format!("init --branch {} {}", main_branch, repo_name))?; + c.p.exp_string(&format!( + "Successfully created \"git/{}/{}.git\"", + username, repo_name + ))?; + + let repo_dir = c + .workdir + .as_ref() + .join("git") + .join(username) + .join(&format!("{}.git", repo_name)); + verify_current_branch(&repo_dir, &format!("refs/heads/{main_branch}")); + + Ok(()) +} + +#[test] +fn can_change_the_main_branch_on_a_repo() -> Result<()> { + let mut c = spawn_interactive_process()?; + let username = get_username().unwrap(); + let repo_name = "my-new-repo"; + let main_branch = "foobar"; + c.p.send_line(&format!("init {}", repo_name))?; + c.p.exp_string(&format!( + "Successfully created \"git/{}/{}.git\"", + username, repo_name + ))?; + + c.p.send_line(&format!( + "set-branch \"git/{username}/{repo_name}.git\" \"{main_branch}\"" + ))?; + c.p.exp_string("Successfully updated branch")?; + + let repo_dir = c + .workdir + .as_ref() + .join("git") + .join(username) + .join(&format!("{}.git", repo_name)); + verify_current_branch(&repo_dir, &format!("refs/heads/{main_branch}")); + + Ok(()) +} -- cgit v1.2.3