From: Manuel Rom Date: Fri, 11 Sep 2020 11:52:18 +0000 (+0200) Subject: Add unit testing capabilities and basic tests for check_for_usbdevice X-Git-Tag: v0.18.4~5 X-Git-Url: https://git.grml.org/?p=grml2usb.git;a=commitdiff_plain;h=6f3eb526aad869050f24159dc46e5d3bfe7cf952 Add unit testing capabilities and basic tests for check_for_usbdevice * Add a test directory and create file 'test/grml2usb_test.py' * Provide basic test for extract_device_name() * Extract regex match into separate function for better readability and testing options * Provide pytest.ini configuration to be able to use custom markers * Add symlink in test directory pointing to grml2usb for usage with importlib --- diff --git a/grml2usb b/grml2usb index 7afeb48..7e68b18 100755 --- a/grml2usb +++ b/grml2usb @@ -906,13 +906,21 @@ def unmount(target, unmount_options): unregister_mountpoint(target) +def extract_device_name(device): + """Extract the device name of a given path + + @device: device name, like /dev/sda1 or /dev/sda + """ + return re.match(r"/dev/(.*?)\d*$", device).group(1) + + def check_for_usbdevice(device): """Check whether the specified device is a removable USB device @device: device name, like /dev/sda1 or /dev/sda """ - usbdevice = re.match(r"/dev/(.*?)\d*$", device).group(1) + usbdevice = extract_device_name(device) # newer systems: usbdev = os.path.realpath("/sys/class/block/" + usbdevice + "/removable") if not os.path.isfile(usbdev): diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..6bba0a2 --- /dev/null +++ b/pytest.ini @@ -0,0 +1,3 @@ +[pytest] +markers = + check_for_usbdevice diff --git a/test/grml2usb.py b/test/grml2usb.py new file mode 120000 index 0000000..07ca4fc --- /dev/null +++ b/test/grml2usb.py @@ -0,0 +1 @@ +../grml2usb \ No newline at end of file diff --git a/test/grml2usb_test.py b/test/grml2usb_test.py new file mode 100644 index 0000000..56b2085 --- /dev/null +++ b/test/grml2usb_test.py @@ -0,0 +1,40 @@ +""" +grml2usb basic pytests +~~~~~~~~~~~~~~~~~~~~~~ + +This script contains basic "unit" tests, implemented for and executed with pytest. + +Requirements: +pytest (pip install pytest) + +Runwith: +$ pytest [-m {basic}] + +:copyright: (c) 2020 by Manuel Rom +:license: GPL v2 or any later version +:bugreports: http://grml.org/bugs/ +""" + + +import importlib + +import pytest + +grml2usb = importlib.import_module("grml2usb", ".") + + +@pytest.mark.check_for_usbdevice +def test_extract_device_name(): + """Assert, that 'extract_device_name' returns a device name for a given path""" + assert grml2usb.extract_device_name("/dev/sda") == "sda" + assert grml2usb.extract_device_name("/dev/sdb") == "sdb" + assert grml2usb.extract_device_name("/dev/sdb4") == "sdb" + + +@pytest.mark.check_for_usbdevice +def test_extract_device_name_invalid(): + """Assert, that 'extract_device_name' raises an Error, when given an incorrect string""" + with pytest.raises(AttributeError): + assert grml2usb.extract_device_name("/dev") + with pytest.raises(AttributeError): + assert grml2usb.extract_device_name("foobar")