Fixing CLI formatter import test.

Signed-off-by: Jiri Podivin <jpodivin@redhat.com>
Change-Id: I66e23eb1c34cb48232b66df90b9141b0c613a69b
(cherry picked from commit df8393d16f)
This commit is contained in:
Jiri Podivin 2022-05-13 11:32:25 +02:00
parent 0a01c883df
commit 59e55fb25c
1 changed files with 11 additions and 13 deletions

View File

@ -13,9 +13,9 @@
# under the License.
#
from unittest import TestCase
from unittest import skipIf
import yaml
import sys
import importlib
import cliff
from validations_libs.cli import common
try:
@ -47,21 +47,19 @@ class TestCommon(TestCase):
def test_read_cli_data_file_yaml_error(self, mock_yaml):
self.assertRaises(RuntimeError, common.read_cli_data_file, 'foo')
@skipIf('_SmartHelpFormatter' not in dir(cliff.command),
"cliff package doesn't include _SmartHelpFormatter"
"in the 'command' submodule. Presumably cliff==2.16.0.")
@mock.patch('cliff._argparse', spec={})
def test_argparse_conditional_false(self, mock_argparse):
"""Test if the imporst are properly resolved based
"""Test if the imports are properly resolved based
on presence of the `SmartHelpFormatter` in the namespace
of the cliff._argparse.
If the attribute isn't in the namespace, and it shouldn't be
because the object is mocked to behave as a dictionary,
the next statement after conditional will trigger `ImportError`.
Because relevant class in the cliff module is mocked with side effect.
because the object is mocked to behave as a dictionary.
The final ValidationHelpFormatter class should have thus have
'cliff.command._SmartHelpFormatter' in it's inheritance chain.
Otherwise it should raise ImportError.
"""
sys.modules[
'cliff.command.SmartHelpFormatter'] = mock.MagicMock(
side_effect=ImportError)
self.assertRaises(
ImportError,
importlib.reload,
common)
self.assertTrue(cliff.command._SmartHelpFormatter in common.ValidationHelpFormatter.__mro__)