Skip to content

Commit 883a8a3

Browse files
committed
Add tests for limit config
- Catching ValueErrors from ResolutionDateRedacter constructor and report the error message now. Previously those error messages didn't get reported to the user.
1 parent 16c3ec7 commit 883a8a3

2 files changed

Lines changed: 64 additions & 7 deletions

File tree

gitprivacy/gitprivacy.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,19 @@ def read_config(self):
4747
with self.repo.config_reader() as config:
4848
self.mode = config.get_value(self.SECTION, 'mode', 'reduce')
4949
self.pattern = config.get_value(self.SECTION, 'pattern', '')
50-
self.limit = config.get_value(self.SECTION, "limitHour", config.get_value(self.SECTION, 'limit', ''))
51-
if config.get_value(self.SECTION, 'limit', False):
50+
self.limit = config.get_value(self.SECTION, "limitHour",
51+
config.get_value(self.SECTION, 'limit', ''))
52+
if config.has_option(self.SECTION, 'limit'):
5253
click.echo(click.wrap_text(
53-
'The option privacy.limit is deprecated and will be removed in future versions.'
54+
'Warning: The option privacy.limit is deprecated and will be removed in future versions.'
5455
'Use privacy.limitHour instead.'
5556
))
56-
if config.get_value(self.SECTION, 'limit', False) and config.get_value(self.SECTION, 'limitHour', False):
57+
if config.has_option(self.SECTION, 'limit') and config.has_option(self.SECTION, 'limitHour'):
5758
click.echo(click.wrap_text(
58-
'Not allowed to use the deprecated privacy.limit and privacy.limitHour at the same time.'
59+
'Error: Not allowed to use the deprecated privacy.limit and privacy.limitHour at the same time.'
5960
'Only use privacy.limitHour instead.'
60-
))
61+
), err=True)
62+
ctx.exit(1)
6163
self.limitDay = config.get_value(self.SECTION, "limitWeekday", '')
6264
self.password = config.get_value(self.SECTION, 'password', '')
6365
self.salt = config.get_value(self.SECTION, 'salt', '')
@@ -104,7 +106,12 @@ def get_dateredacter(self) -> DateRedacter:
104106
"following time unit identifiers: "
105107
"M: month, d: day, h: hour, m: minute, s: second.",
106108
preserve_paragraphs=True))
107-
return ResolutionDateRedacter(self.pattern, self.limit, self.limitDay, self.mode)
109+
try:
110+
redacter = ResolutionDateRedacter(self.pattern, self.limit, self.limitDay, self.mode)
111+
except ValueError as e:
112+
click.echo(click.wrap_text(str(e)))
113+
ctx.exit(1)
114+
return redacter
108115

109116
def write_config(self, **kwargs):
110117
"""Write config"""

tests/test_gitprivacy.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,56 @@ def test_prepush_check(self):
10941094
)
10951095
self.assertEqual(res, 0)
10961096

1097+
def test_limithour_config(self):
1098+
with self.runner.isolated_filesystem():
1099+
self.setUpRepo()
1100+
self.setConfig()
1101+
self.addCommit("a")
1102+
self.git.config(["privacy.limit", "0-8"])
1103+
result = self.invoke('redate')
1104+
self.assertIn("The option privacy.limit is deprecated", result.output)
1105+
self.assertEqual(result.exit_code, 0)
1106+
self.addCommit("b")
1107+
self.git.config(["privacy.limitHour", "0-8"])
1108+
result = self.invoke('redate')
1109+
self.assertIn(
1110+
"Error: Not allowed to use the deprecated",
1111+
result.output
1112+
)
1113+
self.assertEqual(result.exit_code, 1)
1114+
self.git.config(["--unset", "privacy.limit"])
1115+
self.git.config(["privacy.limitHour", "invalid"])
1116+
result = self.invoke('redate')
1117+
self.assertIn(
1118+
"Unexpected syntax for limit.",
1119+
result.output
1120+
)
1121+
self.assertEqual(result.exit_code, 1)
1122+
self.git.config(["privacy.limitHour", "20-24"])
1123+
result = self.invoke('redate')
1124+
self.assertEqual(result.exit_code, 0)
1125+
1126+
def test_limitweekday_config(self):
1127+
with self.runner.isolated_filesystem():
1128+
self.setUpRepo()
1129+
self.setConfig()
1130+
self.addCommit("a")
1131+
self.git.config(["privacy.limitWeekday", "1-0"])
1132+
result = self.invoke('redate')
1133+
self.assertIn("Start day can't be after end day for limit_day.", result.output)
1134+
self.assertEqual(result.exit_code, 1)
1135+
self.addCommit("b")
1136+
self.git.config(["privacy.limitWeekday", "0,1,7"])
1137+
result = self.invoke('redate')
1138+
self.assertIn(
1139+
"Day must be between 0 and 6 for limit_day.",
1140+
result.output
1141+
)
1142+
self.assertEqual(result.exit_code, 1)
1143+
self.git.config(["privacy.limitWeekday", "0, 1, 2,3,4,5,6"])
1144+
result = self.invoke('redate')
1145+
self.assertEqual(result.exit_code, 0)
1146+
10971147
def test_prepush_check_multiple_remotes(self):
10981148
with self.runner.isolated_filesystem():
10991149
self.setUpRepo()

0 commit comments

Comments
 (0)