Skip to content

Conversation

Bogay
Copy link
Contributor

@Bogay Bogay commented Sep 7, 2022

Description

This PR make cz report errors if user want to install pre-commit hook and failed, which fixs #428.

Checklist

  • Add test cases to all the changes you introduce
  • Run ./scripts/format and ./scripts/test locally to ensure this change passes linter check and test
  • Test the changes on the local machine manually
  • Update the documentation for the changes

Expected behavior

Run cz init will print error if there is something wrong.

❯ cz init
? Please choose a supported config file: (default: pyproject.toml) pyproject.toml
? Please choose a cz (commit rule): (default: cz_conventional_commits) cz_conventional_commits
No Existing Tag. Set tag to v0.0.1
? Please enter the correct version format: (default: "$version")
? Do you want to install pre-commit hook? Yes
pre-commit is not installed in current environement.
Installation failed. See error outputs for more information.

And the exit code should not be 0.

❯ echo $?
24

Steps to Test This Pull Request

# Run `cz init` with default choices
cz init
# Check the exit code should not be 0
test $? != 0

Additional context

I found that TestPreCommitCases has some duplicated code. Should they be extracted to methods?

@codecov
Copy link

codecov bot commented Sep 7, 2022

Codecov Report

Base: 97.92% // Head: 98.03% // Increases project coverage by +0.10% 🎉

Coverage data is based on head (a23ef07) compared to base (db42a95).
Patch coverage: 85.96% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #578      +/-   ##
==========================================
+ Coverage   97.92%   98.03%   +0.10%     
==========================================
  Files          35       39       +4     
  Lines        1252     1676     +424     
==========================================
+ Hits         1226     1643     +417     
- Misses         26       33       +7     
Flag Coverage Δ
unittests 98.03% <85.96%> (+0.10%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
commitizen/commands/init.py 87.93% <84.00%> (-3.74%) ⬇️
commitizen/__version__.py 100.00% <100.00%> (ø)
commitizen/commands/changelog.py 98.86% <100.00%> (ø)
commitizen/commands/check.py 100.00% <100.00%> (ø)
commitizen/exceptions.py 100.00% <100.00%> (ø)
commitizen/bump.py 100.00% <0.00%> (ø)
commitizen/__init__.py 100.00% <0.00%> (ø)
commitizen/changelog.py 99.43% <0.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@Lee-W
Copy link
Member

Lee-W commented Sep 10, 2022

I found that TestPreCommitCases has some duplicated code. Should they be extracted to methods?

Yep that would be great

@Bogay
Copy link
Contributor Author

Bogay commented Sep 28, 2022

@Lee-W I think it's ready to be reviewed.

Copy link
Member

@Lee-W Lee-W left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Bogay sorry for the late review. Just one minor design issue I'd like to discuss. Others are just minor nitpick. You could fix it if you have time. Otherwise we could do that in the future. We're almost ready for this merge!

],
).ask()
if hook_types:
if not self._install_pre_commit_hook(hook_types):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should put the following code into _exec_install_pre_commit_hook. This seems to be the responsibility of that function. @Bogay what do you think?

                raise InitFailedError(
                    "Installation failed. See error outputs for more information."
                )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think that's reasonable. We can let _install_pre_commit_hook raise an exception instead of return a bool.
Maybe we can just put all error message inside the InitFailedError so that we don't need extra out.error calls?

This is what the error output looks like:
圖片

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, that sounds great!

return tag_format

def _install_pre_commit_hook(self):
def _search_pre_commit(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: missing return type anntation

)
c = cmd.run(cmd_str)
if c.return_code != 0:
out.error(f"Error running {cmd_str}. Outputs are attached below:")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: I might suggest invoke one out.error and concatenation the string instead.

Comment on lines 129 to 145
cmd_str = "pre-commit install " + "".join(
f"--hook-type {ty}" for ty in hook_types
)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found that I forgot to add a space between --hook-type options...
@Lee-W Do you have any idea how to write tests for this?
Because it depends on pre-commit's behavior, it's harder to test.

Suggested change
cmd_str = "pre-commit install " + "".join(
f"--hook-type {ty}" for ty in hook_types
)
cmd_str = "pre-commit install " + " ".join(
f"--hook-type {ty}" for ty in hook_types
)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could verify whether the code generate the pre-commit command we want. e.g., mock the caller function of cmd_str to check

@Bogay Bogay requested a review from Lee-W October 20, 2022 18:56
@Bogay
Copy link
Contributor Author

Bogay commented Oct 20, 2022

@Lee-W it's done 👍

@Lee-W Lee-W force-pushed the fix/report-hook-installation-fail branch from df02f29 to a23ef07 Compare December 29, 2022 10:01
Copy link
Member

@Lee-W Lee-W left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Bogay Sorry for taking so long. I think we're ready to merge this one!

@woile I plan to merge this later this week. Let me know if you want to take a deeper look.

@Lee-W Lee-W merged commit a1e0383 into commitizen-tools:master Dec 31, 2022
@Bogay Bogay deleted the fix/report-hook-installation-fail branch April 29, 2023 13:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants