test_cli.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import pathlib
  2. import subprocess
  3. from unittest.mock import patch
  4. # pylint: disable=protected-access
  5. import tooncher._cli
  6. def test_cli_help():
  7. proc_info = subprocess.run(
  8. ["tooncher", "--help"],
  9. check=True,
  10. stdout=subprocess.PIPE,
  11. stderr=subprocess.PIPE,
  12. )
  13. assert b"optional arguments:" in proc_info.stdout
  14. assert not proc_info.stderr
  15. @patch("tooncher._cli.run")
  16. @patch("os.environ", {})
  17. def test_engine_path_arg(run_mock):
  18. with patch("sys.argv", ["", "--engine-path", "/opt/ttr/TTREngine", "username"]):
  19. tooncher._cli.main()
  20. run_mock.assert_called_once()
  21. args, kwargs = run_mock.call_args
  22. assert not args
  23. assert kwargs["engine_path"] == "/opt/ttr/TTREngine"
  24. @patch("tooncher._cli.run")
  25. @patch("os.environ", {"TOONCHER_ENGINE_PATH": "/opt/ttr/TTREnvine"})
  26. def test_engine_path_env(run_mock):
  27. with patch("sys.argv", ["", "username"]):
  28. tooncher._cli.main()
  29. run_mock.assert_called_once()
  30. args, kwargs = run_mock.call_args
  31. assert not args
  32. assert kwargs["engine_path"] == "/opt/ttr/TTREnvine"
  33. @patch("tooncher._cli.run")
  34. @patch("os.environ", {"TOONCHER_ENGINE_PATH": "/opt/ttr/TTREnvine"})
  35. def test_engine_path_arg_env(run_mock):
  36. with patch("sys.argv", ["", "--engine-path", "/opt/ttr/TTREngine", "username"]):
  37. tooncher._cli.main()
  38. run_mock.assert_called_once()
  39. args, kwargs = run_mock.call_args
  40. assert not args
  41. assert kwargs["engine_path"] == "/opt/ttr/TTREngine"
  42. @patch("tooncher.launch")
  43. @patch("os.environ", {})
  44. def test_engine_path_config(launch_mock, tmpdir):
  45. config_file = tmpdir.join("config")
  46. config_file.write(
  47. yaml.safe_dump(
  48. {
  49. "engine_path": "/opt/conf/TTR",
  50. "accounts": [{"username": "someone", "password": "secret"}],
  51. }
  52. )
  53. )
  54. with patch("sys.argv", ["", "--config", config_file.strpath, "someone"]):
  55. tooncher._cli.main()
  56. launch_mock.assert_called_once()
  57. args, kwargs = launch_mock.call_args
  58. assert not args
  59. assert kwargs["engine_path"] == pathlib.Path("/opt/conf/TTR")
  60. @patch("tooncher.launch")
  61. @patch("os.environ", {"TOONCHER_ENGINE_PATH": "/opt/ttr/TTREnvine"})
  62. def test_engine_path_env_config(launch_mock, tmpdir):
  63. config_file = tmpdir.join("config")
  64. config_file.write(
  65. yaml.safe_dump(
  66. {
  67. "engine_path": "/opt/conf/TTR",
  68. "accounts": [{"username": "someone", "password": "secret"}],
  69. }
  70. )
  71. )
  72. with patch("sys.argv", ["", "--config", config_file.strpath, "someone"]):
  73. tooncher._cli.main()
  74. launch_mock.assert_called_once()
  75. args, kwargs = launch_mock.call_args
  76. assert not args
  77. assert kwargs["engine_path"] == pathlib.Path("/opt/ttr/TTREnvine")