Skip to content

Commit a7a6bce

Browse files
authored
CLI: fix entry point not taking into account arguments (#255)
1 parent 88ecf47 commit a7a6bce

File tree

3 files changed

+41
-31
lines changed

3 files changed

+41
-31
lines changed

CHANGELOG.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
See Git checking messages for full history.
44

55
## 9.0.1 (2023/xx/xx)
6-
-
7-
- :heart: contributors: @
6+
- CLI: fixed entry point not taking into account arguments
87

98
## 9.0.0 (2023/04/18)
109
- Linux: add failure handling to `XOpenDisplay()` call (fixes #246)

src/mss/__main__.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Source: https://github.com/BoboTiG/python-mss
44
"""
55
import os.path
6+
import sys
67
from argparse import ArgumentParser
78

89
from . import __version__
@@ -14,7 +15,7 @@
1415
def main(*args: str) -> int:
1516
"""Main logic."""
1617

17-
cli_args = ArgumentParser()
18+
cli_args = ArgumentParser(prog="mss")
1819
cli_args.add_argument(
1920
"-c",
2021
"--coordinates",
@@ -42,7 +43,7 @@ def main(*args: str) -> int:
4243
)
4344
cli_args.add_argument("-v", "--version", action="version", version=__version__)
4445

45-
options = cli_args.parse_args(args)
46+
options = cli_args.parse_args(args or None)
4647
kwargs = {"mon": options.monitor, "output": options.output}
4748
if options.coordinates:
4849
try:
@@ -80,6 +81,4 @@ def main(*args: str) -> int:
8081

8182

8283
if __name__ == "__main__": # pragma: nocover
83-
import sys
84-
85-
sys.exit(main(*sys.argv[1:]))
84+
sys.exit(main())

src/tests/test_implementation.py

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
import os
66
import os.path
77
import platform
8-
from unittest.mock import patch
8+
import sys
9+
from datetime import datetime
10+
from unittest.mock import Mock, patch
911

1012
import pytest
1113

1214
import mss.tools
1315
from mss import mss
16+
from mss.__main__ import main as entry_point
1417
from mss.base import MSSBase
1518
from mss.exception import ScreenShotError
1619
from 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])
8286
def 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])
153154
def 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+
171183
def test_grab_with_tuple(pixel_ratio: int):
172184
left = 100
173185
top = 100

0 commit comments

Comments
 (0)