55import os
66import os .path
77import platform
8- from unittest .mock import patch
8+ import sys
9+ from datetime import datetime
10+ from unittest .mock import Mock , patch
911
1012import pytest
1113
1214import mss .tools
1315from mss import mss
16+ from mss .__main__ import main as entry_point
1417from mss .base import MSSBase
1518from mss .exception import ScreenShotError
1619from mss .screenshot import ScreenShot
@@ -78,46 +81,43 @@ def test_factory(monkeypatch):
7881 assert error == "System 'chuck norris' not (yet?) implemented."
7982
8083
84+ @patch .object (sys , "argv" , new = []) # Prevent side effects while testing
8185@pytest .mark .parametrize ("with_cursor" , [False , True ])
8286def test_entry_point (with_cursor : bool , capsys ):
83- from datetime import datetime
84-
85- from mss .__main__ import main as entry_point
86-
8787 def main (* args : str , ret : int = 0 ) -> None :
8888 if with_cursor :
8989 args = args + ("--with-cursor" ,)
9090 assert entry_point (* args ) == ret
9191
9292 # No arguments
9393 main ()
94- out , _ = capsys .readouterr ()
95- for mon , line in enumerate (out .splitlines (), 1 ):
94+ captured = capsys .readouterr ()
95+ for mon , line in enumerate (captured . out .splitlines (), 1 ):
9696 filename = f"monitor-{ mon } .png"
9797 assert line .endswith (filename )
9898 assert os .path .isfile (filename )
9999 os .remove (filename )
100100
101101 for opt in ("-m" , "--monitor" ):
102102 main (opt , "1" )
103- out , _ = capsys .readouterr ()
104- assert out .endswith ("monitor-1.png\n " )
103+ captured = capsys .readouterr ()
104+ assert captured . out .endswith ("monitor-1.png\n " )
105105 assert os .path .isfile ("monitor-1.png" )
106106 os .remove ("monitor-1.png" )
107107
108108 for opt in zip (["-m 1" , "--monitor=1" ], ["-q" , "--quiet" ]):
109109 main (* opt )
110- out , _ = capsys .readouterr ()
111- assert not out
110+ captured = capsys .readouterr ()
111+ assert not captured . out
112112 assert os .path .isfile ("monitor-1.png" )
113113 os .remove ("monitor-1.png" )
114114
115115 fmt = "sct-{mon}-{width}x{height}.png"
116116 for opt in ("-o" , "--out" ):
117117 main (opt , fmt )
118- out , _ = capsys .readouterr ()
118+ captured = capsys .readouterr ()
119119 with mss (display = os .getenv ("DISPLAY" )) as sct :
120- for mon , (monitor , line ) in enumerate (zip (sct .monitors [1 :], out .splitlines ()), 1 ):
120+ for mon , (monitor , line ) in enumerate (zip (sct .monitors [1 :], captured . out .splitlines ()), 1 ):
121121 filename = fmt .format (mon = mon , ** monitor )
122122 assert line .endswith (filename )
123123 assert os .path .isfile (filename )
@@ -127,47 +127,59 @@ def main(*args: str, ret: int = 0) -> None:
127127 for opt in ("-o" , "--out" ):
128128 main ("-m 1" , opt , fmt )
129129 filename = fmt .format (mon = 1 , date = datetime .now ())
130- out , _ = capsys .readouterr ()
131- assert out .endswith (filename + "\n " )
130+ captured = capsys .readouterr ()
131+ assert captured . out .endswith (filename + "\n " )
132132 assert os .path .isfile (filename )
133133 os .remove (filename )
134134
135135 coordinates = "2,12,40,67"
136136 filename = "sct-2x12_40x67.png"
137137 for opt in ("-c" , "--coordinates" ):
138138 main (opt , coordinates )
139- out , _ = capsys .readouterr ()
140- assert out .endswith (filename + "\n " )
139+ captured = capsys .readouterr ()
140+ assert captured . out .endswith (filename + "\n " )
141141 assert os .path .isfile (filename )
142142 os .remove (filename )
143143
144144 coordinates = "2,12,40"
145145 for opt in ("-c" , "--coordinates" ):
146146 main (opt , coordinates , ret = 2 )
147- out , _ = capsys .readouterr ()
148- assert out == "Coordinates syntax: top, left, width, height\n "
147+ captured = capsys .readouterr ()
148+ assert captured . out == "Coordinates syntax: top, left, width, height\n "
149149
150150
151+ @patch .object (sys , "argv" , new = []) # Prevent side effects while testing
151152@patch ("mss.base.MSSBase.monitors" , new = [])
152153@pytest .mark .parametrize ("quiet" , [False , True ])
153154def test_entry_point_error (quiet : bool , capsys ):
154- from mss .__main__ import main as entry_point
155-
156155 def main (* args : str ) -> int :
157156 if quiet :
158157 args = args + ("--quiet" ,)
159158 return entry_point (* args )
160159
161160 if quiet :
162161 assert main () == 1
163- out , err = capsys .readouterr ()
164- assert not out
165- assert not err
162+ captured = capsys .readouterr ()
163+ assert not captured . out
164+ assert not captured . err
166165 else :
167166 with pytest .raises (ScreenShotError ):
168167 main ()
169168
170169
170+ def test_entry_point_with_no_argument (capsys ):
171+ # Make sure to fail if arguments are not handled
172+ with patch ("mss.factory.mss" , new = Mock (side_effect = RuntimeError ("Boom!" ))):
173+ with patch .object (sys , "argv" , ["mss" , "--help" ]):
174+ with pytest .raises (SystemExit ) as exc :
175+ entry_point ()
176+ assert exc .value .code == 0
177+
178+ captured = capsys .readouterr ()
179+ assert not captured .err
180+ assert "usage: mss" in captured .out
181+
182+
171183def test_grab_with_tuple (pixel_ratio : int ):
172184 left = 100
173185 top = 100
0 commit comments