Split up the base_parser function

The goal of breaking apart the base_parser() function is to get rid of
a bunch of conditionals and parameters in the code and, instead, make
code look like simple composition.

When splitting, a choice had to be made as to whether this would operate
by side effect (modifying a passed in parser) or side effect-free
(returning a new parser everytime).

Making a version that's side-effect-free appears to be fighting with the
optparse API (it wants to work by creating a parser object, configuring
the object, and then parsing the arguments with it) so instead, make it
clear that our helper functions are modifying the passed in parser by
(1) not returning the parser and (2) changing the function names to be
more clear that it is operating by side-effect.

Also move all of the generic optparse code, along with the argument
context classes, into a new subdirectory.
This commit is contained in:
Toshio Kuratomi
2018-12-19 00:28:33 -08:00
parent afdbb0d9d5
commit 7e92ff823e
21 changed files with 545 additions and 486 deletions

View File

@@ -15,24 +15,25 @@ import optparse
import pytest
from ansible import arguments
from ansible.arguments import context_objects as co
from ansible.module_utils.common.collections import ImmutableDict
MAKE_IMMUTABLE_DATA = ((u'くらとみ', u'くらとみ'),
(42, 42),
({u'café': u'くらとみ'}, arguments.ImmutableDict({u'café': u'くらとみ'})),
({u'café': u'くらとみ'}, ImmutableDict({u'café': u'くらとみ'})),
([1, u'café', u'くらとみ'], (1, u'café', u'くらとみ')),
(set((1, u'café', u'くらとみ')), frozenset((1, u'café', u'くらとみ'))),
({u'café': [1, set(u'ñ')]},
arguments.ImmutableDict({u'café': (1, frozenset(u'ñ'))})),
ImmutableDict({u'café': (1, frozenset(u'ñ'))})),
([set((1, 2)), {u'くらとみ': 3}],
(frozenset((1, 2)), arguments.ImmutableDict({u'くらとみ': 3}))),
(frozenset((1, 2)), ImmutableDict({u'くらとみ': 3}))),
)
@pytest.mark.parametrize('data, expected', MAKE_IMMUTABLE_DATA)
def test_make_immutable(data, expected):
assert arguments._make_immutable(data) == expected
assert co._make_immutable(data) == expected
def test_cliargs_from_dict():
@@ -43,7 +44,7 @@ def test_cliargs_from_dict():
('check_mode', True),
('start_at_task', u'Start with くらとみ')))
assert frozenset(arguments.CLIArgs(old_dict).items()) == expected
assert frozenset(co.CLIArgs(old_dict).items()) == expected
def test_cliargs():
@@ -58,7 +59,7 @@ def test_cliargs():
('check_mode', True),
('start_at_task', u'Start with くらとみ')))
assert frozenset(arguments.CLIArgs.from_options(options).items()) == expected
assert frozenset(co.CLIArgs.from_options(options).items()) == expected
@pytest.mark.skipIf(argparse is None)
@@ -73,7 +74,7 @@ def test_cliargs_argparse():
expected = frozenset((('accumulate', sum), ('integers', (1, 2))))
assert frozenset(arguments.CLIArgs.from_options(args).items()) == expected
assert frozenset(co.CLIArgs.from_options(args).items()) == expected
# Can get rid of this test when we port ansible.cli from optparse to argparse
@@ -87,4 +88,4 @@ def test_cliargs_optparse():
expected = frozenset((('accumulate', sum), ('integers', (u'1', u'2'))))
assert frozenset(arguments.CLIArgs.from_options(opts).items()) == expected
assert frozenset(co.CLIArgs.from_options(opts).items()) == expected

View File

@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
# Copyright: (c) 2018, Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
# Make coding more python3-ish
from __future__ import (absolute_import, division)
__metaclass__ = type
import pytest
from ansible.arguments import optparse_helpers as opt_help
class TestOptparseHelpersVersion:
def test_version(self):
ver = opt_help.version('ansible-cli-test')
assert 'ansible-cli-test' in ver
assert 'python version' in ver

View File

@@ -31,11 +31,6 @@ from ansible import cli
class TestCliVersion(unittest.TestCase):
def test_version(self):
ver = cli.CLI.version('ansible-cli-test')
self.assertIn('ansible-cli-test', ver)
self.assertIn('python version', ver)
def test_version_info(self):
version_info = cli.CLI.version_info()
self.assertEqual(version_info['string'], __version__)

View File

@@ -26,8 +26,9 @@ import tarfile
import tempfile
import yaml
from ansible import arguments
from ansible import context
from ansible.arguments import context_objects as co
from ansible.arguments import optparse_helpers as opt_help
from ansible.cli.galaxy import GalaxyCLI
from units.compat import unittest
from units.compat.mock import call, patch
@@ -98,12 +99,12 @@ class TestGalaxy(unittest.TestCase):
def setUp(self):
# Reset the stored command line args
arguments.GlobalCLIArgs._Singleton__instance = None
co.GlobalCLIArgs._Singleton__instance = None
self.default_args = ['ansible-galaxy']
def tearDown(self):
# Reset the stored command line args
arguments.GlobalCLIArgs._Singleton__instance = None
co.GlobalCLIArgs._Singleton__instance = None
def test_init(self):
galaxy_cli = GalaxyCLI(args=self.default_args)
@@ -147,7 +148,7 @@ class TestGalaxy(unittest.TestCase):
# removing role
# Have to reset the arguments in the context object manually since we're doing the
# equivalent of running the command line program twice
arguments.GlobalCLIArgs._Singleton__instance = None
co.GlobalCLIArgs._Singleton__instance = None
gc = GalaxyCLI(args=["ansible-galaxy", "remove", role_file, self.role_name])
gc.run()
@@ -172,11 +173,11 @@ class TestGalaxy(unittest.TestCase):
self.assertTrue(mocked_display.called_once_with("- downloading role 'fake_role_name', owned by "))
def run_parse_common(self, galaxycli_obj, action):
with patch.object(ansible.cli.SortedOptParser, "set_usage") as mocked_usage:
with patch.object(opt_help.SortedOptParser, "set_usage") as mocked_usage:
galaxycli_obj.parse()
# checking that the common results of parse() for all possible actions have been created/called
self.assertIsInstance(galaxycli_obj.parser, ansible.cli.SortedOptParser)
self.assertIsInstance(galaxycli_obj.parser, opt_help.SortedOptParser)
formatted_call = {
'import': 'usage: %prog import [options] github_user github_repo',
'delete': 'usage: %prog delete [options] github_user github_repo',

View File

@@ -22,7 +22,7 @@ __metaclass__ = type
from units.compat import unittest
from units.compat.mock import MagicMock
from ansible import arguments
from ansible.arguments import context_objects as co
from ansible.executor.playbook_executor import PlaybookExecutor
from ansible.playbook import Playbook
from ansible.template import Templar
@@ -34,11 +34,11 @@ class TestPlaybookExecutor(unittest.TestCase):
def setUp(self):
# Reset command line args for every test
arguments.CLIArgs._Singleton__instance = None
co.GlobalCLIArgs._Singleton__instance = None
def tearDown(self):
# And cleanup after ourselves too
arguments.CLIArgs._Singleton__instance = None
co.GlobalCLIArgs._Singleton__instance = None
def test_get_serialized_batches(self):
fake_loader = DictDataLoader({

View File

@@ -21,8 +21,8 @@ from __future__ import (absolute_import, division, print_function)
from units.compat import unittest
from units.compat.mock import MagicMock
from ansible import arguments
from ansible import context
from ansible.arguments import context_objects as co
from ansible.executor.task_queue_manager import TaskQueueManager
from ansible.playbook import Playbook
from ansible.plugins.callback import CallbackBase
@@ -38,7 +38,7 @@ class TestTaskQueueManagerCallbacks(unittest.TestCase):
passwords = []
# Reset the stored command line args
arguments.GlobalCLIArgs._Singleton__instance = None
co.GlobalCLIArgs._Singleton__instance = None
self._tqm = TaskQueueManager(inventory, variable_manager, loader, passwords)
self._playbook = Playbook(loader)
@@ -51,7 +51,7 @@ class TestTaskQueueManagerCallbacks(unittest.TestCase):
def tearDown(self):
# Reset the stored command line args
arguments.GlobalCLIArgs._Singleton__instance = None
co.GlobalCLIArgs._Singleton__instance = None
def test_task_queue_manager_callbacks_v2_playbook_on_start(self):
"""

View File

@@ -11,10 +11,10 @@ import os
import pytest
from ansible import arguments
from ansible import constants as C
from ansible import context
from ansible import cli
from ansible.arguments import context_objects as co
from ansible.arguments import optparse_helpers as opt_help
from units.compat import unittest
from ansible.errors import AnsibleError, AnsibleParserError
from ansible.module_utils.six.moves import shlex_quote
@@ -25,19 +25,26 @@ from units.mock.loader import DictDataLoader
@pytest.fixture
def parser():
parser = cli.base_parser(runas_opts=True, meta_opts=True,
runtask_opts=True, vault_opts=True,
async_opts=True, connect_opts=True,
subset_opts=True, check_opts=True,
inventory_opts=True,)
parser = opt_help.create_base_parser()
opt_help.add_runas_options(parser)
opt_help.add_meta_options(parser)
opt_help.add_runtask_options(parser)
opt_help.add_vault_options(parser)
opt_help.add_async_options(parser)
opt_help.add_connect_options(parser)
opt_help.add_subset_options(parser)
opt_help.add_check_options(parser)
opt_help.add_inventory_options(parser)
return parser
@pytest.fixture
def reset_cli_args():
arguments.GlobalCLIArgs._Singleton__instance = None
co.GlobalCLIArgs._Singleton__instance = None
yield
arguments.GlobalCLIArgs._Singleton__instance = None
co.GlobalCLIArgs._Singleton__instance = None
def test_play_context(mocker, parser, reset_cli_args):