Rotary Logger  1.0.2
The middleware rotary logger
Loading...
Searching...
No Matches
test_file_instance.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_file_instance.py
24# CREATION DATE: 01-11-2025
25# LAST Modified: 3:42:18 04-03-2026
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 set of tests in charge of making sure the file_instance class works as expected.
31# // AR
32# +==== END rotary_logger =================+
33"""
34from pathlib import Path
35
36from rotary_logger.file_instance import FileInstance
37
38
39def _find_log_file(root: Path):
40 logs = list(root.rglob('*.log'))
41 if not logs:
42 return None
43 return logs[0]
44
45
46def test_file_instance_write_and_flush(tmp_path: Path) -> None:
47 # Use a file path (with .log suffix) to avoid the library opening a
48 # directory path which can result in inconsistent behaviour.
49 root = tmp_path / 'logs.log'
50 fi = FileInstance(root, max_size_mb=1)
51 try:
52 fi.write('hello file_instance\n')
53 fi.flush()
54 logfile = _find_log_file(tmp_path)
55 assert logfile is not None, 'No .log file created by FileInstance'
56 content = logfile.read_text(encoding=fi.get_encoding())
57 assert 'hello file_instance' in content
58 finally:
59 # best-effort cleanup
60 try:
61 if fi and fi.get_filepath():
62 fp = fi.get_filepath()
63 if fp and getattr(fp, 'descriptor', None):
64 try:
65 fp.descriptor.close()
66 except Exception:
67 pass
68 except Exception:
69 pass
70
71
72def test_file_instance_defaults(tmp_path: Path) -> None:
73 fi = FileInstance(None)
74 # ensure getters return sane defaults
75 assert fi.get_encoding() is not None
76 assert fi.get_flush_size() > 0
77 assert fi.get_max_size() > 0
78
79
81 """When log_to_file=False, no file should be created even after writing."""
82 log_path = tmp_path / 'should_not_exist.log'
83 fi = FileInstance(log_path, log_to_file=False)
84 fi.write('this should not be written to disk\n')
85 fi.flush()
86 assert not log_path.exists(), "File must not be created when log_to_file=False"
87
88
89def test_file_instance_copy_is_independent(tmp_path: Path) -> None:
90 """copy() should return a new instance with identical configuration but no shared state."""
91 fi = FileInstance(None, encoding='utf-16', merged=False)
92 fi.set_merge_stdin(True)
93 copy = fi.copy()
94 assert copy is not fi
95 assert copy.get_encoding() == 'utf-16'
96 assert copy.get_merged() is False
97 assert copy.get_merge_stdin() is True
98 # Mutating the copy must not affect the original
99 copy.set_encoding('utf-8')
100 assert fi.get_encoding() == 'utf-16'
101
102
103def test_file_instance_update_copies_config(tmp_path: Path) -> None:
104 """update() should copy all configuration fields from the source instance."""
105 src = FileInstance(None, encoding='latin-1', merged=False)
106 src.set_merge_stdin(True)
107 dst = FileInstance(None)
108 dst.update(src)
109 assert dst.get_encoding() == 'latin-1'
110 assert dst.get_merged() is False
111 assert dst.get_merge_stdin() is True
112
113
115 """set_merge_stdin / get_merge_stdin round-trip should be consistent."""
116 fi = FileInstance(None)
117 assert fi.get_merge_stdin() is False # default
118 fi.set_merge_stdin(True)
119 assert fi.get_merge_stdin() is True
120 fi.set_merge_stdin(False)
121 assert fi.get_merge_stdin() is False
None test_file_instance_log_to_file_false_suppresses_write(Path tmp_path)
None test_file_instance_defaults(Path tmp_path)
None test_file_instance_copy_is_independent(Path tmp_path)
None test_file_instance_update_copies_config(Path tmp_path)
None test_file_instance_write_and_flush(Path tmp_path)