From 6b7bc87634e156ff6c40308c07e030358e1411fe Mon Sep 17 00:00:00 2001 From: j178 <10510431+j178@users.noreply.github.com> Date: Tue, 3 Dec 2024 13:13:28 +0800 Subject: [PATCH] Fix tests --- .github/workflows/build-binaries.yml | 12 +++--- Cargo.toml | 6 +-- src/cli/hook_impl.rs | 2 +- src/cli/install.rs | 18 ++++---- src/cli/mod.rs | 8 ++-- src/cli/run.rs | 2 +- src/main.rs | 2 +- ...refligit__config__tests__read_config.snap} | 0 ...fligit__config__tests__read_manifest.snap} | 0 tests/common/mod.rs | 8 ++-- tests/hook_impl.rs | 2 +- tests/install.rs | 41 ++++++++----------- tests/run.rs | 2 +- 13 files changed, 47 insertions(+), 56 deletions(-) rename src/snapshots/{pre_commit__config__tests__read_config.snap => prefligit__config__tests__read_config.snap} (100%) rename src/snapshots/{pre_commit__config__tests__read_manifest.snap => prefligit__config__tests__read_manifest.snap} (100%) diff --git a/.github/workflows/build-binaries.yml b/.github/workflows/build-binaries.yml index f4b47a5..addb2b1 100644 --- a/.github/workflows/build-binaries.yml +++ b/.github/workflows/build-binaries.yml @@ -54,11 +54,11 @@ jobs: set -euo pipefail TARGET=${{ matrix.platform.target }} - ARCHIVE_NAME=pre-commit-rs-$TARGET + ARCHIVE_NAME=prefligit-$TARGET ARCHIVE_FILE=$ARCHIVE_NAME.tar.gz mkdir -p $ARCHIVE_NAME - cp target/$TARGET/release/pre-commit $ARCHIVE_NAME/pre-commit + cp target/$TARGET/release/prefligit $ARCHIVE_NAME/prefligit tar czvf $ARCHIVE_FILE $ARCHIVE_NAME shasum -a 256 $ARCHIVE_FILE > $ARCHIVE_FILE.sha256 - name: "Upload binary" @@ -92,8 +92,8 @@ jobs: - name: "Archive binary" shell: bash run: | - ARCHIVE_FILE=pre-commit-rs-${{ matrix.platform.target }}.zip - 7z a $ARCHIVE_FILE ./target/${{ matrix.platform.target }}/release/pre-commit.exe + ARCHIVE_FILE=prefligit-${{ matrix.platform.target }}.zip + 7z a $ARCHIVE_FILE ./target/${{ matrix.platform.target }}/release/prefligit.exe sha256sum $ARCHIVE_FILE > $ARCHIVE_FILE.sha256 - name: "Upload binary" uses: actions/upload-artifact@v4 @@ -128,11 +128,11 @@ jobs: - name: "Archive binary" run: | TARGET=${{ matrix.platform.target }} - ARCHIVE_NAME=pre-commit-rs-$TARGET + ARCHIVE_NAME=prefligit-$TARGET ARCHIVE_FILE=$ARCHIVE_NAME.tar.gz mkdir -p $ARCHIVE_NAME - cp target/$TARGET/release/pre-commit $ARCHIVE_NAME/pre-commit + cp target/$TARGET/release/prefligit $ARCHIVE_NAME/prefligit tar czvf $ARCHIVE_FILE $ARCHIVE_NAME shasum -a 256 $ARCHIVE_FILE > $ARCHIVE_FILE.sha256 - name: "Upload binary" diff --git a/Cargo.toml b/Cargo.toml index 0b83fc8..bb00347 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,10 +10,6 @@ license-file = "LICENSE" # `cargo publish` failed because of this, disabled for now. # include = ["licenses/.*"] -[[bin]] -path = "src/main.rs" -name = "prefligit" - [features] default = ["docker"] profiler = ["dep:pprof", "profiler-flamegraph"] @@ -143,7 +139,7 @@ publish-jobs = ["./publish", "homebrew"] # A GitHub repo to push Homebrew formulas to tap = "j178/homebrew-tap" # Customize the Homebrew formula name -formula = "pre-commit-rs" +formula = "prefligit" # Whether to install an updater program install-updater = false # Path that installers should place binaries in diff --git a/src/cli/hook_impl.rs b/src/cli/hook_impl.rs index 2dc7231..ca9b007 100644 --- a/src/cli/hook_impl.rs +++ b/src/cli/hook_impl.rs @@ -29,7 +29,7 @@ pub(crate) async fn hook_impl( "- To temporarily silence this, run `PRE_COMMIT_ALLOW_NO_CONFIG=1 git ...`" ); eprintln!("- To permanently silence this, install hooks with the `--allow-missing-config` flag"); - eprintln!("- To uninstall hooks, run `pre-commit uninstall`"); + eprintln!("- To uninstall hooks, run `prefligit uninstall`"); Ok(ExitStatus::Failure) }; } diff --git a/src/cli/install.rs b/src/cli/install.rs index 549d979..75a2c26 100644 --- a/src/cli/install.rs +++ b/src/cli/install.rs @@ -136,13 +136,13 @@ fn install_hook_script( args.push("--skip-on-missing-config".to_string()); } - let pre_commit = std::env::current_exe()?; - let pre_commit = pre_commit.simplified().display().to_string(); + let prefligit = std::env::current_exe()?; + let pre_commit = prefligit.simplified().display().to_string(); let hook_script = HOOK_TMPL .replace("ARGS=(hook-impl)", &format!("ARGS=({})", args.join(" "))) .replace( - r#"PRE_COMMIT="pre-commit""#, - &format!(r#"PRE_COMMIT="{pre_commit}""#), + r#"PREFLIGIT="prefligit""#, + &format!(r#"PREFLIGIT="{pre_commit}""#), ); fs_err::OpenOptions::new() .write(true) @@ -162,7 +162,7 @@ fn install_hook_script( writeln!( printer.stdout(), - "pre-commit installed at {}", + "prefligit installed at {}", hook_path.user_display().cyan() )?; @@ -178,9 +178,9 @@ ARGS=(hook-impl) HERE="$(cd "$(dirname "$0")" && pwd)" ARGS+=(--hook-dir "$HERE" -- "$@") -PRE_COMMIT="pre-commit" +PREFLIGIT="prefligit" -exec "$PRE_COMMIT" "${ARGS[@]}" +exec "$PREFLIGIT" "${ARGS[@]}" "# }; @@ -190,7 +190,7 @@ static PRIOR_HASHES: &[&str] = &[]; // Use a different hash from `pre-commit` since our script is different. static CURRENT_HASH: &str = "182c10f181da4464a3eec51b83331688"; -/// Checks if the script contains any of the hashes that `pre-commit` has used in the past. +/// Checks if the script contains any of the hashes that `prefligit` has used in the past. fn is_our_script(hook_path: &Path) -> Result { let content = fs_err::read_to_string(hook_path)?; Ok(std::iter::once(CURRENT_HASH) @@ -217,7 +217,7 @@ pub(crate) async fn uninstall( } else if !is_our_script(&hook_path)? { writeln!( printer.stderr(), - "{} is not managed by pre-commit, skipping.", + "{} is not managed by prefligit, skipping.", hook_path.user_display().cyan() )?; } else { diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 41597ba..8a6ed47 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -76,7 +76,7 @@ impl From for anstream::ColorChoice { #[derive(Parser)] #[command( - name = "pre-commit", + name = "prefligit", author, version, about = "pre-commit reimplemented in Rust" @@ -135,7 +135,7 @@ pub(crate) struct GlobalArgs { #[arg(global = true, short, long, action = ArgAction::Count)] pub(crate) verbose: u8, - /// Display the pre-commit version. + /// Display the prefligit version. #[arg(global = true, short = 'V', long, action = clap::ArgAction::Version)] version: Option, @@ -148,14 +148,14 @@ pub(crate) struct GlobalArgs { #[derive(Debug, Subcommand)] pub(crate) enum Command { - /// Install the git pre-commit hook. + /// Install the git prefligit hook. #[command(name = "install")] Install(InstallArgs), /// Create hook environments for all hooks used in the config file. InstallHooks, /// Run hooks. Run(Box), - /// Uninstall the pre-commit script. + /// Uninstall the prefligit script. Uninstall(UninstallArgs), /// Validate `.pre-commit-config.yaml` files. ValidateConfig(ValidateConfigArgs), diff --git a/src/cli/run.rs b/src/cli/run.rs index d5ad4df..32f4ba8 100644 --- a/src/cli/run.rs +++ b/src/cli/run.rs @@ -46,7 +46,7 @@ pub(crate) async fn run( if should_stash && git::has_unmerged_paths().await? { writeln!( printer.stderr(), - "You have unmerged paths. Resolve them before running pre-commit." + "You have unmerged paths. Resolve them before running prefligit." )?; return Ok(ExitStatus::Failure); } diff --git a/src/main.rs b/src/main.rs index 7580b77..1f5c1fa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -128,7 +128,7 @@ async fn run(mut cli: Cli) -> Result { cli.command = Some(Command::Run(Box::new(cli.run_args.clone()))); } - debug!("pre-commit: {}", env!("CARGO_PKG_VERSION")); + debug!("prefligit: {}", env!("CARGO_PKG_VERSION")); match get_root().await { Ok(root) => { diff --git a/src/snapshots/pre_commit__config__tests__read_config.snap b/src/snapshots/prefligit__config__tests__read_config.snap similarity index 100% rename from src/snapshots/pre_commit__config__tests__read_config.snap rename to src/snapshots/prefligit__config__tests__read_config.snap diff --git a/src/snapshots/pre_commit__config__tests__read_manifest.snap b/src/snapshots/prefligit__config__tests__read_manifest.snap similarity index 100% rename from src/snapshots/pre_commit__config__tests__read_manifest.snap rename to src/snapshots/prefligit__config__tests__read_manifest.snap diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 943f79f..dc7eb73 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -47,7 +47,7 @@ impl TestContext { .map(|pattern| (pattern, "[HOME]/".to_string())), ); - let current_exe = assert_cmd::cargo::cargo_bin("pre-commit"); + let current_exe = assert_cmd::cargo::cargo_bin("prefligit"); filters.extend( Self::path_patterns(¤t_exe) .into_iter() @@ -112,7 +112,7 @@ impl TestContext { } pub fn command(&self) -> Command { - let bin = assert_cmd::cargo::cargo_bin("pre-commit"); + let bin = assert_cmd::cargo::cargo_bin("prefligit"); let mut cmd = Command::new(bin); cmd.current_dir(self.workdir()); cmd.env("PRE_COMMIT_HOME", &*self.home_dir); @@ -177,7 +177,7 @@ impl TestContext { &self.temp_dir } - /// Initialize a sample project for pre-commit. + /// Initialize a sample project for prefligit. pub fn init_project(&self) { Command::new("git") .arg("init") @@ -241,7 +241,7 @@ pub const INSTA_FILTERS: &[(&str, &str)] = &[ (r"(\s|\()(\d+\.)?\d+([KM]i)?B", "$1[SIZE]"), // Rewrite Windows output to Unix output (r"\\([\w\d]|\.\.)", "/$1"), - (r"pre-commit.exe", "pre-commit"), + (r"prefligit.exe", "prefligit"), // The exact message is host language dependent ( r"Caused by: .* \(os error 2\)", diff --git a/tests/hook_impl.rs b/tests/hook_impl.rs index 77f468b..b52b42e 100644 --- a/tests/hook_impl.rs +++ b/tests/hook_impl.rs @@ -37,7 +37,7 @@ fn hook_impl() { success: true exit_code: 0 ----- stdout ----- - pre-commit installed at .git/hooks/pre-commit + prefligit installed at .git/hooks/pre-commit ----- stderr ----- "#); diff --git a/tests/install.rs b/tests/install.rs index 88f6fa0..3d87e57 100644 --- a/tests/install.rs +++ b/tests/install.rs @@ -13,12 +13,12 @@ fn install() -> anyhow::Result<()> { let context = TestContext::new(); context.init_project(); - // Install `pre-commit` hook. + // Install `prefligit` hook. cmd_snapshot!(context.filters(), context.install(), @r#" success: true exit_code: 0 ----- stdout ----- - pre-commit installed at .git/hooks/pre-commit + prefligit installed at .git/hooks/pre-commit ----- stderr ----- "#); @@ -35,10 +35,9 @@ fn install() -> anyhow::Result<()> { HERE="$(cd "$(dirname "$0")" && pwd)" ARGS+=(--hook-dir "$HERE" -- "$@") - PRE_COMMIT="[CURRENT_EXE]" - - exec "$PRE_COMMIT" "${ARGS[@]}" + PREFLIGIT="[CURRENT_EXE]" + exec "$PREFLIGIT" "${ARGS[@]}" "##); } ); @@ -54,8 +53,8 @@ fn install() -> anyhow::Result<()> { exit_code: 0 ----- stdout ----- Hook already exists at .git/hooks/pre-commit, move it to .git/hooks/pre-commit.legacy. - pre-commit installed at .git/hooks/pre-commit - pre-commit installed at .git/hooks/post-commit + prefligit installed at .git/hooks/pre-commit + prefligit installed at .git/hooks/post-commit ----- stderr ----- "#); @@ -71,10 +70,9 @@ fn install() -> anyhow::Result<()> { HERE="$(cd "$(dirname "$0")" && pwd)" ARGS+=(--hook-dir "$HERE" -- "$@") - PRE_COMMIT="[CURRENT_EXE]" - - exec "$PRE_COMMIT" "${ARGS[@]}" + PREFLIGIT="[CURRENT_EXE]" + exec "$PREFLIGIT" "${ARGS[@]}" "##); } ); @@ -96,10 +94,9 @@ fn install() -> anyhow::Result<()> { HERE="$(cd "$(dirname "$0")" && pwd)" ARGS+=(--hook-dir "$HERE" -- "$@") - PRE_COMMIT="[CURRENT_EXE]" - - exec "$PRE_COMMIT" "${ARGS[@]}" + PREFLIGIT="[CURRENT_EXE]" + exec "$PREFLIGIT" "${ARGS[@]}" "##); } ); @@ -110,9 +107,9 @@ fn install() -> anyhow::Result<()> { exit_code: 0 ----- stdout ----- Overwriting existing hook at .git/hooks/pre-commit - pre-commit installed at .git/hooks/pre-commit + prefligit installed at .git/hooks/pre-commit Overwriting existing hook at .git/hooks/post-commit - pre-commit installed at .git/hooks/post-commit + prefligit installed at .git/hooks/post-commit ----- stderr ----- "#); @@ -129,10 +126,9 @@ fn install() -> anyhow::Result<()> { HERE="$(cd "$(dirname "$0")" && pwd)" ARGS+=(--hook-dir "$HERE" -- "$@") - PRE_COMMIT="[CURRENT_EXE]" - - exec "$PRE_COMMIT" "${ARGS[@]}" + PREFLIGIT="[CURRENT_EXE]" + exec "$PREFLIGIT" "${ARGS[@]}" "##); } ); @@ -148,10 +144,9 @@ fn install() -> anyhow::Result<()> { HERE="$(cd "$(dirname "$0")" && pwd)" ARGS+=(--hook-dir "$HERE" -- "$@") - PRE_COMMIT="[CURRENT_EXE]" - - exec "$PRE_COMMIT" "${ARGS[@]}" + PREFLIGIT="[CURRENT_EXE]" + exec "$PREFLIGIT" "${ARGS[@]}" "##); } ); @@ -201,7 +196,7 @@ fn uninstall() -> anyhow::Result<()> { ----- stdout ----- ----- stderr ----- - .git/hooks/pre-commit is not managed by pre-commit, skipping. + .git/hooks/pre-commit is not managed by prefligit, skipping. "#); // Restore previous hook. @@ -248,7 +243,7 @@ fn init_template_dir() { success: true exit_code: 0 ----- stdout ----- - pre-commit installed at .git/hooks/pre-commit + prefligit installed at .git/hooks/pre-commit ----- stderr ----- `init.templateDir` not set to the target directory diff --git a/tests/run.rs b/tests/run.rs index 1f79a26..61bd2e4 100644 --- a/tests/run.rs +++ b/tests/run.rs @@ -778,7 +778,7 @@ fn merge_conflicts() -> Result<()> { ----- stdout ----- ----- stderr ----- - You have unmerged paths. Resolve them before running pre-commit. + You have unmerged paths. Resolve them before running prefligit. "#); // Fix the conflict and run again.