diff --git a/checkbox-support/checkbox_support/helpers/slugify.py b/checkbox-support/checkbox_support/helpers/slugify.py new file mode 100644 index 0000000000..c5966ce7d2 --- /dev/null +++ b/checkbox-support/checkbox_support/helpers/slugify.py @@ -0,0 +1,27 @@ +# This file is part of Checkbox. +# +# Copyright 2024 Canonical Ltd. +# Written by: +# Pierre Equoy +# +# Checkbox is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 3, +# as published by the Free Software Foundation. +# +# Checkbox is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Checkbox. If not, see . + +import string + + +def slugify(_string): + """Transform any string to one that can be used in filenames.""" + valid_chars = frozenset( + "-_.{}{}".format(string.ascii_letters, string.digits) + ) + return "".join(c if c in valid_chars else "_" for c in _string) diff --git a/checkbox-support/checkbox_support/parsers/udevadm.py b/checkbox-support/checkbox_support/parsers/udevadm.py index 7778d1ed76..55b2b4cee2 100644 --- a/checkbox-support/checkbox_support/parsers/udevadm.py +++ b/checkbox-support/checkbox_support/parsers/udevadm.py @@ -27,6 +27,7 @@ import re import string +from checkbox_support.helpers.slugify import slugify from checkbox_support.lib.bit import get_bitmask from checkbox_support.lib.bit import test_bit from checkbox_support.lib.input import Input @@ -96,14 +97,6 @@ CAMERA_RE = re.compile(r"Camera", re.I) -def slugify(_string): - """Transform any string to one that can be used in job IDs.""" - valid_chars = frozenset( - "-_.{}{}".format(string.ascii_letters, string.digits) - ) - return "".join(c if c in valid_chars else "_" for c in _string) - - def find_pkname_is_root_mountpoint(devname, lsblk=None): """Check for partition mounted as root for a DISK device.""" if lsblk: diff --git a/checkbox-support/checkbox_support/tests/test_slugify.py b/checkbox-support/checkbox_support/tests/test_slugify.py new file mode 100644 index 0000000000..0325fb9357 --- /dev/null +++ b/checkbox-support/checkbox_support/tests/test_slugify.py @@ -0,0 +1,33 @@ +# This file is part of Checkbox. +# +# Copyright 2024 Canonical Ltd. +# Written by: +# Pierre Equoy +# +# Checkbox is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 3, +# as published by the Free Software Foundation. +# +# Checkbox is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Checkbox. If not, see . + +from unittest import TestCase +from unittest.mock import patch +from io import StringIO + +from checkbox_support.helpers.slugify import slugify + + +class TestSlugify(TestCase): + def test_slugify_no_change(self): + result = slugify("abc123") + self.assertEqual(result, "abc123") + + def test_slugify_special_chars(self): + result = slugify("C'était une belle journée !") + self.assertEqual(result, "C__tait_une_belle_journ_e__") diff --git a/providers/resource/bin/graphics_card_resource.py b/providers/resource/bin/graphics_card_resource.py index dc40eeadf8..942026ad1e 100755 --- a/providers/resource/bin/graphics_card_resource.py +++ b/providers/resource/bin/graphics_card_resource.py @@ -22,9 +22,10 @@ import collections import subprocess import shlex -import string import re +from checkbox_support.helpers.slugify import slugify + def get_ubuntu_version(): """Get Ubuntu release version for checking.""" @@ -59,14 +60,6 @@ def compare_ubuntu_release_version(_version): return os_version >= _version -def slugify(_string): - """Transform any string to one that can be used in job IDs.""" - valid_chars = frozenset( - "-_.{}{}".format(string.ascii_letters, string.digits) - ) - return "".join(c if c in valid_chars else "_" for c in _string) - - def subprocess_lines_generator(command): """ Generator that opens a subprocess and spits out lines diff --git a/providers/resource/bin/snapd_resource.py b/providers/resource/bin/snapd_resource.py index 2639aaacbf..a740a0e752 100755 --- a/providers/resource/bin/snapd_resource.py +++ b/providers/resource/bin/snapd_resource.py @@ -7,9 +7,9 @@ import argparse import os -import string import sys +from checkbox_support.helpers.slugify import slugify from checkbox_support.snap_utils.asserts import decode from checkbox_support.snap_utils.asserts import model_to_resource from checkbox_support.snap_utils.asserts import serial_to_resource @@ -21,14 +21,6 @@ from collections import namedtuple -def slugify(_string): - """Transform string to one that can be used as the key in a resource job""" - valid_chars = frozenset( - "_{}{}".format(string.ascii_letters, string.digits) - ) - return "".join(c if c in valid_chars else "_" for c in _string) - - class ModelAssertion: def invoked(self):