Skip to content

Commit 82ecc4b

Browse files
committed
Improvements to logging and tests
1 parent 53f22d1 commit 82ecc4b

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/tctools/patch_plc/patch_plc_class.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
from argparse import RawDescriptionHelpFormatter
23
from collections.abc import Iterable
34
from dataclasses import dataclass, field
@@ -200,6 +201,8 @@ def operation_merge(
200201
self.logger.info("No new source files or folders, stopping")
201202
return 0
202203

204+
self.log_sources(new_sources, True)
205+
203206
if self.args.check:
204207
self.logger.info("Some file or folders would be added")
205208
return 1 # Something left to do, so exit with error (= check has failed)
@@ -229,6 +232,8 @@ def operation_remove(
229232
self.logger.info("No files or folders to un-register, stopping")
230233
return 0
231234

235+
self.log_sources(to_remove, False)
236+
232237
if self.args.check:
233238
self.logger.info("Some file or folders would be un-registered")
234239
return 1 # Something left to do, so exit with error (= check has failed)
@@ -278,6 +283,9 @@ def operation_reset(self, current_sources: FileItems, new_sources: FileItemsGrou
278283
self.logger.info("No files or folders to change, stopping")
279284
return 0
280285

286+
self.log_sources(to_add, True)
287+
self.log_sources(to_remove, False)
288+
281289
if self.args.check:
282290
self.logger.info("Some file or folders would be (un-)registered")
283291
return 1 # Something left to do, so exit with error (= check has failed)
@@ -432,6 +440,20 @@ def remove_helper(ref_elements, ref_set):
432440
remove_helper(self._element_folders, to_remove.folders)
433441
remove_helper(self._element_files, to_remove.files)
434442

443+
def log_sources(self, source: FileItems, add: bool):
444+
"""Log a set of sources in its entirety.
445+
446+
Logged a INFO level when `dry` or `check` (otherwise the output is kind of
447+
meaningless), otherwise at DEBUG.
448+
"""
449+
level = logging.INFO if self.args.check or self.args.dry else logging.DEBUG
450+
451+
action = "New" if add else "Remove"
452+
for item in source.files:
453+
self.logger.log(level, f"{action} file: {item}")
454+
for item in source.folders:
455+
self.logger.log(level, f"{action} folder: {item}")
456+
435457
@staticmethod
436458
def path_to_str(path: PurePath) -> str:
437459
"""Turn any path into a windows-path string.

tests/test_patch_plc.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
import shutil
33
import subprocess
44
import sys
5+
from collections.abc import Generator
56
from pathlib import Path, PureWindowsPath
6-
from typing import Any, Generator
77

88
import pytest
99

@@ -23,12 +23,12 @@ def to_paths(*paths: str) -> list[Path]:
2323

2424

2525
@pytest.fixture()
26-
def plc_dir(plc_code) -> Generator[Any, Any, None]:
26+
def plc_dir(plc_code) -> Generator[Path, None, None]:
2727
yield plc_code / "TwinCAT Project1" / "MyPlc"
2828

2929

3030
@pytest.fixture()
31-
def project(plc_dir) -> Generator[Any, Any, None]:
31+
def project(plc_dir) -> Generator[Path, None, None]:
3232
yield plc_dir / "MyPlc.plcproj"
3333

3434

@@ -180,7 +180,7 @@ def test_remove_recursive(plc_dir, project, recursive):
180180
lines_after = content_after.count("\n")
181181

182182
if not recursive:
183-
assert content_after == content_before # No changes
183+
assert content_after == content_before # No changes
184184
return
185185

186186
for item in tracked_files + tracked_folders:

0 commit comments

Comments
 (0)