Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 1 | from unittest import mock |
| 2 | |
| 3 | from tests.test_base import CfgCheckerTestBase |
| 4 | |
| 5 | |
| 6 | class TestCliCommands(CfgCheckerTestBase): |
| 7 | def test_do_cli_main_command(self): |
| 8 | _module_name = 'cfg_checker.cfg_check' |
| 9 | _m = self._try_import(_module_name) |
| 10 | with self.save_arguments(): |
| 11 | with self.redirect_output(): |
| 12 | with self.assertRaises(SystemExit) as ep: |
| 13 | import sys |
| 14 | sys.argv = ["fake.py", "reclass", "list", "-p", "/tmp"] |
| 15 | _m.cfg_check.config_check_entrypoint() |
| 16 | |
| 17 | self.assertEqual( |
| 18 | ep.exception.code, |
| 19 | 0, |
| 20 | "'mcp-checker reclass list -p /tmp' command failed" |
| 21 | ) |
| 22 | |
| 23 | def test_do_cli_main_command_debug(self): |
| 24 | _module_name = 'cfg_checker.cfg_check' |
| 25 | _m = self._try_import(_module_name) |
| 26 | with self.save_arguments(): |
| 27 | with self.redirect_output(): |
| 28 | with self.assertRaises(SystemExit) as ep: |
| 29 | import sys |
| 30 | sys.argv = [ |
| 31 | "fake.py", |
| 32 | "-d", |
| 33 | "reclass", |
| 34 | "list", |
| 35 | "-p", |
| 36 | "/tmp" |
| 37 | ] |
| 38 | _m.cfg_check.config_check_entrypoint() |
| 39 | |
| 40 | self.assertEqual( |
| 41 | ep.exception.code, |
| 42 | 0, |
| 43 | "mcp-checker command failes" |
| 44 | ) |
| 45 | |
| 46 | def test_cli_main_unknown_argument(self): |
| 47 | _module_name = 'cfg_checker.cfg_check' |
| 48 | _m = self._try_import(_module_name) |
| 49 | with self.redirect_output(): |
| 50 | with self.assertRaises(SystemExit) as ep: |
| 51 | import sys |
| 52 | sys.argv.append("reclass") |
| 53 | sys.argv.append("list") |
| 54 | _m.cfg_check.config_check_entrypoint() |
| 55 | |
| 56 | self.assertEqual( |
| 57 | ep.exception.code, |
| 58 | 1, |
| 59 | "Unknown argument not handled" |
| 60 | ) |
| 61 | |
| 62 | def test_do_cli_module_command(self): |
| 63 | _module_name = 'cfg_checker.cli.command' |
| 64 | _m = self._try_import(_module_name) |
| 65 | _command = "reclass" |
| 66 | with self.save_arguments(): |
| 67 | with self.redirect_output(): |
| 68 | with self.assertRaises(SystemExit) as ep: |
| 69 | import sys |
| 70 | sys.argv = ["fake.py", "list", "-p", "/tmp"] |
| 71 | _m.cli.command.cli_command( |
| 72 | "Fake Reclass Comparer", |
| 73 | _command |
| 74 | ) |
| 75 | |
| 76 | self.assertEqual( |
| 77 | ep.exception.code, |
| 78 | 0, |
| 79 | "Cli command execution failed" |
| 80 | ) |
| 81 | |
| 82 | def test_do_cli_module_command_with_error(self): |
| 83 | _module_name = 'cfg_checker.cli.command' |
| 84 | _m = self._try_import(_module_name) |
| 85 | _command = "reclass" |
| 86 | with self.save_arguments(): |
| 87 | with self.redirect_output(): |
| 88 | with self.assertRaises(SystemExit) as ep: |
| 89 | import sys |
| 90 | sys.argv = ["fake.py", "list", "-p", "/notexistingfolder"] |
| 91 | _m.cli.command.cli_command( |
| 92 | "Fake Reclass Comparer", |
| 93 | _command |
| 94 | ) |
| 95 | |
| 96 | self.assertEqual( |
| 97 | ep.exception.code, |
| 98 | 1, |
| 99 | "Cli command execution failed" |
| 100 | ) |
| 101 | |
| 102 | def test_cli_module_unknown_command(self): |
| 103 | _module_name = 'cfg_checker.cli.command' |
| 104 | _m = self._try_import(_module_name) |
| 105 | _fake_args = mock.MagicMock(name="FakeArgsClass") |
| 106 | _command = "unknowncommand" |
| 107 | with self.redirect_output(): |
| 108 | _r_value = _m.cli.command.execute_command(_fake_args, _command) |
| 109 | |
| 110 | self.assertEqual( |
| 111 | _r_value, |
| 112 | 1, |
| 113 | "Unknown command 'type' not handled" |
| 114 | ) |
| 115 | |
| 116 | def test_cli_module_no_type(self): |
| 117 | _module_name = 'cfg_checker.cli.command' |
| 118 | _m = self._try_import(_module_name) |
| 119 | _type = {} |
| 120 | _command = "unknowncommand" |
| 121 | with self.redirect_output(): |
| 122 | _r_value = _m.cli.command.execute_command(_type, _command) |
| 123 | |
| 124 | self.assertEqual( |
| 125 | _r_value, |
| 126 | 1, |
| 127 | "Unknown command not handled" |
| 128 | ) |
| 129 | |
| 130 | def test_cli_module_unknown_type(self): |
| 131 | _module_name = 'cfg_checker.cli.command' |
| 132 | _m = self._try_import(_module_name) |
| 133 | _fake_args = mock.MagicMock(name="FakeArgsClass") |
| 134 | _command = "reclass" |
| 135 | with self.redirect_output(): |
| 136 | _r_value = _m.cli.command.execute_command(_fake_args, _command) |
| 137 | |
| 138 | self.assertEqual( |
| 139 | _r_value, |
| 140 | 1, |
| 141 | "Unknown command not handled" |
| 142 | ) |
| 143 | |
| 144 | def test_cli_module_unknown_argument(self): |
| 145 | _module_name = 'cfg_checker.cli.command' |
| 146 | _m = self._try_import(_module_name) |
| 147 | _command = "reclass" |
| 148 | with self.redirect_output(): |
| 149 | with self.assertRaises(SystemExit) as ep: |
| 150 | _m.cli.command.cli_command( |
| 151 | "Fake Reclass Comparer", |
| 152 | _command |
| 153 | ) |
| 154 | |
| 155 | self.assertEqual( |
| 156 | ep.exception.code, |
| 157 | 1, |
| 158 | "Unknown argument not handled" |
| 159 | ) |