Skip to content

Commit

Permalink
Convert more getters to properties
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-hellings authored May 23, 2022
1 parent 0b8d4eb commit ac34617
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 37 deletions.
5 changes: 3 additions & 2 deletions src/tox_ansible/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Options(object):
def __init__(self, tox):
self.tox = tox
self.reader = tox.get_reader(INI_SECTION)
opts = tox.get_opts()
opts = tox.opts
self.scenario = self._parse_opt(opts, SCENARIO_OPTION_NAME, SCENARIO_ENV_NAME)
self.driver = self._parse_opt(opts, DRIVER_OPTION_NAME, DRIVER_ENV_NAME)
self.matrix = Matrix()
Expand Down Expand Up @@ -86,7 +86,8 @@ def do_filter(self):
no environments to execute against."""
return len(self.scenario) != 0 or len(self.driver) != 0

def get_global_opts(self):
@property
def global_opts(self):
opts = self.reader.getlist(INI_MOLECULE_GLOBAL_OPTS, sep="\n")
return opts

Expand Down
6 changes: 4 additions & 2 deletions src/tox_ansible/tox_ansible_test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ def get_name(self, fmt=""):
def description(self):
return f"Auto-generated for: ansible-test {self.command} {' '.join(self.args)}"

def get_dependencies(self):
@property
def dependencies(self):
return self._dependencies

def get_working_dir(self):
@property
def working_dir(self):
"""Get the directory where the test should be executed.
:return: Path where the test case should be executed from"""
Expand Down
3 changes: 2 additions & 1 deletion src/tox_ansible/tox_base_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ def __init__(self):
self.ansible = None
self._config = Tox()

def get_basepython(self):
@property
def basepython(self):
"""The python version that should be used to execute this, if a
particular one is requested. If not, then leave it up to the system
default. The name of the executable is arrived at simply by appending
Expand Down
9 changes: 5 additions & 4 deletions src/tox_ansible/tox_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ def toxinidir(self):
"""Returns the configured toxinidir for working with base directory paths"""
return self.config.toxinidir

def get_opts(self):
@property
def opts(self):
"""Return the options as a dictionary-style object.
:return: A dictionary of the command line options"""
Expand Down Expand Up @@ -128,7 +129,7 @@ def customize_envconfig(self, config, options):
config.commands = tox_case.get_commands(options)
# Default deps to install molecule, etc
do = DepOption()
processed_deps = do.postprocess(config, tox_case.get_dependencies())
processed_deps = do.postprocess(config, tox_case.dependencies)
if config.deps:
processed_deps = config.deps + processed_deps
config.deps = processed_deps
Expand All @@ -137,9 +138,9 @@ def customize_envconfig(self, config, options):
config.envdir = self.config.toxworkdir.join("ansible")
# Need to run molecule from the role directory
if not config.changedir or config.changedir == self.config.toxinidir:
config.changedir = py.path.local(tox_case.get_working_dir())
config.changedir = py.path.local(tox_case.working_dir)
if not config.basepython and tox_case.python is not None:
config.basepython = tox_case.get_basepython()
config.basepython = tox_case.basepython

if hasattr(config, "whitelist_externals"):
allowlist = "whitelist_externals"
Expand Down
6 changes: 4 additions & 2 deletions src/tox_ansible/tox_lint_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ def get_commands(self, options):
cmds.append(flake8)
return cmds

def get_working_dir(self):
@property
def working_dir(self):
return self._config.toxinidir

def get_dependencies(self):
@property
def dependencies(self):
deps = set(["flake8", "ansible-lint", "yamllint", "ansible"])
return deps

Expand Down
8 changes: 5 additions & 3 deletions src/tox_ansible/tox_molecule_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def get_commands(self, options):
:return: the default commands to run to execute this test case, if the
user does not configure them explicitly"""
molecule = ["molecule"]
molecule.extend(options.get_global_opts())
molecule.extend(options.global_opts)

if options.molecule_config_files:
for config_file in options.molecule_config_files:
Expand All @@ -64,13 +64,15 @@ def get_commands(self, options):
molecule.extend(tox.posargs)
return [molecule]

def get_working_dir(self):
@property
def working_dir(self):
"""Get the directory where the test should be executed.
:return: Path where the test case should be executed from"""
return os.path.dirname(os.path.dirname(self.scenario.directory))

def get_dependencies(self) -> Iterable:
@property
def dependencies(self) -> Iterable:
"""The dependencies for this particular test case.
:return: A list of the pip dependencies for this test case"""
Expand Down
8 changes: 4 additions & 4 deletions tests/test_tox_lint_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ def test_names_are_correct(mocker):
return_value="/home",
)
assert tc.get_name() == "lint_all"
assert tc.get_working_dir() == "/home"
assert tc.get_dependencies() == deps
assert tc.working_dir == "/home"
assert tc.dependencies == deps


def test_expand_python():
Expand All @@ -31,7 +31,7 @@ def test_expand_ansible():

def test_commands_are_correct(mocker):
options = Mock()
options.get_global_opts.return_value = []
options.global_opts = []
options.ansible_lint = None
options.yamllint = None
case1 = Mock(scenario=Scenario("molecule/s1"))
Expand All @@ -47,7 +47,7 @@ def test_commands_are_correct(mocker):

def test_lint_options_correct(mocker):
options = mocker.Mock()
options.get_global_opts.return_value = []
options.global_opts = []
options.ansible_lint = "some/path"
options.yamllint = "some/yaml.path"
bummer = ToxLintCase([])
Expand Down
47 changes: 28 additions & 19 deletions tests/test_tox_molecule_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,36 @@ def opts(mocker):


def test_case_is_simple(config, opts, scenario, mocker):
mocker.patch.object(Options, "get_global_opts", return_value=[])
mocker.patch.object(
Options, "global_opts", new_callable=mocker.PropertyMock, return_value=[]
)
mocker.patch.object(
Tox, "posargs", new_callable=mocker.PropertyMock, return_value=[]
)
t = ToxMoleculeCase(scenario)
opts.molecule_config_files = []
assert t.get_name() == "my_test"
assert t.get_working_dir() == ""
assert t.working_dir == ""
cmds = [["molecule", "test", "-s", scenario.name]]
assert t.get_commands(opts) == cmds
assert t.get_basepython() is None
assert t.basepython is None


def test_case_is_simple_with_config_files(config, opts, scenario, mocker):
base_configs = [
"/home/jdoe/my_ansible_collections/tests/molecule_one.yml",
"/home/jdoe/my_ansible_collections/tests/molecule_one.yml",
]
mocker.patch.object(Options, "get_global_opts", return_value=[])
mocker.patch.object(
Options, "global_opts", new_callable=mocker.PropertyMock, return_value=[]
)
mocker.patch.object(
Tox, "posargs", new_callable=mocker.PropertyMock, return_value=[]
)
t = ToxMoleculeCase(scenario)
opts.molecule_config_files = base_configs
assert t.get_name() == "my_test"
assert t.get_working_dir() == ""
assert t.working_dir == ""
cmds = [
[
"molecule",
Expand All @@ -67,11 +71,16 @@ def test_case_is_simple_with_config_files(config, opts, scenario, mocker):
]
]
assert t.get_commands(opts) == cmds
assert t.get_basepython() is None
assert t.basepython is None


def test_case_has_global_opts(mocker, scenario, opts, config):
mocker.patch.object(Options, "get_global_opts", return_value=["-c", "derp"])
mocker.patch.object(
Options,
"global_opts",
new_callable=mocker.PropertyMock,
return_value=["-c", "derp"],
)
mocker.patch.object(
Tox, "posargs", new_callable=mocker.PropertyMock, return_value=[]
)
Expand All @@ -87,8 +96,8 @@ def test_case_expand_ansible(scenario):
ts = t.expand_ansible("2.7")
assert ts.ansible == "2.7"
assert ts.get_name() == "ansible27-my_test"
assert "ansible==2.7.*" in ts.get_dependencies()
assert ts.get_basepython() is None
assert "ansible==2.7.*" in ts.dependencies
assert ts.basepython is None
assert "Auto-generated for: molecule test -s my_test" == ts.description


Expand All @@ -97,7 +106,7 @@ def test_case_expand_python(scenario):
ts = t.expand_python("4.1")
assert ts.python == "4.1"
assert ts.get_name() == "py41-my_test"
assert ts.get_basepython() == "python4.1"
assert ts.basepython == "python4.1"


def test_case_expand_twice(scenario):
Expand All @@ -109,28 +118,28 @@ def test_case_expand_twice(scenario):

def test_case_includes_docker_deps(scenario):
t = ToxMoleculeCase(scenario, drivers=["docker"])
assert "molecule-docker" in t.get_dependencies()
assert "molecule-podman" in t.get_dependencies()
assert "molecule-docker" in t.dependencies
assert "molecule-podman" in t.dependencies


def test_case_includes_openstack_deps(scenario):
t = ToxMoleculeCase(scenario, drivers=["openstack"])
assert "openstacksdk" in t.get_dependencies()
assert "moelcule-podman" not in t.get_dependencies()
assert "openstacksdk" in t.dependencies
assert "moelcule-podman" not in t.dependencies


def test_case_ignores_delegated_driver(scenario):
t = ToxMoleculeCase(scenario, drivers=["delegated"])
assert "molecule-delegated" not in t.get_dependencies()
assert "molecule-delegated" not in t.dependencies


def test_case_handles_unknown_driver(scenario):
t = ToxMoleculeCase(scenario, drivers=["derpy"])
assert "molecule-derpy" in t.get_dependencies()
assert "molecule-derpy" in t.dependencies


def test_case_for_multiple_drivers(scenario):
t = ToxMoleculeCase(scenario, drivers=["docker", "podman", "vagrant"])
assert "molecule-docker" in t.get_dependencies()
assert "molecule-podman" in t.get_dependencies()
assert "molecule-vagrant" in t.get_dependencies()
assert "molecule-docker" in t.dependencies
assert "molecule-podman" in t.dependencies
assert "molecule-vagrant" in t.dependencies

0 comments on commit ac34617

Please sign in to comment.