diff --git a/src/tox_ansible/options.py b/src/tox_ansible/options.py index c997581..7f27e83 100644 --- a/src/tox_ansible/options.py +++ b/src/tox_ansible/options.py @@ -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() @@ -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 diff --git a/src/tox_ansible/tox_ansible_test_case.py b/src/tox_ansible/tox_ansible_test_case.py index aeb996b..30e3621 100644 --- a/src/tox_ansible/tox_ansible_test_case.py +++ b/src/tox_ansible/tox_ansible_test_case.py @@ -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""" diff --git a/src/tox_ansible/tox_base_case.py b/src/tox_ansible/tox_base_case.py index 3d5eba1..eba744e 100644 --- a/src/tox_ansible/tox_base_case.py +++ b/src/tox_ansible/tox_base_case.py @@ -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 diff --git a/src/tox_ansible/tox_helper.py b/src/tox_ansible/tox_helper.py index 6e975be..e6d1c0f 100644 --- a/src/tox_ansible/tox_helper.py +++ b/src/tox_ansible/tox_helper.py @@ -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""" @@ -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 @@ -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" diff --git a/src/tox_ansible/tox_lint_case.py b/src/tox_ansible/tox_lint_case.py index d7ac479..052ab9f 100644 --- a/src/tox_ansible/tox_lint_case.py +++ b/src/tox_ansible/tox_lint_case.py @@ -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 diff --git a/src/tox_ansible/tox_molecule_case.py b/src/tox_ansible/tox_molecule_case.py index 9417422..6131a4f 100644 --- a/src/tox_ansible/tox_molecule_case.py +++ b/src/tox_ansible/tox_molecule_case.py @@ -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: @@ -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""" diff --git a/tests/test_tox_lint_case.py b/tests/test_tox_lint_case.py index ffb7639..2ebce26 100644 --- a/tests/test_tox_lint_case.py +++ b/tests/test_tox_lint_case.py @@ -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(): @@ -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")) @@ -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([]) diff --git a/tests/test_tox_molecule_case.py b/tests/test_tox_molecule_case.py index f80380c..23e0e3f 100644 --- a/tests/test_tox_molecule_case.py +++ b/tests/test_tox_molecule_case.py @@ -28,17 +28,19 @@ 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): @@ -46,14 +48,16 @@ def test_case_is_simple_with_config_files(config, opts, scenario, mocker): "/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", @@ -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=[] ) @@ -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 @@ -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): @@ -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