@@ -29,7 +29,7 @@ def __init__(self, file: StringPathLike, **kwargs) -> None:
2929 suffix = Path (file ).suffix .lower ()
3030 if suffix not in self ._supported_formats :
3131 raise ValueError (f"Unsupported file format: { suffix } " )
32- self ._writer = can .Printer ( file = file )
32+ self ._writer = can .Logger ( filename = file )
3333
3434 @property
3535 def writer (self ) -> FileIOMessageWriter :
@@ -59,26 +59,20 @@ def test_attributes(self):
5959 assert hasattr (can .io .BaseRotatingLogger , "do_rollover" )
6060
6161 def test_get_new_writer (self , tmp_path ):
62- with self ._get_instance (tmp_path / "__unused.txt" ) as logger_instance :
63- writer = logger_instance ._get_new_writer (tmp_path / "file.ASC" )
64- assert isinstance (writer , can .ASCWriter )
65- writer .stop ()
62+ with self ._get_instance (tmp_path / "file.ASC" ) as logger_instance :
63+ assert isinstance (logger_instance .writer , can .ASCWriter )
6664
67- writer = logger_instance ._get_new_writer (tmp_path / "file.BLF" )
68- assert isinstance (writer , can .BLFWriter )
69- writer .stop ()
65+ with self ._get_instance (tmp_path / "file.BLF" ) as logger_instance :
66+ assert isinstance (logger_instance .writer , can .BLFWriter )
7067
71- writer = logger_instance ._get_new_writer (tmp_path / "file.CSV" )
72- assert isinstance (writer , can .CSVWriter )
73- writer .stop ()
68+ with self ._get_instance (tmp_path / "file.CSV" ) as logger_instance :
69+ assert isinstance (logger_instance .writer , can .CSVWriter )
7470
75- writer = logger_instance ._get_new_writer (tmp_path / "file.LOG" )
76- assert isinstance (writer , can .CanutilsLogWriter )
77- writer .stop ()
71+ with self ._get_instance (tmp_path / "file.LOG" ) as logger_instance :
72+ assert isinstance (logger_instance .writer , can .CanutilsLogWriter )
7873
79- writer = logger_instance ._get_new_writer (tmp_path / "file.TXT" )
80- assert isinstance (writer , can .Printer )
81- writer .stop ()
74+ with self ._get_instance (tmp_path / "file.TXT" ) as logger_instance :
75+ assert isinstance (logger_instance .writer , can .Printer )
8276
8377 def test_rotation_filename (self , tmp_path ):
8478 with self ._get_instance (tmp_path / "__unused.txt" ) as logger_instance :
@@ -89,63 +83,61 @@ def test_rotation_filename(self, tmp_path):
8983 assert logger_instance .rotation_filename (default_name ) == "default_by_namer"
9084
9185 def test_rotate_without_rotator (self , tmp_path ):
92- with self ._get_instance (tmp_path / "__unused.txt" ) as logger_instance :
93- source = str (tmp_path / "source.txt" )
94- dest = str (tmp_path / "dest.txt" )
86+ source = str (tmp_path / "source.txt" )
87+ dest = str (tmp_path / "dest.txt" )
9588
96- assert os .path .exists (source ) is False
97- assert os .path .exists (dest ) is False
89+ assert os .path .exists (source ) is False
90+ assert os .path .exists (dest ) is False
9891
99- logger_instance ._writer = logger_instance ._get_new_writer (source )
100- logger_instance .stop ()
92+ with self ._get_instance (source ) as logger_instance :
93+ # use context manager to create `source` file and close it
94+ pass
10195
102- assert os .path .exists (source ) is True
103- assert os .path .exists (dest ) is False
96+ assert os .path .exists (source ) is True
97+ assert os .path .exists (dest ) is False
10498
105- logger_instance .rotate (source , dest )
99+ logger_instance .rotate (source , dest )
106100
107- assert os .path .exists (source ) is False
108- assert os .path .exists (dest ) is True
101+ assert os .path .exists (source ) is False
102+ assert os .path .exists (dest ) is True
109103
110104 def test_rotate_with_rotator (self , tmp_path ):
111- with self ._get_instance (tmp_path / "__unused.txt" ) as logger_instance :
112- rotator_func = Mock ()
113- logger_instance .rotator = rotator_func
105+ source = str (tmp_path / "source.txt" )
106+ dest = str (tmp_path / "dest.txt" )
114107
115- source = str ( tmp_path / " source.txt" )
116- dest = str ( tmp_path / " dest.txt" )
108+ assert os . path . exists ( source ) is False
109+ assert os . path . exists ( dest ) is False
117110
118- assert os .path .exists (source ) is False
119- assert os .path .exists (dest ) is False
111+ with self ._get_instance (source ) as logger_instance :
112+ # use context manager to create `source` file and close it
113+ pass
120114
121- logger_instance ._writer = logger_instance ._get_new_writer (source )
122- logger_instance .stop ()
115+ rotator_func = Mock ()
116+ logger_instance .rotator = rotator_func
117+ logger_instance ._writer = logger_instance ._get_new_writer (source )
118+ logger_instance .stop ()
123119
124- assert os .path .exists (source ) is True
125- assert os .path .exists (dest ) is False
120+ assert os .path .exists (source ) is True
121+ assert os .path .exists (dest ) is False
126122
127- logger_instance .rotate (source , dest )
128- rotator_func .assert_called_with (source , dest )
123+ logger_instance .rotate (source , dest )
124+ rotator_func .assert_called_with (source , dest )
129125
130- # assert that no rotation was performed since rotator_func
131- # does not do anything
132- assert os .path .exists (source ) is True
133- assert os .path .exists (dest ) is False
126+ # assert that no rotation was performed since rotator_func
127+ # does not do anything
128+ assert os .path .exists (source ) is True
129+ assert os .path .exists (dest ) is False
134130
135131 def test_stop (self , tmp_path ):
136132 """Test if stop() method of writer is called."""
137133 with self ._get_instance (tmp_path / "file.ASC" ) as logger_instance :
138134 # replace stop method of writer with Mock
139- original_stop = logger_instance .writer .stop
140- mock_stop = Mock ()
135+ mock_stop = Mock (side_effect = logger_instance .writer .stop )
141136 logger_instance .writer .stop = mock_stop
142137
143138 logger_instance .stop ()
144139 mock_stop .assert_called ()
145140
146- # close file.ASC to enable cleanup of temp_dir
147- original_stop ()
148-
149141 def test_on_message_received (self , tmp_path ):
150142 with self ._get_instance (tmp_path / "file.ASC" ) as logger_instance :
151143 # Test without rollover
@@ -181,12 +173,9 @@ def test_on_message_received(self, tmp_path):
181173 writers_on_message_received .assert_called_with (msg )
182174
183175 def test_issue_1792 (self , tmp_path ):
184- with self ._get_instance (tmp_path / "__unused.log" ) as logger_instance :
185- writer = logger_instance ._get_new_writer (
186- tmp_path / "2017_Jeep_Grand_Cherokee_3.6L_V6.log"
187- )
188- assert isinstance (writer , can .CanutilsLogWriter )
189- writer .stop ()
176+ filepath = tmp_path / "2017_Jeep_Grand_Cherokee_3.6L_V6.log"
177+ with self ._get_instance (filepath ) as logger_instance :
178+ assert isinstance (logger_instance .writer , can .CanutilsLogWriter )
190179
191180
192181class TestSizedRotatingLogger :
0 commit comments