Rotary Logger  1.0.2
The middleware rotary logger
Loading...
Searching...
No Matches
test_rotary_logger_verify_path.py
Go to the documentation of this file.
1"""
2# +==== BEGIN rotary_logger =================+
3# LOGO:
4# ..........####...####..........
5# ......###.....#.#########......
6# ....##........#.###########....
7# ...#..........#.############...
8# ...#..........#.#####.######...
9# ..#.....##....#.###..#...####..
10# .#.....#.##...#.##..##########.
11# #.....##########....##...######
12# #.....#...##..#.##..####.######
13# .#...##....##.#.##..###..#####.
14# ..#.##......#.#.####...######..
15# ..#...........#.#############..
16# ..#...........#.#############..
17# ...##.........#.############...
18# ......#.......#.#########......
19# .......#......#.########.......
20# .........#####...#####.........
21# /STOP
22# PROJECT: rotary_logger
23# FILE: test_rotary_logger_verify_path.py
24# CREATION DATE: 29-10-2025
25# LAST Modified: 23:33:44 29-10-2025
26# DESCRIPTION:
27# A module that provides a universal python light on iops way of logging to files your program execution.
28# /STOP
29# COPYRIGHT: (c) Asperguide
30# PURPOSE: This is the test file in charge of checking if the error handling for paths work for writable and unwritable paths.
31# // AR
32# +==== END rotary_logger =================+
33"""
34
35import sys
36from pathlib import Path
37
38try:
39 sys.path.insert(0, '.')
40 from rotary_logger import constants as CONST
41 from rotary_logger.rotary_logger_cls import RotaryLogger
42except ImportError as e:
43 raise RuntimeError(f"Failed to import the library, error: {e}") from e
44
45
47 """If the provided path is not writable (e.g. /root), the function should
48 fall back to the package default log folder.
49 """
50 rl = RotaryLogger()
51 # When tests run as root, /root may be writable. Make the test robust by
52 # checking writability first and asserting the expected behavior.
53 candidate = Path('/root')
54 result = rl._verify_user_log_path(candidate)
55 try:
56 # If we can create a temporary file in /root/logs then the system
57 # permits writing there (likely running as root), so the function
58 # should return the candidate-with-logs path. Otherwise it should
59 # fall back to the default folder.
60 writable = False
61 testdir = candidate / CONST.LOG_FOLDER_BASE_NAME
62 testdir.mkdir(parents=True, exist_ok=True)
63 testfile = testdir / ".rotary_write_test_check"
64 with open(testfile, "w", encoding="utf-8") as fh:
65 fh.write("x")
66 testfile.unlink()
67 writable = True
68 except OSError:
69 writable = False
70
71 if writable:
72 assert result == candidate / CONST.LOG_FOLDER_BASE_NAME
73 else:
74 assert result == CONST.DEFAULT_LOG_FOLDER
75
76
77def test_tmpdir_accepted(tmp_path: Path) -> None:
78 """A writable absolute tmp path should be accepted and the configured
79 base folder ('logs') appended if missing.
80 """
81 rl = RotaryLogger()
82 # tmp_path is absolute; _verify_user_log_path should append 'logs'
83 result = rl._verify_user_log_path(tmp_path)
84 assert result == tmp_path / CONST.LOG_FOLDER_BASE_NAME
85 # Directory should have been created
86 assert (tmp_path / CONST.LOG_FOLDER_BASE_NAME).exists()