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):
--- /dev/null
+"""
+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")