8000 Merge branch 'master' into make-command-required · modlin/commitizen@db0b1d5 · GitHub
[go: up one dir, main page]

Skip to content

Commit db0b1d5

Browse files
committed
Merge branch 'master' into make-command-required
2 parents 77fe6fa + 976573e commit db0b1d5

File tree

9 files changed

+90
-9
lines changed

9 files changed

+90
-9
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# CHANGELOG
22

3+
## v1.8.0
4+
5+
### Feature
6+
7+
- new custom exception for commitizen
8+
- commit is aborted if nothing in staging
9+
310
## v1.7.0
411

512
### Feature

commitizen/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "1.7.0"
1+
__version__ = "1.8.0"

commitizen/commands/commit.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
import questionary
66

77
from commitizen import factory, git, out
8+
from commitizen.cz.exceptions import CzException
89

910
NO_ANSWERS = 5
1011
COMMIT_ERROR = 6
1112
NO_COMMIT_BACKUP = 7
13+
NOTHING_TO_COMMIT = 8
14+
CUSTOM_ERROR = 9
1215

1316

1417
class Commit:
@@ -21,6 +24,10 @@ def __init__(self, config: dict, arguments: dict):
2124
self.temp_file: str = os.path.join(tempfile.gettempdir(), "cz.commit.backup")
2225

2326
def __call__(self):
27+
if git.is_staging_clean():
28+
out.write("No files added to staging!")
29+
raise SystemExit(NOTHING_TO_COMMIT)
30+
2431
retry: bool = self.arguments.get("retry")
2532

2633
if retry:
@@ -36,7 +43,15 @@ def __call__(self):
3643
# Prompt user for the commit message
3744
cz = self.cz
3845
questions = cz.questions()
39-
answers = questionary.prompt(questions, style=cz.style)
46+
try:
47+
answers = questionary.prompt(questions, style=cz.style)
48+
except ValueError as err:
49+
root_err = err.__context__
50+
if isinstance(root_err, CzException):
51+
out.error(root_err.__str__())
52+
raise SystemExit(CUSTOM_ERROR)
53+
raise err
54+
4055
if not answers:
4156
raise SystemExit(NO_ANSWERS)
4257
m = cz.message(answers)

commitizen/cz/conventional_commits/conventional_commits.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
from commitizen import defaults
44
from commitizen.cz.base import BaseCommitizen
5+
from commitizen.cz.exceptions import CzException
56

67
__all__ = ["ConventionalCommitsCz"]
78

89

9-
class NoSubjectException(Exception):
10+
class NoSubjectException(CzException):
1011
...
1112

1213

@@ -26,7 +27,7 @@ def parse_subject(text):
2627
text = text.strip(".").strip()
2728

2829
if not text:
29-
raise NoSubjectException
30+
raise NoSubjectException("Subject is required.")
3031

3132
return text
3233

commitizen/cz/exceptions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class CzException(Exception):
2+
...

commitizen/git.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,10 @@ def get_commits(start: str, end: str = "HEAD", from_beginning: bool = False) ->
3333
def tag_exist(tag: str) -> bool:
3434
c = cmd.run(f"git tag --list {tag}")
3535
return tag in c.out
36+
37+
38+
def is_staging_clean() -> bool:
39+
"""Check if staing is clean"""
40+
c = cmd.run("git diff --no-ext-diff --name-only")
41+
c_cached = cmd.run("git diff --no-ext-diff --cached --name-only")
42+
return not (bool(c.out) or bool(c_cached.out))

docs/customization.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ The basic steps are:
77
3. expose the class at the end of your file assigning it to `discover_this`
88
4. Create a python package starting with `cz_` using `setup.py`, `poetry`, etc
99

10-
1110
Check an [example](convcomms) on how to configure `BaseCommitizen`.
1211

1312
## Custom commit rules
@@ -122,4 +121,15 @@ That's it, your commitizen now supports custom rules and you can run
122121
cz -n cz_strange bump
123122
```
124123

125-
[convcomms]: https://github.com/Woile/commitizen/blob/master/commitizen/cz/conventional_commits/conventional_commits.py
124+
[convcomms]: https://github.com/Woile/commitizen/blob/master/commitizen/cz/conventional_commits/conventional_commits.py
125+
126+
## Raise Customize Exception
127+
128+
If you wannt `commitizen` to catch your exception and print the message, you'll have to inherit `CzException`.
129+
130+
```python
131+
from commitizen.cz.exception import CzException
132+
133+
class NoSubjectProvidedException(CzException):
134+
...
135+
```

pyproject.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[tool.commitizen]
2-
version = "1.7.0"
2+
version = "1.8.0"
33
tag_format = "v$version"
44
files = [
55
"pyproject.toml:version",
@@ -29,7 +29,7 @@ exclude = '''
2929

3030
[tool.poetry]
3131
name = "commitizen"
32-
version = "1.7.0"
32+
version = "1.8.0"
3333
description = "Python commitizen client tool"
3434
authors = ["Santiago Fraire <santiwilly@gmail.com>"]
3535
license = "MIT"
@@ -45,7 +45,7 @@ classifiers = [
4545

4646
[tool.poetry.dependencies]
4747
python = "^3.6"
48-
questionary = "^1.0"
48+
questionary = "^1.4"
4949
decli = "^0.5.0"
5050
colorama = "^0.4.1"
5151
termcolor = "^1.1"

tests/test_commands.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,18 @@
44
import pytest
55

66
from commitizen import cmd, commands, defaults
7+
from commitizen.cz.exceptions import CzException
78

89
config = {"name": defaults.name}
910

1011

12+
@pytest.fixture
13+
def staging_is_clean(mocker):
14+
is_staging_clean_mock = mocker.patch("commitizen.git.is_staging_clean")
15+
is_staging_clean_mock.return_value = False
16+
17+
18+
@pytest.mark.usefixtures("staging_is_clean")
1119
def test_commit(mocker):
1220
prompt_mock = mocker.patch("questionary.prompt")
1321
prompt_mock.return_value = {
@@ -27,6 +35,7 @@ def test_commit(mocker):
2735
success_mock.assert_called_once()
2836

2937

38+
@pytest.mark.usefixtures("staging_is_clean")
3039
def test_commit_retry_fails_no_backup(mocker):
3140
commit_mock = mocker.patch("commitizen.git.commit")
3241
commit_mock.return_value = cmd.Command("success", "", "", "")
@@ -35,6 +44,7 @@ def test_commit_retry_fails_no_backup(mocker):
3544
commands.Commit(config, {"retry": True})()
3645

3746

47+
@pytest.mark.usefixtures("staging_is_clean")
3848
def test_commit_retry_works(mocker):
3949
prompt_mock = mocker.patch("questionary.prompt")
4050
prompt_mock.return_value = {
@@ -72,6 +82,35 @@ def test_commit_retry_works(mocker):
7282
assert not os.path.isfile(temp_file)
7383

7484

85+
def test_commit_when_nothing_to_commit(mocker):
86+
is_staging_clean_mock = mocker.patch("commitizen.git.is_staging_clean")
87+
is_staging_clean_mock.return_value = True
88+
89+
with pytest.raises(SystemExit) as err:
90+
commit_cmd = commands.Commit(config, {})
91+
commit_cmd()
92+
93+
assert err.value.code == commands.commit.NOTHING_TO_COMMIT
94+
95+
96+
def test_commit_when_customized_expected_raised(mocker, capsys):
97+
_err = ValueError()
98+
_err.__context__ = CzException("This is the root custom err")
99+
100+
prompt_mock = mocker.patch("questionary.prompt")
101+
prompt_mock.side_effect = _err
102+
103+
with pytest.raises(SystemExit) as err:
104+
commit_cmd = commands.Commit(config, {})
105+
commit_cmd()
106+
107+
assert err.value.code == commands.commit.CUSTOM_ERROR
108+
109+
# Assert only the content in the formatted text
110+
captured = capsys.readouterr()
111+
assert "This is the root custom err" in captured.err
112+
113+
75114
def test_example():
76115
with mock.patch("commitizen.out.write") as write_mock:
77116
commands.Example(config)()

0 commit comments

Comments
 (0)
0