Skip to content

Commit 085dcb3

Browse files
authored
Fix optional scope not respected when using --scopes flag (#113)
2 parents 88f9fd3 + 3268646 commit 085dcb3

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

conventional_pre_commit/format.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ def r_scope(optional=True, scopes: Optional[List[str]] = None):
3939

4040
if scopes:
4141
scopes_pattern = _get_scope_pattern(scopes)
42-
return scopes_pattern
42+
if optional:
43+
return f"(?:{scopes_pattern})?"
44+
else:
45+
return scopes_pattern
4346

4447
if optional:
4548
return r"(\([\w \/:,-]+\))?"

tests/test_format.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def test_r_scope__special_chars():
6666

6767
def test_r_scope__scopes():
6868
scopes_input = ["api", "client"]
69-
result = format.r_scope(scopes=scopes_input)
69+
result = format.r_scope(scopes=scopes_input, optional=False)
7070
regex = re.compile(result)
7171
assert regex.match("(api)")
7272
assert regex.match("(client)")

tests/test_hook.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,36 @@ def test_subprocess_fail__conventional_with_multiple_scopes(cmd, conventional_co
152152
assert result == RESULT_FAIL
153153

154154

155+
def test_main_success__custom_scopes_optional_scope(conventional_commit_path):
156+
result = main(["--scopes", "api,client", conventional_commit_path])
157+
assert result == RESULT_SUCCESS
158+
159+
160+
def test_main_success__custom_scopes_with_allowed_scope(conventional_commit_with_multiple_scopes_path):
161+
result = main(["--scopes", "chore,api,client", conventional_commit_with_multiple_scopes_path])
162+
assert result == RESULT_SUCCESS
163+
164+
165+
def test_main_fail__custom_scopes_with_disallowed_scope(conventional_commit_with_scope_path):
166+
result = main(["--scopes", "api,client", conventional_commit_with_scope_path])
167+
assert result == RESULT_FAIL
168+
169+
170+
def test_main_fail__custom_scopes_require_scope_no_scope(conventional_commit_path):
171+
result = main(["--scopes", "chore,feat,fix,custom", "--force-scope", conventional_commit_path])
172+
assert result == RESULT_FAIL
173+
174+
175+
def test_main_success__custom_scopes_require_scope_with_allowed_scope(conventional_commit_with_scope_path):
176+
result = main(["--scopes", "api,client,scope", "--force-scope", conventional_commit_with_scope_path])
177+
assert result == RESULT_SUCCESS
178+
179+
180+
def test_main_fail__custom_scopes_require_scope_with_disallowed_scope(conventional_commit_with_scope_path):
181+
result = main(["--scopes", "api,client", "--force-scope", conventional_commit_with_scope_path])
182+
assert result == RESULT_FAIL
183+
184+
155185
def test_subprocess_success__fixup_commit(cmd, fixup_commit_path):
156186
result = subprocess.call((cmd, fixup_commit_path))
157187

0 commit comments

Comments
 (0)