|
| 1 | +import sys |
| 2 | +from io import StringIO |
| 3 | +import json |
| 4 | +from contextlib import contextmanager |
| 5 | +import pkg_resources |
| 6 | +from unittest import TestCase |
| 7 | +from unittest.mock import mock_open |
| 8 | +from unittest.mock import patch |
| 9 | + |
| 10 | +from src.superannotate.lib.app.interface.cli_interface import CLIFacade |
| 11 | +from src.superannotate.lib.core import CONFIG_FILE_LOCATION |
| 12 | + |
| 13 | + |
| 14 | +try: |
| 15 | + CLI_VERSION = pkg_resources.get_distribution("superannotate").version |
| 16 | +except Exception: |
| 17 | + CLI_VERSION = None |
| 18 | + |
| 19 | + |
| 20 | +@contextmanager |
| 21 | +def catch_prints(): |
| 22 | + out = StringIO() |
| 23 | + sys.stdout = out |
| 24 | + yield out |
| 25 | + |
| 26 | + |
| 27 | +class CLITest(TestCase): |
| 28 | + |
| 29 | + @patch('builtins.input') |
| 30 | + def test_init_update(self, input_mock): |
| 31 | + input_mock.side_effect = ["y", "token"] |
| 32 | + with open(CONFIG_FILE_LOCATION) as f: |
| 33 | + config_data = f.read() |
| 34 | + with patch('builtins.open', mock_open(read_data=config_data)) as config_file: |
| 35 | + try: |
| 36 | + with catch_prints() as out: |
| 37 | + cli = CLIFacade() |
| 38 | + cli.init() |
| 39 | + except SystemExit: |
| 40 | + input_args = [i[0][0] for i in input_mock.call_args_list] |
| 41 | + self.assertIn(f"File {CONFIG_FILE_LOCATION} exists. Do you want to overwrite? [y/n] : ", input_args) |
| 42 | + input_mock.assert_called_with("Input the team SDK token from https://app.superannotate.com/team : ") |
| 43 | + config_file().write.assert_called_once_with( |
| 44 | + json.dumps( |
| 45 | + {"main_endpoint": "https://api.devsuperannotate.com", "ssl_verify": False, "token": "token"}, |
| 46 | + indent=4 |
| 47 | + ) |
| 48 | + ) |
| 49 | + self.assertEqual(out.getvalue().strip(), "Configuration file successfully updated.") |
| 50 | + |
| 51 | + @patch('builtins.input') |
| 52 | + def test_init_create(self, input_mock): |
| 53 | + input_mock.side_effect = ["token"] |
| 54 | + with patch('builtins.open', mock_open(read_data="{}")) as config_file: |
| 55 | + try: |
| 56 | + with catch_prints() as out: |
| 57 | + cli = CLIFacade() |
| 58 | + cli.init() |
| 59 | + except SystemExit: |
| 60 | + input_mock.assert_called_with("Input the team SDK token from https://app.superannotate.com/team : ") |
| 61 | + config_file().write.assert_called_once_with( |
| 62 | + json.dumps( |
| 63 | + {"token": "token"}, |
| 64 | + indent=4 |
| 65 | + ) |
| 66 | + ) |
| 67 | + self.assertEqual(out.getvalue().strip(), "Configuration file successfully created.") |
0 commit comments