diff --git a/src/bin/git-stack/alias.rs b/src/bin/git-stack/alias.rs index 2cfbb6d..5f46510 100644 --- a/src/bin/git-stack/alias.rs +++ b/src/bin/git-stack/alias.rs @@ -36,7 +36,7 @@ fn register(_colored_stdout: bool, colored_stderr: bool) -> proc_exit::ExitResul let mut user_config = git2::Config::open_default() .with_code(proc_exit::Code::FAILURE)? - .open_level(git2::ConfigLevel::XDG) + .open_global() .with_code(proc_exit::Code::FAILURE)?; let stderr_palette = if colored_stderr { @@ -105,7 +105,7 @@ fn unregister(_colored_stdout: bool, colored_stderr: bool) -> proc_exit::ExitRes let mut user_config = git2::Config::open_default() .with_code(proc_exit::Code::FAILURE)? - .open_level(git2::ConfigLevel::XDG) + .open_global() .with_code(proc_exit::Code::FAILURE)?; let stderr_palette = if colored_stderr { @@ -216,7 +216,7 @@ fn status(colored_stdout: bool, colored_stderr: bool) -> proc_exit::ExitResult { .paint(format_args!(" {} = {}", name, value)), stdout_palette .hint - .paint(format_args!(" # instead of \"{}\"", alias.action)) + .paint(format_args!(" # instead of `{}`", alias.action)) ); } covered.insert(name.to_owned()); diff --git a/tests/testsuite/alias.rs b/tests/testsuite/alias.rs new file mode 100644 index 0000000..df7e8b0 --- /dev/null +++ b/tests/testsuite/alias.rs @@ -0,0 +1,373 @@ +// Not correctly overriding on Windows +#![cfg(target_os = "linux")] + +#[test] +fn list_no_config() { + let root = snapbox::path::PathFixture::mutable_temp().unwrap(); + let root_path = root.path().unwrap(); + + let home_root = root_path.join("home"); + std::fs::create_dir_all(&home_root).unwrap(); + + let repo_root = root_path.join("repo"); + git2::Repository::init(&repo_root).unwrap(); + + snapbox::cmd::Command::new(snapbox::cmd::cargo_bin!("git-stack")) + .arg("alias") + .current_dir(&repo_root) + .env("HOME", &home_root) + .assert() + .success() + .stdout_eq( + "\ +[alias] +# next = stack next # unregistered +# prev = stack previous # unregistered +# reword = stack reword # unregistered +# amend = stack amend # unregistered +# sync = stack sync # unregistered +# run = stack run # unregistered +", + ) + .stderr_matches( + "\ +note: To register, pass `--register` +", + ); + + root.close().unwrap(); +} + +#[test] +fn list_global_config() { + let root = snapbox::path::PathFixture::mutable_temp().unwrap(); + let root_path = root.path().unwrap(); + + let home_root = root_path.join("home"); + std::fs::create_dir_all(&home_root).unwrap(); + std::fs::write( + home_root.join(".gitconfig"), + " +[alias] + next = foo +", + ) + .unwrap(); + + let repo_root = root_path.join("repo"); + git2::Repository::init(&repo_root).unwrap(); + + snapbox::cmd::Command::new(snapbox::cmd::cargo_bin!("git-stack")) + .arg("alias") + .current_dir(&repo_root) + .env("HOME", &home_root) + .assert() + .success() + .stdout_eq( + "\ +[alias] + next = foo # instead of `stack next` +# prev = stack previous # unregistered +# reword = stack reword # unregistered +# amend = stack amend # unregistered +# sync = stack sync # unregistered +# run = stack run # unregistered +", + ) + .stderr_matches( + "\ +note: To register, pass `--register` +", + ); + + root.close().unwrap(); +} + +#[test] +fn register_no_config() { + let root = snapbox::path::PathFixture::mutable_temp().unwrap(); + let root_path = root.path().unwrap(); + + let home_root = root_path.join("home"); + std::fs::create_dir_all(&home_root).unwrap(); + + let repo_root = root_path.join("repo"); + git2::Repository::init(&repo_root).unwrap(); + + snapbox::cmd::Command::new(snapbox::cmd::cargo_bin!("git-stack")) + .arg("alias") + .arg("--register") + .current_dir(&repo_root) + .env("HOME", &home_root) + .assert() + .success() + .stdout_eq( + "\ +", + ) + .stderr_matches( + r#"Registering: next="stack next" +Registering: prev="stack previous" +Registering: reword="stack reword" +Registering: amend="stack amend" +Registering: sync="stack sync" +Registering: run="stack run" +"#, + ); + + snapbox::cmd::Command::new(snapbox::cmd::cargo_bin!("git-stack")) + .arg("alias") + .current_dir(&repo_root) + .env("HOME", &home_root) + .assert() + .success() + .stdout_eq( + "\ +[alias] + next = stack next # registered + prev = stack previous # registered + reword = stack reword # registered + amend = stack amend # registered + sync = stack sync # registered + run = stack run # registered +", + ) + .stderr_matches( + "\ +note: To unregister, pass `--unregister` +", + ); + + root.close().unwrap(); +} + +#[test] +fn register_no_overwrite_alias() { + let root = snapbox::path::PathFixture::mutable_temp().unwrap(); + let root_path = root.path().unwrap(); + + let home_root = root_path.join("home"); + std::fs::create_dir_all(&home_root).unwrap(); + std::fs::write( + home_root.join(".gitconfig"), + " +[alias] + next = foo + prev = stack previous -v +", + ) + .unwrap(); + + let repo_root = root_path.join("repo"); + git2::Repository::init(&repo_root).unwrap(); + + snapbox::cmd::Command::new(snapbox::cmd::cargo_bin!("git-stack")) + .arg("alias") + .arg("--register") + .current_dir(&repo_root) + .env("HOME", &home_root) + .assert() + .failure() + .stdout_eq( + "\ +", + ) + .stderr_matches( + r#"error: next="foo" is registered, not overwriting with "stack next" +Registering: reword="stack reword" +Registering: amend="stack amend" +Registering: sync="stack sync" +Registering: run="stack run" +"#, + ); + + snapbox::cmd::Command::new(snapbox::cmd::cargo_bin!("git-stack")) + .arg("alias") + .current_dir(&repo_root) + .env("HOME", &home_root) + .assert() + .success() + .stdout_eq( + r#"[alias] + next = foo # instead of `stack next` + prev = stack previous -v # diverged from "stack previous" + reword = stack reword # registered + amend = stack amend # registered + sync = stack sync # registered + run = stack run # registered +"#, + ) + .stderr_matches( + "\ +note: To unregister, pass `--unregister` +", + ); + + root.close().unwrap(); +} + +#[test] +fn register_unregister() { + let root = snapbox::path::PathFixture::mutable_temp().unwrap(); + let root_path = root.path().unwrap(); + + let home_root = root_path.join("home"); + std::fs::create_dir_all(&home_root).unwrap(); + + let repo_root = root_path.join("repo"); + git2::Repository::init(&repo_root).unwrap(); + + snapbox::cmd::Command::new(snapbox::cmd::cargo_bin!("git-stack")) + .arg("alias") + .arg("--register") + .current_dir(&repo_root) + .env("HOME", &home_root) + .assert() + .success(); + + snapbox::cmd::Command::new(snapbox::cmd::cargo_bin!("git-stack")) + .arg("alias") + .arg("--unregister") + .current_dir(&repo_root) + .env("HOME", &home_root) + .assert() + .success() + .stdout_matches("") + .stderr_matches( + r#"Unregistering: next="stack next" +Unregistering: prev="stack previous" +Unregistering: reword="stack reword" +Unregistering: amend="stack amend" +Unregistering: sync="stack sync" +Unregistering: run="stack run" +"#, + ); + + root.close().unwrap(); +} + +#[test] +fn reregister() { + let root = snapbox::path::PathFixture::mutable_temp().unwrap(); + let root_path = root.path().unwrap(); + + let home_root = root_path.join("home"); + std::fs::create_dir_all(&home_root).unwrap(); + + let repo_root = root_path.join("repo"); + git2::Repository::init(&repo_root).unwrap(); + + snapbox::cmd::Command::new(snapbox::cmd::cargo_bin!("git-stack")) + .arg("alias") + .arg("--register") + .current_dir(&repo_root) + .env("HOME", &home_root) + .assert() + .success(); + + snapbox::cmd::Command::new(snapbox::cmd::cargo_bin!("git-stack")) + .arg("alias") + .arg("--register") + .current_dir(&repo_root) + .env("HOME", &home_root) + .assert() + .success() + .stdout_eq( + "\ +", + ) + .stderr_matches(r#""#); + + root.close().unwrap(); +} + +#[test] +fn unregister_no_config() { + let root = snapbox::path::PathFixture::mutable_temp().unwrap(); + let root_path = root.path().unwrap(); + + let home_root = root_path.join("home"); + std::fs::create_dir_all(&home_root).unwrap(); + + let repo_root = root_path.join("repo"); + git2::Repository::init(&repo_root).unwrap(); + + snapbox::cmd::Command::new(snapbox::cmd::cargo_bin!("git-stack")) + .arg("alias") + .arg("--unregister") + .current_dir(&repo_root) + .env("HOME", &home_root) + .assert() + .success() + .stdout_eq( + "\ +", + ) + .stderr_matches(r#""#); + + root.close().unwrap(); +} + +#[test] +fn unregister_existing_config() { + let root = snapbox::path::PathFixture::mutable_temp().unwrap(); + let root_path = root.path().unwrap(); + + let home_root = root_path.join("home"); + std::fs::create_dir_all(&home_root).unwrap(); + std::fs::write( + home_root.join(".gitconfig"), + " +[alias] + next = foo + prev = stack previous -v + reword = stack reword +", + ) + .unwrap(); + + let repo_root = root_path.join("repo"); + git2::Repository::init(&repo_root).unwrap(); + + snapbox::cmd::Command::new(snapbox::cmd::cargo_bin!("git-stack")) + .arg("alias") + .arg("--unregister") + .current_dir(&repo_root) + .env("HOME", &home_root) + .assert() + .success() + .stdout_eq( + "\ +", + ) + .stderr_matches( + r#"Unregistering: prev="stack previous -v" +Unregistering: reword="stack reword" +"#, + ); + + snapbox::cmd::Command::new(snapbox::cmd::cargo_bin!("git-stack")) + .arg("alias") + .current_dir(&repo_root) + .env("HOME", &home_root) + .assert() + .success() + .stdout_eq( + "\ +[alias] + next = foo # instead of `stack next` +# prev = stack previous # unregistered +# reword = stack reword # unregistered +# amend = stack amend # unregistered +# sync = stack sync # unregistered +# run = stack run # unregistered +", + ) + .stderr_matches( + "\ +note: To register, pass `--register` +", + ); + + root.close().unwrap(); +} diff --git a/tests/testsuite/main.rs b/tests/testsuite/main.rs index 3f50d41..5701e8b 100644 --- a/tests/testsuite/main.rs +++ b/tests/testsuite/main.rs @@ -1,3 +1,4 @@ +mod alias; mod amend; mod branches; mod fixture;