-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add tests for our CLI #877
Open
PerchunPak
wants to merge
12
commits into
master
Choose a base branch
from
cli-tests
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9d58e84
tests: add basic end-to-end tests for CLI
katrinafyi 4576a03
fix badly formatted string
katrinafyi c7b4be1
Merge branch 'py-mine:master' into cli-tests
katrinafyi 866515a
normalise --help output
katrinafyi 3d0c7a2
Make tests fully offline and improve coverage
PerchunPak fee4398
Address review
PerchunPak b62a763
Fix tests
PerchunPak 67a2608
Use repr for common errors
PerchunPak 42e63e1
Fix CI
PerchunPak 65ba25b
Raise `TimeoutError` instead of `socket.timeout` in mock
PerchunPak f5499c7
Catch `TimeoutError` in CLI as one of the common exceptions
PerchunPak c01c7ca
Clarify comment as requested in review
PerchunPak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,290 @@ | ||
import io | ||
import socket | ||
|
||
from mcstatus import JavaServer, BedrockServer | ||
from mcstatus.responses import JavaStatusResponse, BedrockStatusResponse, RawJavaResponse | ||
from mcstatus.querier import QueryResponse | ||
|
||
import os | ||
import json | ||
import contextlib | ||
from unittest import mock | ||
from unittest.mock import patch | ||
import pytest | ||
|
||
from mcstatus.__main__ import main as main_under_test, PING_PACKET_FAIL_WARNING, QUERY_FAIL_WARNING | ||
|
||
JAVA_RAW_RESPONSE: RawJavaResponse = { | ||
"players": {"max": 20, "online": 0}, | ||
"version": {"name": "1.8-pre1", "protocol": 44}, | ||
"description": "A Minecraft Server", | ||
"enforcesSecureChat": True, | ||
"favicon": "data:image/png;base64,foo", | ||
} | ||
|
||
QUERY_RAW_RESPONSE = [ | ||
{ | ||
"hostname": "A Minecraft Server", | ||
"gametype": "GAME TYPE", | ||
"game_id": "GAME ID", | ||
"version": "1.8", | ||
"plugins": "", | ||
"map": "world", | ||
"numplayers": "3", | ||
"maxplayers": "20", | ||
"hostport": "9999", | ||
"hostip": "192.168.56.1", | ||
}, | ||
["Dinnerbone", "Djinnibone", "Steve"], | ||
] | ||
|
||
BEDROCK_RAW_RESPONSE = [ | ||
"MCPE", | ||
"§r§4G§r§6a§r§ey§r§2B§r§1o§r§9w§r§ds§r§4e§r§6r", | ||
"422", | ||
"1.18.100500", | ||
"1", | ||
"69", | ||
"3767071975391053022", | ||
"map name here", | ||
"Default", | ||
"1", | ||
"19132", | ||
"-1", | ||
"3", | ||
] | ||
|
||
# NOTE: if updating this, be sure to change other occurrences of this help text! | ||
# to update, use: `COLUMNS=100000 poetry run mcstatus --help` | ||
EXPECTED_HELP_OUTPUT = """ | ||
usage: mcstatus [-h] [--bedrock] address {ping,status,query,json} ... | ||
|
||
mcstatus provides an easy way to query Minecraft servers for any information they can expose. It provides three modes of access: query, status, ping and json. | ||
|
||
positional arguments: | ||
address The address of the server. | ||
|
||
options: | ||
-h, --help show this help message and exit | ||
--bedrock Specifies that 'address' is a Bedrock server (default: Java). | ||
|
||
commands: | ||
Command to run, defaults to 'status'. | ||
|
||
{ping,status,query,json} | ||
ping Ping server for latency. | ||
status Prints server status. Supported by all Minecraft servers that are version 1.7 or higher. | ||
query Prints detailed server information. Must be enabled in servers' server.properties file. | ||
json Prints server status and query in json. Supported by all Minecraft servers that are version 1.7 or higher. | ||
""" # noqa: E501(line length) | ||
|
||
|
||
@contextlib.contextmanager | ||
def patch_stdout_stderr(): | ||
outpatch = patch("sys.stdout", new=io.StringIO()) | ||
errpatch = patch("sys.stderr", new=io.StringIO()) | ||
with outpatch as out, errpatch as err: | ||
yield out, err | ||
|
||
|
||
@pytest.fixture | ||
def mock_network_requests(): | ||
with \ | ||
patch("mcstatus.server.JavaServer.lookup", return_value=JavaServer("example.com", port=25565)), \ | ||
patch("mcstatus.server.JavaServer.ping", return_value=0), \ | ||
patch("mcstatus.server.JavaServer.status", return_value=JavaStatusResponse.build(JAVA_RAW_RESPONSE)), \ | ||
patch("mcstatus.server.JavaServer.query", return_value=QueryResponse(*QUERY_RAW_RESPONSE)), \ | ||
patch("mcstatus.server.BedrockServer.lookup", return_value=BedrockServer("example.com", port=25565)), \ | ||
patch("mcstatus.server.BedrockServer.status", return_value=( | ||
BedrockStatusResponse.build(BEDROCK_RAW_RESPONSE, latency=123) | ||
) | ||
): # fmt: skip # multiline with was added in Python 3.10 | ||
yield | ||
|
||
|
||
def normalise_help_output(s: str) -> str: | ||
""" | ||
Normalises the output of `mcstatus --help`, to work around | ||
some discrepancies between Python versions while still retaining | ||
meaningful information for comparison. | ||
""" | ||
|
||
elided = "[...]:" | ||
|
||
s = s.strip() | ||
|
||
# drop lines which end in ":". these argparse section headings vary between python versions. | ||
# it is just a small style change, so it doesn't matter so much to do `sys.version_info` check | ||
return "\n".join(ln if not ln.endswith(":") else elided for ln in s.splitlines()) | ||
|
||
|
||
# NOTE: for premature exits in argparse, we must catch SystemExit. | ||
# for ordinary exits in the CLI code, we can simply inspect the return value. | ||
|
||
|
||
def test_no_args(): | ||
with patch_stdout_stderr() as (out, err), pytest.raises(SystemExit) as exn: | ||
main_under_test([]) | ||
|
||
assert out.getvalue() == "" | ||
assert "usage: " in err.getvalue() | ||
assert exn.value.code != 0 | ||
|
||
|
||
def test_help(): | ||
with patch_stdout_stderr() as (out, err), pytest.raises(SystemExit) as exn: | ||
main_under_test(["--help"]) | ||
|
||
assert "usage: " in out.getvalue() | ||
assert err.getvalue() == "" | ||
assert exn.value.code == 0 | ||
|
||
|
||
@mock.patch.dict(os.environ, {"COLUMNS": "100000"}) # prevent line-wrapping in --help output | ||
def test_help_matches_recorded_output(): | ||
with patch_stdout_stderr() as (out, err), pytest.raises(SystemExit): | ||
main_under_test(["--help"]) | ||
|
||
assert normalise_help_output(out.getvalue()) == normalise_help_output(EXPECTED_HELP_OUTPUT) | ||
assert err.getvalue() == "" | ||
|
||
|
||
def test_one_argument_is_status(mock_network_requests): | ||
with patch_stdout_stderr() as (out, err): | ||
assert main_under_test(["example.com"]) == 0 | ||
|
||
assert ( | ||
"version: Java 1.8-pre1 (protocol 44)\n" | ||
"motd: \x1b[0mA Minecraft Server\x1b[0m\n" | ||
"players: 0/20 No players online\n" | ||
"ping: 0.00 ms\n" | ||
) == out.getvalue() | ||
assert err.getvalue() == "" | ||
|
||
|
||
def test_status(mock_network_requests): | ||
with patch_stdout_stderr() as (out, err): | ||
assert main_under_test(["example.com", "status"]) == 0 | ||
|
||
assert ( | ||
"version: Java 1.8-pre1 (protocol 44)\n" | ||
"motd: \x1b[0mA Minecraft Server\x1b[0m\n" | ||
"players: 0/20 No players online\n" | ||
"ping: 0.00 ms\n" | ||
) == out.getvalue() | ||
assert err.getvalue() == "" | ||
|
||
|
||
def test_status_bedrock(mock_network_requests): | ||
with patch_stdout_stderr() as (out, err): | ||
assert main_under_test(["example.com", "--bedrock", "status"]) == 0 | ||
|
||
assert ( | ||
"version: Bedrock 1.18.100500 (protocol 422)\n" | ||
"motd: \x1b[0m\x1b[0m\x1b[0m\x1b[38;2;170;0;0mG\x1b[0m\x1b[0m\x1b[38;2;255;170;0ma\x1b[0m\x1b[0m\x1b[38;2;255;255;85m" | ||
"y\x1b[0m\x1b[0m\x1b[38;2;0;170;0mB\x1b[0m\x1b[0m\x1b[38;2;0;0;170mo\x1b[0m\x1b[0m\x1b[38;2;85;85;255mw\x1b[0m\x1b[0m" | ||
"\x1b[38;2;255;85;255ms\x1b[0m\x1b[0m\x1b[38;2;170;0;0me\x1b[0m\x1b[0m\x1b[38;2;255;170;0mr\x1b[0m\n" | ||
"players: 1/69\n" | ||
"ping: 123.00 ms\n" | ||
) == out.getvalue() | ||
assert err.getvalue() == "" | ||
|
||
|
||
def test_status_offline(mock_network_requests): | ||
with patch_stdout_stderr() as (out, err), patch("mcstatus.server.JavaServer.status", side_effect=TimeoutError): | ||
assert main_under_test(["example.com", "status"]) == 1 | ||
|
||
assert out.getvalue() == "" | ||
assert err.getvalue() == "Error: TimeoutError()\n" | ||
|
||
|
||
def test_query(mock_network_requests): | ||
with patch_stdout_stderr() as (out, err): | ||
assert main_under_test(["example.com", "query"]) == 0 | ||
|
||
assert ( | ||
"host: 192.168.56.1:9999\n" | ||
"software: Java 1.8 vanilla\n" | ||
"motd: \x1b[0mA Minecraft Server\x1b[0m\n" | ||
"plugins: []\n" | ||
"players: 3/20 ['Dinnerbone', 'Djinnibone', 'Steve']\n" | ||
) == out.getvalue() | ||
assert err.getvalue() == "" | ||
|
||
|
||
def test_query_offline(mock_network_requests): | ||
with patch_stdout_stderr() as (out, err), patch("mcstatus.server.JavaServer.query", side_effect=socket.timeout): | ||
assert main_under_test(["example.com", "query"]) != 0 | ||
|
||
assert out.getvalue() == "" | ||
assert err.getvalue() == QUERY_FAIL_WARNING + "\n" | ||
|
||
|
||
def test_json(mock_network_requests): | ||
with patch_stdout_stderr() as (out, err): | ||
assert main_under_test(["example.com", "json"]) == 0 | ||
|
||
data = json.loads(out.getvalue()) | ||
assert data == { | ||
"online": True, | ||
"kind": "Java", | ||
"status": { | ||
"players": {"online": 0, "max": 20, "sample": None}, | ||
"version": {"name": "1.8-pre1", "protocol": 44}, | ||
"motd": "A Minecraft Server", | ||
"latency": 0, | ||
"raw": { | ||
"players": {"max": 20, "online": 0}, | ||
"version": {"name": "1.8-pre1", "protocol": 44}, | ||
"description": "A Minecraft Server", | ||
"enforcesSecureChat": True, | ||
"favicon": "data:image/png;base64,foo", | ||
}, | ||
"enforces_secure_chat": True, | ||
"icon": "data:image/png;base64,foo", | ||
"forge_data": None, | ||
}, | ||
"query": { | ||
"ip": "192.168.56.1", | ||
"port": "9999", | ||
"map": "world", | ||
"plugins": [], | ||
"raw": { | ||
"hostname": "A Minecraft Server", | ||
"gametype": "GAME TYPE", | ||
"game_id": "GAME ID", | ||
"version": "1.8", | ||
"plugins": "", | ||
"map": "world", | ||
"numplayers": "3", | ||
"maxplayers": "20", | ||
"hostport": "9999", | ||
"hostip": "192.168.56.1", | ||
}, | ||
}, | ||
} | ||
assert err.getvalue() == "" | ||
|
||
|
||
def test_ping(mock_network_requests): | ||
with patch_stdout_stderr() as (out, err): | ||
assert main_under_test(["example.com", "ping"]) == 0 | ||
|
||
assert float(out.getvalue()) == 0 | ||
assert err.getvalue() == "" | ||
|
||
|
||
def test_ping_bedrock(mock_network_requests): | ||
with patch_stdout_stderr() as (out, err): | ||
assert main_under_test(["example.com", "--bedrock", "ping"]) == 0 | ||
|
||
assert float(out.getvalue()) == 123 | ||
assert err.getvalue() == "" | ||
|
||
|
||
def test_ping_server_doesnt_support(mock_network_requests): | ||
with patch_stdout_stderr() as (out, err), patch("mcstatus.server.JavaServer.ping", side_effect=TimeoutError("timeout")): | ||
assert main_under_test(["example.com", "ping"]) == 0 | ||
|
||
assert float(out.getvalue()) == 0 | ||
assert err.getvalue() == PING_PACKET_FAIL_WARNING.format(address="example.com:25565", ping_exc="timeout") + "\n" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you elaborate on this? How do these differ across python versions? Would it be possible to instead add a
sys.version_info
conditional and only perform 1 replace for the older form to normalize into the newer one?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Switching on version is frowned upon and, for such a trivial change that's probably not explicitly documented, would require bisecting the versions.
You could maintain a manual map of replacements, I think these are very few in number. They only concern the "positional arguments", "required arguments", etc section headings.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, okay then, doing that does seem fairly annoying, so let's not go there. But it might be worth explaining this a bit more in that comment for people looking at the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really know how to clarify it more, is what is done in c01c7ca enough?