|
| 1 | +# -------------------------------------------------------------------------------------------- |
| 2 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +# Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | +# -------------------------------------------------------------------------------------------- |
| 5 | +from __future__ import print_function |
| 6 | +import os |
| 7 | +import logging |
| 8 | +import unittest |
| 9 | +try: |
| 10 | + import mock |
| 11 | +except ImportError: |
| 12 | + from unittest import mock |
| 13 | +from six import StringIO |
| 14 | +import sys |
| 15 | + |
| 16 | +from knack.arguments import ArgumentsContext |
| 17 | +from knack.commands import CLICommandsLoader, CLICommand, CommandGroup |
| 18 | +from knack.config import CLIConfig |
| 19 | +from tests.util import DummyCLI, redirect_io |
| 20 | + |
| 21 | + |
| 22 | +# a dummy callback for arg-parse |
| 23 | +def load_params(_): |
| 24 | + pass |
| 25 | + |
| 26 | + |
| 27 | +def list_foo(my_param): |
| 28 | + print(str(my_param), end='') |
| 29 | + |
| 30 | + |
| 31 | +class TestCommandWithConfiguredDefaults(unittest.TestCase): |
| 32 | + |
| 33 | + @classmethod |
| 34 | + def setUpClass(cls): |
| 35 | + # Ensure initialization has occurred correctly |
| 36 | + logging.basicConfig(level=logging.DEBUG) |
| 37 | + |
| 38 | + @classmethod |
| 39 | + def tearDownClass(cls): |
| 40 | + logging.shutdown() |
| 41 | + |
| 42 | + def _set_up_command_table(self, required): |
| 43 | + |
| 44 | + class TestCommandsLoader(CLICommandsLoader): |
| 45 | + |
| 46 | + def load_command_table(self, args): |
| 47 | + super(TestCommandsLoader, self).load_command_table(args) |
| 48 | + with CommandGroup(self, 'foo', '{}#{{}}'.format(__name__)) as g: |
| 49 | + g.command('list', 'list_foo') |
| 50 | + return self.command_table |
| 51 | + |
| 52 | + def load_arguments(self, command): |
| 53 | + with ArgumentsContext(self, 'foo') as c: |
| 54 | + c.argument('my_param', options_list='--my-param', |
| 55 | + configured_default='param', required=required) |
| 56 | + super(TestCommandsLoader, self).load_arguments(command) |
| 57 | + self.cli_ctx = DummyCLI(commands_loader_cls=TestCommandsLoader) |
| 58 | + |
| 59 | + @mock.patch.dict(os.environ, {'CLI_DEFAULTS_PARAM': 'myVal'}) |
| 60 | + @redirect_io |
| 61 | + def test_apply_configured_defaults_on_required_arg(self): |
| 62 | + self._set_up_command_table(required=True) |
| 63 | + self.cli_ctx.invoke('foo list'.split()) |
| 64 | + actual = self.io.getvalue() |
| 65 | + expected = 'myVal' |
| 66 | + self.assertEqual(expected, actual) |
| 67 | + |
| 68 | + @redirect_io |
| 69 | + def test_no_configured_default_on_required_arg(self): |
| 70 | + self._set_up_command_table(required=True) |
| 71 | + with self.assertRaises(SystemExit): |
| 72 | + self.cli_ctx.invoke('foo list'.split()) |
| 73 | + actual = self.io.getvalue() |
| 74 | + expected = 'required: --my-param' |
| 75 | + if sys.version_info[0] == 2: |
| 76 | + expected = 'argument --my-param is required' |
| 77 | + self.assertEqual(expected in actual, True) |
| 78 | + |
| 79 | + @mock.patch.dict(os.environ, {'CLI_DEFAULTS_PARAM': 'myVal'}) |
| 80 | + @redirect_io |
| 81 | + def test_apply_configured_defaults_on_optional_arg(self): |
| 82 | + self._set_up_command_table(required=False) |
| 83 | + self.cli_ctx.invoke('foo list'.split()) |
| 84 | + actual = self.io.getvalue() |
| 85 | + expected = 'myVal' |
| 86 | + self.assertEqual(expected, actual) |
| 87 | + |
| 88 | + |
| 89 | +if __name__ == '__main__': |
| 90 | + unittest.main() |
0 commit comments