From 0f67041f72cfb08eb697f73f6d903223be695172 Mon Sep 17 00:00:00 2001 From: Cristiano Nicolai <570894+cristianonicolai@users.noreply.github.com> Date: Wed, 11 Oct 2023 23:52:37 +1000 Subject: [PATCH] Enable CSpell check (#188) Co-authored-by: Sorin Sbarnea --- .config/dictionary.txt | 37 ++++++++++++++++++++ .pre-commit-config.yaml | 5 +++ README.md | 12 +++---- cspell.config.yaml | 23 ++++++++++++ src/pytest_ansible/module_dispatcher/v213.py | 2 +- src/pytest_ansible/molecule.py | 6 ++-- src/pytest_ansible/plugin.py | 4 +-- src/pytest_ansible/units.py | 4 +-- tests/integration/test_molecule.py | 2 +- tests/test_adhoc.py | 4 +-- tests/test_params.py | 18 ++++++---- 11 files changed, 94 insertions(+), 23 deletions(-) create mode 100644 cspell.config.yaml diff --git a/.config/dictionary.txt b/.config/dictionary.txt index 3f89b3f9..fd926d97 100644 --- a/.config/dictionary.txt +++ b/.config/dictionary.txt @@ -1,9 +1,46 @@ argparsing +caplog cidrblock filterwarnings pathsep pytest +setenv +setuptools startpath +metafunc testpaths udring +webservers addoption +Rominger +Barroso +Laska +runas +runtask +pluginmanager +modifyitems +fixturenames +getfixturevalue +getplugin +pytestmark +autouse +rootpath +VIRSH +pytester +getgroup +addini +addinivalue_line +lineinfile +pandoc +funcargs +pytrace +reportinfo +fspath +NOTESTSCOLLECTED +TESTSFAILED +USAGEERROR +makepyfile +runpytest_subprocess +parseoutcomes +runpytest +errlines diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 34200940..0e8ca871 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -53,6 +53,11 @@ repos: hooks: - id: black language_version: python3 + - repo: https://github.com/streetsidesoftware/cspell-cli + rev: v7.3.1 + hooks: + - id: cspell + name: Spell check with cspell - repo: https://github.com/pycqa/pylint rev: v3.0.1 hooks: diff --git a/README.md b/README.md index 49bd61b3..84304a7c 100644 --- a/README.md +++ b/README.md @@ -263,7 +263,7 @@ def test_uptime(ansible_adhoc): The `HostManager` object returned by the `ansible_adhoc()` function provides numerous ways of calling ansible modules against some, or all, of the -inventory. The following demonstates sample usage. +inventory. The following demonstrates sample usage. ```python def test_host_manager(ansible_adhoc): @@ -301,11 +301,11 @@ such as cloud modules (ec2, gce etc...). def test_do_something_cloudy(localhost, ansible_adhoc): """Deploy an ec2 instance using multiple fixtures.""" params = dict( - key_name='somekey', + key_name='some_key', instance_type='t2.micro', image='ami-123456', wait=True, - group='webserver', + group='webservers', count=1, vpc_subnet_id='subnet-29e63245', assign_public_ip=True, @@ -395,7 +395,7 @@ def test_terminate_us_east_1_instances(ansible_adhoc): '''do some testing''' ``` -#### Parameterizing with `pytest.mark.ansible` +#### Parameterize with `pytest.mark.ansible` Perhaps the `--ansible-inventory=` includes many systems, but you only wish to interact with a subset. The `pytest.mark.ansible` marker can be @@ -477,7 +477,7 @@ def test_adhoc_result(ansible_adhoc): # With __getattr__ assert contacted.localhost.is_successful - # Or __gettem__ + # Or __getitem__ assert contacted['localhost'].is_successful ``` @@ -488,7 +488,7 @@ on that use by way of the `ModuleResult` interface. The `ModuleResult` class represents the dictionary returned by the ansible module for a particular host. The contents of the dictionary depend on the module called. -The `ModuleResult` interface provides some convenient proprerties to +The `ModuleResult` interface provides some convenient properties to determine the success of the module call. Examples are included below. ```python diff --git a/cspell.config.yaml b/cspell.config.yaml new file mode 100644 index 00000000..fc1d19f1 --- /dev/null +++ b/cspell.config.yaml @@ -0,0 +1,23 @@ +dictionaryDefinitions: + - name: words + path: .config/dictionary.txt + addWords: true +dictionaries: + - bash + - networking-terms + - python + - words + - en_US +ignorePaths: + # All dot files in the root + - \.* + # This file + - cspell.config.yaml + # The shared file for tool configuration + - pyproject.toml + # requirements files + - .config/requirements* + # The tox configuration file + - tox.ini +languageSettings: + - languageId: python diff --git a/src/pytest_ansible/module_dispatcher/v213.py b/src/pytest_ansible/module_dispatcher/v213.py index 2d13987a..328868d9 100644 --- a/src/pytest_ansible/module_dispatcher/v213.py +++ b/src/pytest_ansible/module_dispatcher/v213.py @@ -29,7 +29,7 @@ try: # init_plugin_loader was introduced in Ansible-core change here, v2.15 # https://github.com/ansible/ansible/pull/78915 - # Whenever a new vXYZ.py dispather module is introduced, make this static import + # Whenever a new vXYZ.py dispatcher module is introduced, make this static import # pylint: disable=ungrouped-imports from ansible.plugins.loader import init_plugin_loader except ImportError: diff --git a/src/pytest_ansible/molecule.py b/src/pytest_ansible/molecule.py index 31f79968..805eb50c 100644 --- a/src/pytest_ansible/molecule.py +++ b/src/pytest_ansible/molecule.py @@ -120,8 +120,8 @@ def __init__(self, name, parent) -> None: """Construct MoleculeItem.""" self.funcargs = {} super().__init__(name, parent) - moleculeyml = self.path - with Path(moleculeyml).open(encoding="utf-8") as stream: + molecule_yml = self.path + with Path(molecule_yml).open(encoding="utf-8") as stream: # If the molecule.yml file is empty, YAML loader returns None. To # simplify things down the road, we replace None with an empty # dict. @@ -227,7 +227,7 @@ def runtest(self): def reportinfo(self): """Return representation of test location when in verbose mode.""" - return self.fspath, 0, f"usecase: {self.name}" + return self.fspath, 0, f"use_case: {self.name}" def __str__(self) -> str: """Return name of the test.""" diff --git a/src/pytest_ansible/plugin.py b/src/pytest_ansible/plugin.py index 558579ae..8216fe05 100644 --- a/src/pytest_ansible/plugin.py +++ b/src/pytest_ansible/plugin.py @@ -164,7 +164,7 @@ def pytest_addoption(parser): "--ansible-unit-inject-only", action="store_true", default=False, - help="Enable support for ansible collection unit tests by only injecting exisiting ANSIBLE_COLLECTIONS_PATH.", + help="Enable support for ansible collection unit tests by only injecting existing ANSIBLE_COLLECTIONS_PATH.", ) group.addoption( "--molecule", @@ -323,7 +323,7 @@ def __init__(self, config) -> None: """Initialize plugin.""" self.config = config - def pytest_report_header(self, config, startdir): + def pytest_report_header(self): """Return the version of ansible.""" return f"ansible: {ansible.__version__}" diff --git a/src/pytest_ansible/units.py b/src/pytest_ansible/units.py index f984d0d9..5b8d1f1e 100644 --- a/src/pytest_ansible/units.py +++ b/src/pytest_ansible/units.py @@ -42,8 +42,8 @@ def get_collection_name(start_path: Path) -> tuple[str | None, str | None]: logger.info("Looking for collection info in %s", info_file) try: - with info_file.open(encoding="utf-8") as fhand: - galaxy_info = yaml.safe_load(fhand) + with info_file.open(encoding="utf-8") as file_handler: + galaxy_info = yaml.safe_load(file_handler) except FileNotFoundError: logger.error("No galaxy.yml file found, plugin not activated") return None, None diff --git a/tests/integration/test_molecule.py b/tests/integration/test_molecule.py index b4695ad3..dd6476d1 100644 --- a/tests/integration/test_molecule.py +++ b/tests/integration/test_molecule.py @@ -47,7 +47,7 @@ def test_molecule_disabled() -> None: def test_molecule_runtest() -> None: - """Test running the molecule scenarion via pytest.""" + """Test running the molecule scenario via pytest.""" try: proc = subprocess.run( f"{sys.executable} -m pytest --molecule tests/fixtures/molecule/default/molecule.yml", diff --git a/tests/test_adhoc.py b/tests/test_adhoc.py index 1c9a346a..59c4cbc4 100644 --- a/tests/test_adhoc.py +++ b/tests/test_adhoc.py @@ -170,7 +170,7 @@ def test_func(ansible_module): 'to create when becoming an unprivileged user') else: assert 'msg' in result, "Missing expected field in JSON response: msg" - assert 'sudo: unknown user: asdfasdf' in result['msg'] + assert 'sudo: unknown user: unknown_user' in result['msg'] """ testdir.makepyfile(src) @@ -183,7 +183,7 @@ def test_func(ansible_module): "localhost", # run against a single host "--ansible-become", # Enable become support "--ansible-become-user", - "asdfasdf", # Connect as asdfasdf + "unknown_user", # Connect as unknown_user ], ) assert result.ret == EXIT_OK diff --git a/tests/test_params.py b/tests/test_params.py index f1168da9..7dd8e2a9 100644 --- a/tests/test_params.py +++ b/tests/test_params.py @@ -192,7 +192,7 @@ def test_func(ansible_module): or parse_version(ansible.__version__) >= parse_version("2.4.0"), reason="requires ansible >= 2.0 and < 2.4", ) -def test_params_required_with_bogus_inventory_v2(testdir, option, recwarn): +def test_params_required_with_bogus_inventory_v2(testdir, option): src = """ import pytest def test_func(ansible_module): @@ -207,7 +207,7 @@ def test_func(ansible_module): ) as mock_exists: result = testdir.runpytest( *[ - "-vvvvvs", + *option.args, "--ansible-inventory", "bogus", "--ansible-host-pattern", @@ -224,7 +224,7 @@ def test_func(ansible_module): @pytest.mark.requires_ansible_v24() @pytest.mark.skipif(has_ansible_v28, reason="requires ansible < 2.8") -def test_params_required_with_bogus_inventory_v24(testdir, option, recwarn): +def test_params_required_with_bogus_inventory_v24(testdir, option): src = """ import pytest def test_func(ansible_module): @@ -239,7 +239,13 @@ def test_func(ansible_module): testdir.makepyfile(src) result = testdir.runpytest( - *["-vvvvvs", "--ansible-inventory", "bogus", "--ansible-host-pattern", "all"], + *[ + *option.args, + "--ansible-inventory", + "bogus", + "--ansible-host-pattern", + "all", + ], ) # Assert pytest exit code @@ -282,7 +288,7 @@ def test_func(ansible_module): assert result.ret == EXIT_OK -def test_param_override_with_marker(testdir): +def test_param_override_with_marker(testdir, option): src = """ import pytest @pytest.mark.ansible(inventory='local,', connection='local', host_pattern='all') @@ -292,7 +298,7 @@ def test_func(ansible_module): testdir.makepyfile(src) result = testdir.runpytest( *[ - "-vvvvvs", + *option.args, "--tb", "native", "--ansible-inventory",