Skip to content

Commit

Permalink
nixos/test-driver: apply ruff check suggestions (#358150)
Browse files Browse the repository at this point in the history
  • Loading branch information
mweinelt authored Nov 22, 2024
2 parents 9069a28 + 172a35f commit b9dec6d
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 104 deletions.
82 changes: 49 additions & 33 deletions nixos/lib/test-driver/default.nix
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
{ lib
, python3Packages
, enableOCR ? false
, qemu_pkg ? qemu_test
, coreutils
, imagemagick_light
, netpbm
, qemu_test
, socat
, ruff
, tesseract4
, vde2
, extraPythonPackages ? (_ : [])
, nixosTests
{
lib,
python3Packages,
enableOCR ? false,
qemu_pkg ? qemu_test,
coreutils,
imagemagick_light,
netpbm,
qemu_test,
socat,
ruff,
tesseract4,
vde2,
extraPythonPackages ? (_: [ ]),
nixosTests,
}:
let
fs = lib.fileset;
in
python3Packages.buildPythonApplication {
pname = "nixos-test-driver";
version = "1.1";
pyproject = true;

src = fs.toSource {
root = ./.;
fileset = fs.unions [
Expand All @@ -27,37 +30,50 @@ python3Packages.buildPythonApplication {
./extract-docstrings.py
];
};
pyproject = true;

propagatedBuildInputs = [
coreutils
netpbm
python3Packages.colorama
python3Packages.junit-xml
python3Packages.ptpython
qemu_pkg
socat
vde2
]
++ (lib.optionals enableOCR [ imagemagick_light tesseract4 ])
build-system = with python3Packages; [
setuptools
];

dependencies =
with python3Packages;
[
colorama
junit-xml
ptpython
]
++ extraPythonPackages python3Packages;

nativeBuildInputs = [
python3Packages.setuptools
];
propagatedBuildInputs =
[
coreutils
netpbm
qemu_pkg
socat
vde2
]
++ lib.optionals enableOCR [
imagemagick_light
tesseract4
];

passthru.tests = {
inherit (nixosTests.nixos-test-driver) driver-timeout;
};

doCheck = true;
nativeCheckInputs = with python3Packages; [ mypy ruff black ];

nativeCheckInputs = with python3Packages; [
mypy
ruff
];

checkPhase = ''
echo -e "\x1b[32m## run mypy\x1b[0m"
mypy test_driver extract-docstrings.py
echo -e "\x1b[32m## run ruff\x1b[0m"
echo -e "\x1b[32m## run ruff check\x1b[0m"
ruff check .
echo -e "\x1b[32m## run black\x1b[0m"
black --check --diff .
echo -e "\x1b[32m## run ruff format\x1b[0m"
ruff format --check --diff .
'';
}
6 changes: 1 addition & 5 deletions nixos/lib/test-driver/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ find = {}
test_driver = ["py.typed"]

[tool.ruff]
target-version = "py312"
line-length = 88

lint.select = ["E", "F", "I", "U", "N"]
Expand All @@ -35,11 +36,6 @@ ignore_missing_imports = true
module = "junit_xml.*"
ignore_missing_imports = true

[tool.black]
line-length = 88
target-version = ['py39']
include = '\.pyi?$'

[tool.mypy]
warn_redundant_casts = true
disallow_untyped_calls = true
Expand Down
27 changes: 14 additions & 13 deletions nixos/lib/test-driver/test_driver/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import signal
import tempfile
import threading
from contextlib import contextmanager
from collections.abc import Callable, Iterator
from contextlib import AbstractContextManager, contextmanager
from pathlib import Path
from typing import Any, Callable, ContextManager, Dict, Iterator, List, Optional, Union
from typing import Any

from colorama import Fore, Style

Expand Down Expand Up @@ -44,17 +45,17 @@ class Driver:
and runs the tests"""

tests: str
vlans: List[VLan]
machines: List[Machine]
polling_conditions: List[PollingCondition]
vlans: list[VLan]
machines: list[Machine]
polling_conditions: list[PollingCondition]
global_timeout: int
race_timer: threading.Timer
logger: AbstractLogger

def __init__(
self,
start_scripts: List[str],
vlans: List[int],
start_scripts: list[str],
vlans: list[int],
tests: str,
out_dir: Path,
logger: AbstractLogger,
Expand All @@ -73,7 +74,7 @@ def __init__(
vlans = list(set(vlans))
self.vlans = [VLan(nr, tmp_dir, self.logger) for nr in vlans]

def cmd(scripts: List[str]) -> Iterator[NixStartScript]:
def cmd(scripts: list[str]) -> Iterator[NixStartScript]:
for s in scripts:
yield NixStartScript(s)

Expand Down Expand Up @@ -119,7 +120,7 @@ def subtest(self, name: str) -> Iterator[None]:
self.logger.error(f'Test "{name}" failed with error: "{e}"')
raise e

def test_symbols(self) -> Dict[str, Any]:
def test_symbols(self) -> dict[str, Any]:
@contextmanager
def subtest(name: str) -> Iterator[None]:
return self.subtest(name)
Expand Down Expand Up @@ -207,7 +208,7 @@ def create_machine(
self,
start_command: str | dict,
*,
name: Optional[str] = None,
name: str | None = None,
keep_vm_state: bool = False,
) -> Machine:
# Legacy args handling
Expand Down Expand Up @@ -273,11 +274,11 @@ def check_polling_conditions(self) -> None:

def polling_condition(
self,
fun_: Optional[Callable] = None,
fun_: Callable | None = None,
*,
seconds_interval: float = 2.0,
description: Optional[str] = None,
) -> Union[Callable[[Callable], ContextManager], ContextManager]:
description: str | None = None,
) -> Callable[[Callable], AbstractContextManager] | AbstractContextManager:
driver = self

class Poll:
Expand Down
43 changes: 22 additions & 21 deletions nixos/lib/test-driver/test_driver/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
import time
import unicodedata
from abc import ABC, abstractmethod
from collections.abc import Iterator
from contextlib import ExitStack, contextmanager
from pathlib import Path
from queue import Empty, Queue
from typing import Any, Dict, Iterator, List
from typing import Any
from xml.sax.saxutils import XMLGenerator
from xml.sax.xmlreader import AttributesImpl

Expand All @@ -18,17 +19,17 @@

class AbstractLogger(ABC):
@abstractmethod
def log(self, message: str, attributes: Dict[str, str] = {}) -> None:
def log(self, message: str, attributes: dict[str, str] = {}) -> None:
pass

@abstractmethod
@contextmanager
def subtest(self, name: str, attributes: Dict[str, str] = {}) -> Iterator[None]:
def subtest(self, name: str, attributes: dict[str, str] = {}) -> Iterator[None]:
pass

@abstractmethod
@contextmanager
def nested(self, message: str, attributes: Dict[str, str] = {}) -> Iterator[None]:
def nested(self, message: str, attributes: dict[str, str] = {}) -> Iterator[None]:
pass

@abstractmethod
Expand Down Expand Up @@ -68,11 +69,11 @@ def __init__(self, outfile: Path) -> None:
self._print_serial_logs = True
atexit.register(self.close)

def log(self, message: str, attributes: Dict[str, str] = {}) -> None:
def log(self, message: str, attributes: dict[str, str] = {}) -> None:
self.tests[self.currentSubtest].stdout += message + os.linesep

@contextmanager
def subtest(self, name: str, attributes: Dict[str, str] = {}) -> Iterator[None]:
def subtest(self, name: str, attributes: dict[str, str] = {}) -> Iterator[None]:
old_test = self.currentSubtest
self.tests.setdefault(name, self.TestCaseState())
self.currentSubtest = name
Expand All @@ -82,7 +83,7 @@ def subtest(self, name: str, attributes: Dict[str, str] = {}) -> Iterator[None]:
self.currentSubtest = old_test

@contextmanager
def nested(self, message: str, attributes: Dict[str, str] = {}) -> Iterator[None]:
def nested(self, message: str, attributes: dict[str, str] = {}) -> Iterator[None]:
self.log(message)
yield

Expand Down Expand Up @@ -123,25 +124,25 @@ def close(self) -> None:


class CompositeLogger(AbstractLogger):
def __init__(self, logger_list: List[AbstractLogger]) -> None:
def __init__(self, logger_list: list[AbstractLogger]) -> None:
self.logger_list = logger_list

def add_logger(self, logger: AbstractLogger) -> None:
self.logger_list.append(logger)

def log(self, message: str, attributes: Dict[str, str] = {}) -> None:
def log(self, message: str, attributes: dict[str, str] = {}) -> None:
for logger in self.logger_list:
logger.log(message, attributes)

@contextmanager
def subtest(self, name: str, attributes: Dict[str, str] = {}) -> Iterator[None]:
def subtest(self, name: str, attributes: dict[str, str] = {}) -> Iterator[None]:
with ExitStack() as stack:
for logger in self.logger_list:
stack.enter_context(logger.subtest(name, attributes))
yield

@contextmanager
def nested(self, message: str, attributes: Dict[str, str] = {}) -> Iterator[None]:
def nested(self, message: str, attributes: dict[str, str] = {}) -> Iterator[None]:
with ExitStack() as stack:
for logger in self.logger_list:
stack.enter_context(logger.nested(message, attributes))
Expand Down Expand Up @@ -173,7 +174,7 @@ class TerminalLogger(AbstractLogger):
def __init__(self) -> None:
self._print_serial_logs = True

def maybe_prefix(self, message: str, attributes: Dict[str, str]) -> str:
def maybe_prefix(self, message: str, attributes: dict[str, str]) -> str:
if "machine" in attributes:
return f"{attributes['machine']}: {message}"
return message
Expand All @@ -182,16 +183,16 @@ def maybe_prefix(self, message: str, attributes: Dict[str, str]) -> str:
def _eprint(*args: object, **kwargs: Any) -> None:
print(*args, file=sys.stderr, **kwargs)

def log(self, message: str, attributes: Dict[str, str] = {}) -> None:
def log(self, message: str, attributes: dict[str, str] = {}) -> None:
self._eprint(self.maybe_prefix(message, attributes))

@contextmanager
def subtest(self, name: str, attributes: Dict[str, str] = {}) -> Iterator[None]:
def subtest(self, name: str, attributes: dict[str, str] = {}) -> Iterator[None]:
with self.nested("subtest: " + name, attributes):
yield

@contextmanager
def nested(self, message: str, attributes: Dict[str, str] = {}) -> Iterator[None]:
def nested(self, message: str, attributes: dict[str, str] = {}) -> Iterator[None]:
self._eprint(
self.maybe_prefix(
Style.BRIGHT + Fore.GREEN + message + Style.RESET_ALL, attributes
Expand Down Expand Up @@ -241,12 +242,12 @@ def close(self) -> None:
def sanitise(self, message: str) -> str:
return "".join(ch for ch in message if unicodedata.category(ch)[0] != "C")

def maybe_prefix(self, message: str, attributes: Dict[str, str]) -> str:
def maybe_prefix(self, message: str, attributes: dict[str, str]) -> str:
if "machine" in attributes:
return f"{attributes['machine']}: {message}"
return message

def log_line(self, message: str, attributes: Dict[str, str]) -> None:
def log_line(self, message: str, attributes: dict[str, str]) -> None:
self.xml.startElement("line", attrs=AttributesImpl(attributes))
self.xml.characters(message)
self.xml.endElement("line")
Expand All @@ -260,7 +261,7 @@ def warning(self, *args, **kwargs) -> None: # type: ignore
def error(self, *args, **kwargs) -> None: # type: ignore
self.log(*args, **kwargs)

def log(self, message: str, attributes: Dict[str, str] = {}) -> None:
def log(self, message: str, attributes: dict[str, str] = {}) -> None:
self.drain_log_queue()
self.log_line(message, attributes)

Expand All @@ -273,7 +274,7 @@ def log_serial(self, message: str, machine: str) -> None:

self.enqueue({"msg": message, "machine": machine, "type": "serial"})

def enqueue(self, item: Dict[str, str]) -> None:
def enqueue(self, item: dict[str, str]) -> None:
self.queue.put(item)

def drain_log_queue(self) -> None:
Expand All @@ -287,12 +288,12 @@ def drain_log_queue(self) -> None:
pass

@contextmanager
def subtest(self, name: str, attributes: Dict[str, str] = {}) -> Iterator[None]:
def subtest(self, name: str, attributes: dict[str, str] = {}) -> Iterator[None]:
with self.nested("subtest: " + name, attributes):
yield

@contextmanager
def nested(self, message: str, attributes: Dict[str, str] = {}) -> Iterator[None]:
def nested(self, message: str, attributes: dict[str, str] = {}) -> Iterator[None]:
self.xml.startElement("nest", attrs=AttributesImpl({}))
self.xml.startElement("head", attrs=AttributesImpl(attributes))
self.xml.characters(message)
Expand Down
Loading

0 comments on commit b9dec6d

Please sign in to comment.