@@ -95,72 +95,104 @@ def test_factory_unknown_system(backend: str, monkeypatch: pytest.MonkeyPatch) -
9595 assert error == "System 'chuck norris' not (yet?) implemented."
9696
9797
98- @patch .object (sys , "argv" , new = []) # Prevent side effects while testing
98+ @pytest .fixture
99+ def reset_sys_argv (monkeypatch : pytest .MonkeyPatch ) -> None :
100+ monkeypatch .setattr (sys , "argv" , [])
101+
102+
103+ @pytest .mark .usefixtures ("reset_sys_argv" )
99104@pytest .mark .parametrize ("with_cursor" , [False , True ])
100- def test_entry_point (with_cursor : bool , capsys : pytest .CaptureFixture ) -> None :
101- def main (* args : str , ret : int = 0 ) -> None :
105+ class TestEntryPoint :
106+ """CLI entry-point scenarios split into focused tests."""
107+
108+ @staticmethod
109+ def _run_main (with_cursor : bool , * args : str , ret : int = 0 ) -> None :
102110 if with_cursor :
103111 args = (* args , "--with-cursor" )
104112 assert entry_point (* args ) == ret
105113
106- # No arguments
107- main ()
108- captured = capsys .readouterr ()
109- for mon , line in enumerate (captured .out .splitlines (), 1 ):
110- filename = Path (f"monitor-{ mon } .png" )
111- assert line .endswith (filename .name )
112- assert filename .is_file ()
113- filename .unlink ()
114-
115- file = Path ("monitor-1.png" )
116- for opt in ("-m" , "--monitor" ):
117- main (opt , "1" )
118- captured = capsys .readouterr ()
119- assert captured .out .endswith (f"{ file .name } \n " )
120- assert filename .is_file ()
121- filename .unlink ()
122-
123- for opts in zip (["-m 1" , "--monitor=1" ], ["-q" , "--quiet" ]):
124- main (* opts )
125- captured = capsys .readouterr ()
126- assert not captured .out
127- assert filename .is_file ()
128- filename .unlink ()
129-
130- fmt = "sct-{mon}-{width}x{height}.png"
131- for opt in ("-o" , "--out" ):
132- main (opt , fmt )
133- captured = capsys .readouterr ()
134- with mss .mss (display = os .getenv ("DISPLAY" )) as sct :
135- for mon , (monitor , line ) in enumerate (zip (sct .monitors [1 :], captured .out .splitlines ()), 1 ):
136- filename = Path (fmt .format (mon = mon , ** monitor ))
137- assert line .endswith (filename .name )
138- assert filename .is_file ()
139- filename .unlink ()
140-
141- fmt = "sct_{mon}-{date:%Y-%m-%d}.png"
142- for opt in ("-o" , "--out" ):
143- main ("-m 1" , opt , fmt )
144- filename = Path (fmt .format (mon = 1 , date = datetime .now (tz = UTC )))
145- captured = capsys .readouterr ()
146- assert captured .out .endswith (f"{ filename } \n " )
147- assert filename .is_file ()
148- filename .unlink ()
149-
150- coordinates = "2,12,40,67"
151- filename = Path ("sct-2x12_40x67.png" )
152- for opt in ("-c" , "--coordinates" ):
153- main (opt , coordinates )
154- captured = capsys .readouterr ()
155- assert captured .out .endswith (f"{ filename } \n " )
156- assert filename .is_file ()
157- filename .unlink ()
158-
159- coordinates = "2,12,40"
160- for opt in ("-c" , "--coordinates" ):
161- main (opt , coordinates , ret = 2 )
114+ def test_no_arguments (self , with_cursor : bool , capsys : pytest .CaptureFixture ) -> None :
115+ self ._run_main (with_cursor )
162116 captured = capsys .readouterr ()
163- assert captured .out == "Coordinates syntax: top, left, width, height\n "
117+ for mon , line in enumerate (captured .out .splitlines (), 1 ):
118+ filename = Path (f"monitor-{ mon } .png" )
119+ assert line .endswith (filename .name )
120+ assert filename .is_file ()
121+ filename .unlink ()
122+
123+ def test_monitor_option_and_quiet (self , with_cursor : bool , capsys : pytest .CaptureFixture ) -> None :
124+ file = Path ("monitor-1.png" )
125+ filename : Path | None = None
126+ for opt in ("-m" , "--monitor" ):
127+ self ._run_main (with_cursor , opt , "1" )
128+ captured = capsys .readouterr ()
129+ assert captured .out .endswith (f"{ file .name } \n " )
130+ filename = Path (captured .out .rstrip ())
131+ assert filename .is_file ()
132+ filename .unlink ()
133+
134+ assert filename is not None
135+ for opts in zip (["-m 1" , "--monitor=1" ], ["-q" , "--quiet" ]):
136+ self ._run_main (with_cursor , * opts )
137+ captured = capsys .readouterr ()
138+ assert not captured .out
139+ assert filename .is_file ()
140+ filename .unlink ()
141+
142+ def test_custom_output_pattern (self , with_cursor : bool , capsys : pytest .CaptureFixture ) -> None :
143+ fmt = "sct-{mon}-{width}x{height}.png"
144+ for opt in ("-o" , "--out" ):
145+ self ._run_main (with_cursor , opt , fmt )
146+ captured = capsys .readouterr ()
147+ with mss .mss (display = os .getenv ("DISPLAY" )) as sct :
148+ for mon , (monitor , line ) in enumerate (zip (sct .monitors [1 :], captured .out .splitlines ()), 1 ):
149+ filename = Path (fmt .format (mon = mon , ** monitor ))
150+ assert line .endswith (filename .name )
151+ assert filename .is_file ()
152+ filename .unlink ()
153+
154+ def test_output_pattern_with_date (self , with_cursor : bool , capsys : pytest .CaptureFixture ) -> None :
155+ fmt = "sct_{mon}-{date:%Y-%m-%d}.png"
156+ for opt in ("-o" , "--out" ):
157+ self ._run_main (with_cursor , "-m 1" , opt , fmt )
158+ filename = Path (fmt .format (mon = 1 , date = datetime .now (tz = UTC )))
159+ captured = capsys .readouterr ()
160+ assert captured .out .endswith (f"{ filename } \n " )
161+ assert filename .is_file ()
162+ filename .unlink ()
163+
164+ def test_coordinates_capture (self , with_cursor : bool , capsys : pytest .CaptureFixture ) -> None :
165+ coordinates = "2,12,40,67"
166+ filename = Path ("sct-2x12_40x67.png" )
167+ for opt in ("-c" , "--coordinates" ):
168+ self ._run_main (with_cursor , opt , coordinates )
169+ captured = capsys .readouterr ()
170+ assert captured .out .endswith (f"{ filename } \n " )
171+ assert filename .is_file ()
172+ filename .unlink ()
173+
174+ def test_invalid_coordinates (self , with_cursor : bool , capsys : pytest .CaptureFixture ) -> None :
175+ coordinates = "2,12,40"
176+ for opt in ("-c" , "--coordinates" ):
177+ self ._run_main (with_cursor , opt , coordinates , ret = 2 )
178+ captured = capsys .readouterr ()
179+ assert captured .out == "Coordinates syntax: top, left, width, height\n "
180+
181+ def test_backend_option (self , with_cursor : bool , capsys : pytest .CaptureFixture ) -> None :
182+ backend = "default"
183+ for opt in ("-b" , "--backend" ):
184+ self ._run_main (with_cursor , opt , backend , "-m1" )
185+ captured = capsys .readouterr ()
186+ filename = Path (captured .out .rstrip ())
187+ assert filename .is_file ()
188+ filename .unlink ()
189+
190+ def test_invalid_backend_option (self , with_cursor : bool , capsys : pytest .CaptureFixture ) -> None :
191+ backend = "chuck_norris"
192+ for opt in ("-b" , "--backend" ):
193+ self ._run_main (with_cursor , opt , backend , "-m1" , ret = 2 )
194+ captured = capsys .readouterr ()
195+ assert "argument -b/--backend: invalid choice: 'chuck_norris' (choose from" in captured .err
164196
165197
166198@patch .object (sys , "argv" , new = []) # Prevent side effects while testing
0 commit comments