"""Testing file 'cli/cli.py'."""
import importlib
import sys
import unittest
from pathlib import Path
from unittest.mock import patch
from click.testing import CliRunner
try:
from cli import cli
from cli.foxbms_version import __version__
from cli.helpers.package_helpers import PACKAGE_COMMANDS
except ModuleNotFoundError:
sys.path.insert(0, str(Path(__file__).parents[2]))
from cli import cli
from cli.foxbms_version import __version__
from cli.helpers.package_helpers import PACKAGE_COMMANDS
@patch("cli.helpers.misc.ROOT_IS_PROJECT", new=True)
class TestFoxCliMain(unittest.TestCase):
"""Test of the main entry point, when it is in the foxBMS project"""
@classmethod
def setUpClass(cls):
importlib.reload(cli)
def test_get_program_config(self):
"""Test '--show-config' option"""
foxbms_config = cli.get_program_config()
self.assertEqual({"foxBMS 2": __version__}, foxbms_config)
def test_main_no_args(self):
"""Test main entry point, when no commands are provided"""
runner = CliRunner()
result = runner.invoke(cli.main)
self.assertEqual(0, result.exit_code)
def test_main_show_config(self):
"""Test main entry point, when no commands are provided"""
runner = CliRunner()
result = runner.invoke(cli.main, ["--show-config"])
self.assertEqual(0, result.exit_code)
def test_install(self):
"""Check installation message"""
runner = CliRunner()
result = runner.invoke(cli.main, ["install"])
self.assertEqual(0, result.exit_code)
def test_install_check(self):
"""Check installation '--check'-option"""
runner = CliRunner()
runner.invoke(cli.main, ["install", "--check"])
def test_no_project(self):
"""Check main entry point, when it is not a foxBMS project"""
runner = CliRunner()
with patch("cli.helpers.misc.ROOT_IS_PROJECT", new=False):
importlib.reload(cli)
result = runner.invoke(cli.main, ["--help"])
self.assertEqual(0, result.exit_code)
for command in PACKAGE_COMMANDS["unsupported"]:
self.assertTrue((" " + command + " ") not in result.output)
for command in PACKAGE_COMMANDS["supported"]:
self.assertTrue((" " + command + " ") in result.output)
class TestFoxCliMainProject(unittest.TestCase):
"""Test configuration of the main entry point"""
def test_no_project(self):
"""Check configuration when ROOT_IS_PROJECT is False"""
with patch("cli.helpers.misc.ROOT_IS_PROJECT", new=False):
importlib.reload(cli)
command_names = cli.main.commands
for command in PACKAGE_COMMANDS["unsupported"]:
self.assertTrue(command not in command_names)
for command in PACKAGE_COMMANDS["supported"]:
self.assertTrue(command in command_names)
def test_project(self):
"""Check configuration ROOT_IS_PROJECT is True"""
with patch("cli.helpers.misc.ROOT_IS_PROJECT", new=True):
importlib.reload(cli)
command_names = cli.main.commands
for command in PACKAGE_COMMANDS["unsupported"]:
if command != "ci":
self.assertTrue(command in command_names)
for command in PACKAGE_COMMANDS["supported"]:
self.assertTrue(command in command_names)
if __name__ == "__main__":
unittest.main()