Browse Source

refactor: use `pathlib.Path` instances instead of `os.path.*`; increase test coverage

Fabian Peter Hammerle 1 week ago
parent
commit
858dfee530

+ 1 - 1
.github/workflows/python.yml

@@ -49,7 +49,7 @@ jobs:
       env:
         PYTHON_VERSION: ${{ matrix.python-version }}
     - run: pipenv graph
-    - run: pipenv run pytest --cov="$(cat *.egg-info/top_level.txt)" --cov-report=term-missing --cov-fail-under=90
+    - run: pipenv run pytest --cov="$(cat *.egg-info/top_level.txt)" --cov-report=term-missing --cov-fail-under=97
     - run: pipenv run pylint --load-plugins=pylint_import_requirements "$(cat *.egg-info/top_level.txt)"
     # https://github.com/PyCQA/pylint/issues/352
     - run: pipenv run pylint tests/*

+ 5 - 0
.pylintrc

@@ -0,0 +1,5 @@
+[MESSAGE CONTROL]
+
+disable=missing-class-docstring,
+        missing-function-docstring,
+        missing-module-docstring

+ 10 - 15
acpi_backlight/__init__.py

@@ -1,11 +1,9 @@
-# pylint: disable=missing-docstring
-
 import argparse
-import os
+import pathlib
 
 import acpi_backlight.evaluate
 
-_ACPI_BACKLIGHT_ROOT_DIR_PATH = "/sys/class/backlight"
+_ACPI_BACKLIGHT_ROOT_DIR_PATH = pathlib.Path("/sys/class/backlight")
 
 
 class Backlight:
@@ -13,30 +11,27 @@ class Backlight:
     # pylint: disable=too-few-public-methods; does not count properties
 
     def __init__(self, name: str = "intel_backlight"):
-        self._acpi_dir_path = os.path.join(_ACPI_BACKLIGHT_ROOT_DIR_PATH, name)
+        self._acpi_dir_path = _ACPI_BACKLIGHT_ROOT_DIR_PATH.joinpath(name)
 
     @property
-    def _brightness_path(self) -> str:
-        return os.path.join(self._acpi_dir_path, "brightness")
+    def _brightness_path(self) -> pathlib.Path:
+        return self._acpi_dir_path.joinpath("brightness")
 
     @property
-    def _max_brightness_path(self) -> str:
-        return os.path.join(self._acpi_dir_path, "max_brightness")
+    def _max_brightness_path(self) -> pathlib.Path:
+        return self._acpi_dir_path.joinpath("max_brightness")
 
     @property
     def _brightness_absolute(self) -> int:
-        with open(self._brightness_path, "r") as brightness_file:
-            return int(brightness_file.read())
+        return int(self._brightness_path.read_text(encoding="ascii"))
 
     @_brightness_absolute.setter
     def _brightness_absolute(self, brightness_absolute: int):
-        with open(self._brightness_path, "w") as brightness_file:
-            return brightness_file.write(str(brightness_absolute))
+        self._brightness_path.write_text(str(brightness_absolute))
 
     @property
     def _max_brightness_absolute(self) -> int:
-        with open(self._max_brightness_path, "r") as max_brightness_file:
-            return int(max_brightness_file.read())
+        return int(self._max_brightness_path.read_text())
 
     @property
     def brightness_relative(self) -> float:

+ 0 - 2
acpi_backlight/evaluate.py

@@ -1,5 +1,3 @@
-# pylint: disable=missing-docstring
-
 import ast
 import operator as python_operator
 

+ 13 - 6
test/test_backlight.py → tests/test_backlight.py

@@ -1,3 +1,4 @@
+import pathlib
 import unittest.mock
 
 import pytest
@@ -10,13 +11,13 @@ from acpi_backlight import Backlight, backlight_eval
 
 def test_init_default():
     backlight = Backlight()
-    assert backlight._acpi_dir_path == "/sys/class/backlight/intel_backlight"
+    assert backlight._acpi_dir_path.as_posix() == "/sys/class/backlight/intel_backlight"
 
 
 @pytest.mark.parametrize("name", ["intel_backlight", "other"])
 def test_init(name):
     backlight = Backlight(name=name)
-    assert backlight._acpi_dir_path == "/sys/class/backlight/{}".format(name)
+    assert backlight._acpi_dir_path.as_posix() == "/sys/class/backlight/" + name
 
 
 @pytest.mark.parametrize(
@@ -30,10 +31,13 @@ def test_init(name):
     ),
 )
 def test_brightness_relative_get(
-    tmp_path, max_brightness, brightness_absolute_str, expected_brightness_relative
+    tmp_path: pathlib.Path,
+    max_brightness,
+    brightness_absolute_str,
+    expected_brightness_relative,
 ):
     backlight = Backlight()
-    backlight._acpi_dir_path = str(tmp_path)
+    backlight._acpi_dir_path = tmp_path
     tmp_path.joinpath("brightness").write_text(brightness_absolute_str)
     tmp_path.joinpath("max_brightness").write_text(str(max_brightness))
     assert backlight.brightness_relative == pytest.approx(expected_brightness_relative)
@@ -52,10 +56,13 @@ def test_brightness_relative_get(
     ),
 )
 def test_brightness_relative_set(
-    tmp_path, max_brightness, brightness_relative, expected_brightness_abs_str
+    tmp_path: pathlib.Path,
+    max_brightness,
+    brightness_relative,
+    expected_brightness_abs_str,
 ):
     backlight = Backlight()
-    backlight._acpi_dir_path = str(tmp_path)
+    backlight._acpi_dir_path = tmp_path
     tmp_path.joinpath("max_brightness").write_text(str(max_brightness))
     backlight.brightness_relative = brightness_relative
     assert tmp_path.joinpath("brightness").read_text() == expected_brightness_abs_str

+ 0 - 2
tests/test_evaluate.py

@@ -1,5 +1,3 @@
-# pylint: disable=missing-docstring
-
 import pytest
 
 from acpi_backlight.evaluate import evaluate_expression

+ 29 - 0
tests/test_main.py

@@ -0,0 +1,29 @@
+import pathlib
+import unittest.mock
+
+import pytest
+
+import acpi_backlight
+
+
+@pytest.mark.parametrize(
+    ("expr_str", "brightness"),
+    [
+        ("0", "0"),
+        ("1", "200"),
+        ("0.4", "80"),
+        ("b / 2 + 0.21", "92"),
+        ("b - 1", "0"),
+        ("b + 1", "200"),
+    ],
+)
+def test_main(tmp_path: pathlib.Path, expr_str: str, brightness: str):
+    acpi_dir_path = tmp_path.joinpath("intel_backlight")
+    acpi_dir_path.mkdir()
+    acpi_dir_path.joinpath("brightness").write_text("100")
+    acpi_dir_path.joinpath("max_brightness").write_text("200")
+    with unittest.mock.patch("sys.argv", ["", expr_str]), unittest.mock.patch(
+        "acpi_backlight._ACPI_BACKLIGHT_ROOT_DIR_PATH", tmp_path
+    ):
+        acpi_backlight.main()
+    assert acpi_dir_path.joinpath("brightness").read_text() == brightness