123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- import pathlib
- import subprocess
- from unittest.mock import patch
- import pytest
- import yaml
- # pylint: disable=protected-access
- import tooncher._cli
- def test_cli_help():
- proc_info = subprocess.run(
- ["tooncher", "--help"],
- check=True,
- stdout=subprocess.PIPE,
- stderr=subprocess.PIPE,
- )
- assert proc_info.stdout.startswith(b"usage: tooncher ")
- assert not proc_info.stderr
- @patch("tooncher._cli.run")
- @patch("os.environ", {})
- def test_engine_path_arg(run_mock):
- with patch("sys.argv", ["", "--engine-path", "/opt/ttr/TTREngine", "username"]):
- tooncher._cli.main()
- run_mock.assert_called_once()
- args, kwargs = run_mock.call_args
- assert not args
- assert kwargs["engine_path"] == "/opt/ttr/TTREngine"
- @patch("tooncher._cli.run")
- @patch("os.environ", {"TOONCHER_ENGINE_PATH": "/opt/ttr/TTREnvine"})
- def test_engine_path_env(run_mock):
- with patch("sys.argv", ["", "username"]):
- tooncher._cli.main()
- run_mock.assert_called_once()
- args, kwargs = run_mock.call_args
- assert not args
- assert kwargs["engine_path"] == "/opt/ttr/TTREnvine"
- @patch("tooncher._cli.run")
- @patch("os.environ", {"TOONCHER_ENGINE_PATH": "/opt/ttr/TTREnvine"})
- def test_engine_path_arg_env(run_mock):
- with patch("sys.argv", ["", "--engine-path", "/opt/ttr/TTREngine", "username"]):
- tooncher._cli.main()
- run_mock.assert_called_once()
- args, kwargs = run_mock.call_args
- assert not args
- assert kwargs["engine_path"] == "/opt/ttr/TTREngine"
- @patch("tooncher.launch")
- @patch("os.environ", {})
- def test_engine_path_config(launch_mock, tmpdir):
- config_file = tmpdir.join("config")
- config_file.write(
- yaml.safe_dump(
- {
- "engine_path": "/opt/conf/TTR",
- "accounts": [{"username": "someone", "password": "secret"}],
- }
- )
- )
- with patch("sys.argv", ["", "--config", config_file.strpath, "someone"]):
- tooncher._cli.main()
- launch_mock.assert_called_once()
- args, kwargs = launch_mock.call_args
- assert not args
- assert kwargs["engine_path"] == pathlib.Path("/opt/conf/TTR")
- @patch("tooncher.launch")
- @patch("os.environ", {"TOONCHER_ENGINE_PATH": "/opt/ttr/TTREnvine"})
- def test_engine_path_env_config(launch_mock, tmpdir):
- config_file = tmpdir.join("config")
- config_file.write(
- yaml.safe_dump(
- {
- "engine_path": "/opt/conf/TTR",
- "accounts": [{"username": "someone", "password": "secret"}],
- }
- )
- )
- with patch("sys.argv", ["", "--config", config_file.strpath, "someone"]):
- tooncher._cli.main()
- launch_mock.assert_called_once()
- args, kwargs = launch_mock.call_args
- assert not args
- assert kwargs["engine_path"] == pathlib.Path("/opt/ttr/TTREnvine")
- @patch("tooncher.launch")
- def test_account(launch_mock, tmpdir):
- config_file = tmpdir.join("config")
- config_file.write(
- yaml.safe_dump(
- {
- "engine_path": "/opt/conf/TTR",
- "accounts": [
- {"username": "someone", "password": "secret"},
- {"username": "toon", "password": "town"},
- ],
- }
- )
- )
- with patch("sys.argv", ["", "--config", config_file.strpath, "toon"]):
- tooncher._cli.main()
- launch_mock.assert_called_once_with(
- engine_path=pathlib.Path("/opt/conf/TTR"),
- username="toon",
- password="town",
- validate_ssl_certs=True,
- cpu_limit_percent=None,
- )
- def test_account_duplicate_username(tmpdir):
- config_file = tmpdir.join("config")
- config_file.write(
- yaml.safe_dump(
- {
- "engine_path": "/opt/conf/TTR",
- "accounts": [
- {"username": "someone", "password": "secret"},
- {"username": "toon", "password": "town"},
- {"username": "toon", "password": "town2"},
- ],
- }
- )
- )
- with patch("sys.argv", ["", "--config", config_file.strpath, "toon"]):
- with pytest.raises(ValueError, match=r"multiple .* username"):
- tooncher._cli.main()
- def test_account_unknown_username(tmpdir):
- config_file = tmpdir.join("config")
- config_file.write(
- yaml.safe_dump(
- {
- "engine_path": "/opt/conf/TTR",
- "accounts": [
- {"username": "someone", "password": "secret"},
- {"username": "toon", "password": "town"},
- ],
- }
- )
- )
- with patch("sys.argv", ["", "--config", config_file.strpath, "player"]):
- with pytest.raises(ValueError, match=r"not found"):
- tooncher._cli.main()
|