Add unit testing capabilities and basic tests for check_for_usbdevice
authorManuel Rom <manuel.rom@student.tugraz.at>
Fri, 11 Sep 2020 11:52:18 +0000 (13:52 +0200)
committerMichael Prokop <mika@grml.org>
Fri, 2 Oct 2020 13:23:20 +0000 (15:23 +0200)
* 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

grml2usb
pytest.ini [new file with mode: 0644]
test/grml2usb.py [new symlink]
test/grml2usb_test.py [new file with mode: 0644]

index 7afeb48..7e68b18 100755 (executable)
--- a/grml2usb
+++ b/grml2usb
@@ -906,13 +906,21 @@ def unmount(target, unmount_options):
             unregister_mountpoint(target)
 
 
             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
     """
 
 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):
     # 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 (file)
index 0000000..6bba0a2
--- /dev/null
@@ -0,0 +1,3 @@
+[pytest]
+markers =
+    check_for_usbdevice
diff --git a/test/grml2usb.py b/test/grml2usb.py
new file mode 120000 (symlink)
index 0000000..07ca4fc
--- /dev/null
@@ -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 (file)
index 0000000..56b2085
--- /dev/null
@@ -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:
+<project root>$ pytest [-m {basic}]
+
+:copyright: (c) 2020 by Manuel Rom <roma@synpro.solutions>
+: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")