Skip to content

Commit 6035d25

Browse files
committed
Format and use f-strings
Signed-off-by: Philippe Ombredanne <[email protected]>
1 parent 7c51073 commit 6035d25

File tree

2 files changed

+24
-28
lines changed

2 files changed

+24
-28
lines changed

tests/licensedcode/licensedcode_test_utils.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def __attrs_post_init__(self, *args, **kwargs):
6464
with io.open(self.data_file, encoding='utf-8') as df:
6565
data = saneyaml.load(df.read()) or {}
6666
except Exception as e:
67-
raise Exception('Failed to read:', 'file://' + self.data_file, e)
67+
raise Exception(f'Failed to read: file://{self.data_file}', e)
6868

6969
self.license_expressions = data.pop('license_expressions', [])
7070
self.notes = data.pop('notes', None)
@@ -83,22 +83,23 @@ def __attrs_post_init__(self, *args, **kwargs):
8383
except:
8484
raise Exception(
8585
'Unable to parse License rule expression: '
86-
+repr(exp) + ' for: file://' + self.data_file +
87-
'\n' + traceback.format_exc()
88-
)
86+
f'{exp!r} for: file://{self.data_file}\n' +
87+
traceback.format_exc()
88+
)
8989
if expression is None:
9090
raise Exception(
9191
'Unable to parse License rule expression: '
92-
+repr(exp) + ' for:' + repr(self.data_file))
93-
92+
f'{exp!r} for: file://{self.data_file}'
93+
)
9494
new_exp = expression.render()
9595
self.license_expressions[i] = new_exp
9696

9797
else:
9898
if not self.notes:
9999
raise Exception(
100100
'A license test without expected license_expressions should '
101-
'have explanatory notes: for: file://' + self.data_file)
101+
f'have explanatory notes: for: file://{self.data_file}'
102+
)
102103

103104
def to_dict(self):
104105
dct = {}
@@ -165,8 +166,9 @@ def build_tests(test_dir, clazz, regen=False):
165166

166167
# avoid duplicated test method
167168
if hasattr(clazz, test_name):
168-
msg = ('Duplicated test method name: {test_name}: file://{test_file}'
169-
).format(**locals())
169+
msg = (
170+
f'Duplicated test method name: {test_name}: file://{test_file}'
171+
)
170172
raise Exception(msg)
171173

172174
# attach that method to our license_test class
@@ -219,30 +221,28 @@ def closure_test_function(*args, **kwargs):
219221
results_failure_trace.extend(['',
220222
'======= MATCH ====', repr(match),
221223
'======= Matched Query Text for:',
222-
'file://{test_file}'.format(**locals())
224+
f'file://{test_file}'
223225
])
224226
if test_data_file:
225-
results_failure_trace.append(
226-
'file://{test_data_file}'.format(**locals()))
227+
results_failure_trace.append(f'file://{test_data_file}')
227228

228229
results_failure_trace.append('')
229230
results_failure_trace.append(qtext)
230231
results_failure_trace.extend(['',
231232
'======= Matched Rule Text for:',
232-
'file://{rule_text_file}'.format(**locals()),
233-
'file://{rule_data_file}'.format(**locals()),
233+
f'file://{rule_text_file}',
234+
f'file://{rule_data_file}',
234235
'',
235236
itext,
236237
])
237238
if not matches:
238239
results_failure_trace.extend(['',
239240
'======= NO MATCH ====',
240241
'======= Not Matched Query Text for:',
241-
'file://{test_file}'.format(**locals())
242+
f'file://{test_file}'
242243
])
243244
if test_data_file:
244-
results_failure_trace.append(
245-
'file://{test_data_file}'.format(**locals()))
245+
results_failure_trace.append(f'file://{test_data_file}')
246246

247247
# this assert will always fail and provide a detailed failure trace
248248
assert '\n'.join(results_failure_trace) == '\n'.join(expected)

tests/licensedcode/test_detection_validate.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def make_validation_test(rule, test_name):
3939
if rule.is_false_positive:
4040

4141
def closure_test_function(*args, **kwargs):
42-
check_special_rule_can_be_detected(rule)
42+
check_special_rule_cannot_be_detected(rule)
4343

4444
else:
4545

@@ -53,24 +53,18 @@ def closure_test_function(*args, **kwargs):
5353
return closure_test_function
5454

5555

56-
def check_special_rule_can_be_detected(rule):
56+
def check_special_rule_cannot_be_detected(rule):
5757
idx = cache.get_index()
5858
results = idx.match(location=rule.text_file)
59-
try:
60-
assert not results
61-
except:
59+
if results:
6260
data_file = rule.data_file
6361
if not data_file:
6462
data_file = rule.text_file.replace('.LICENSE', '.yml')
6563
# On failure, we compare againto get additional failure details such as
6664
# a clickable text_file path
67-
results = (
68-
results,
69-
f'file://{data_file}',
70-
f'file://{rule.text_file}',
71-
)
65+
results = (results, f'file://{data_file}', f'file://{rule.text_file}')
7266
# this assert will always fail and provide a more detailed failure trace
73-
assert not results
67+
assert results == []
7468

7569

7670
def check_rule_or_license_can_be_self_detected_exactly(rule):
@@ -240,6 +234,7 @@ class TestValidateLicenseExtended5(unittest.TestCase):
240234

241235

242236
_rules = sorted(models.get_rules(), key=lambda r: r.identifier)
237+
243238
build_validation_tests(
244239
_rules,
245240
test_classes=[
@@ -251,4 +246,5 @@ class TestValidateLicenseExtended5(unittest.TestCase):
251246
TestValidateLicenseExtended5,
252247
]
253248
)
249+
254250
del _rules

0 commit comments

Comments
 (0)