Skip to content

Commit 6552565

Browse files
committed
Normalize filenames derived from free-text 'name:' field
With the new liberal free-form 'name:' we must take better care to ensure all filenames are normalized. To keep the dependencies low we use a local good-enough slugify() for this purpose. Signed-off-by: Joachim Wiberg <[email protected]>
1 parent 79c3661 commit 6552565

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

9pm.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,15 +185,28 @@ def run_test(args, test):
185185

186186
return skip_suite, skip, err
187187

188+
189+
def slugify(text, lowercase=True):
190+
"""
191+
Local good-enough slugify() replacement
192+
193+
Replace spaces, special chars, and non-ASCII chars with '-',
194+
then squash any resulting repeated '-' for readability.
195+
"""
196+
if lowercase:
197+
text = text.lower()
198+
199+
s = re.sub(r'[^\w-]', '-', text, flags=re.ASCII)
200+
return re.sub(r'-{2,}', '-', s)
201+
202+
188203
# In this function, we generate an unique name for each case and suite. Both
189204
# suites and cases can be passed an arbitrary amount of times and the same test
190205
# can reside in different suites. We need something unique to identify them by.
191206
def prefix_name(name):
192207
global TEST_CNT
193208
TEST_CNT += 1
194-
# Normalize name for filesystem safety: replace spaces and remove/replace special chars
195-
normalized = name.lower().replace(" ", "-").replace(".", "").replace("<", "").replace(">", "").replace("?", "")
196-
return str(TEST_CNT).zfill(4) + "-" + normalized
209+
return str(TEST_CNT).zfill(4) + "-" + slugify(name, lowercase=True)
197210

198211
def gen_name(filepath):
199212
return os.path.basename(filepath)

0 commit comments

Comments
 (0)