Skip to content

Commit

Permalink
Increase test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea committed Feb 11, 2023
1 parent ea887d8 commit b2e271a
Show file tree
Hide file tree
Showing 14 changed files with 72 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ jobs:
WSLENV: FORCE_COLOR:PYTEST_REQPASS:TOXENV:GITHUB_STEP_SUMMARY
# Number of expected test passes, safety measure for accidental skip of
# tests. Update value if you add/remove tests.
PYTEST_REQPASS: 784
PYTEST_REQPASS: 788

steps:
- name: Activate WSL1
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ source = ["src", ".tox/*/site-packages"]
exclude_lines = ["pragma: no cover", "if TYPE_CHECKING:"]
omit = ["*/src/*/_vendor/*", "test/*"]
# Increase it just so it would pass on any single-python run
fail_under = 88
fail_under = 93
skip_covered = true
skip_empty = true
# During development we might remove code (files) with coverage data, and we dont want to fail:
Expand Down
2 changes: 1 addition & 1 deletion src/ansiblelint/_mockings.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def _perform_mockings() -> None: # noqa: C901
else:
path = f"{options.cache_dir}/roles/{role_name}"
# Avoid error from makedirs if destination is a broken symlink
if os.path.islink(path) and not os.path.exists(path):
if os.path.islink(path) and not os.path.exists(path): # pragma: no cover
_logger.warning("Removed broken symlink from %s", path)
os.unlink(path)
os.makedirs(path, exist_ok=True)
Expand Down
4 changes: 2 additions & 2 deletions src/ansiblelint/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ def __init__(
self.message = str(message or getattr(rule, "shortdesc", ""))

# Safety measure to ensure we do not end-up with incorrect indexes
if linenumber == 0:
if linenumber == 0: # pragma: no cover
raise RuntimeError(
"MatchError called incorrectly as line numbers start with 1"
)
if column == 0:
if column == 0: # pragma: no cover
raise RuntimeError(
"MatchError called incorrectly as column numbers start with 1"
)
Expand Down
3 changes: 0 additions & 3 deletions src/ansiblelint/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,6 @@ def is_excluded(self, lintable: Lintable) -> bool:

# Exclusions should be evaluated only using absolute paths in order
# to work correctly.
if not lintable:
return False

abs_path = str(lintable.abspath)
if self.project_dir and not abs_path.startswith(self.project_dir):
_logger.debug(
Expand Down
5 changes: 2 additions & 3 deletions src/ansiblelint/schemas/__main__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
"""Module containing cached JSON schemas."""
# pragma: no cover
import sys

from ansiblelint.schemas.main import refresh_schemas

if __name__ == "__main__":
if refresh_schemas():
if refresh_schemas(): # pragma: no cover
# flake8: noqa: T201
print("Schemas were updated.")
sys.exit(1)
else:
else: # pragma: no cover
# flake8: noqa: T201
print("Schemas not updated", 0)
2 changes: 1 addition & 1 deletion src/ansiblelint/schemas/__store__.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"ansible-lint-config": {
"etag": "88b1ad25b4c62f19e15be159696e9d57587a37b2351a543470c1cd601ea9cdf3",
"etag": "45ec120948f291620e297af0d75625ceb79d9295e2ec8b31652948c31ceeb209",
"url": "https://raw.githubusercontent.com/ansible/ansible-lint/main/src/ansiblelint/schemas/ansible-lint-config.json"
},
"ansible-navigator-config": {
Expand Down
17 changes: 3 additions & 14 deletions src/ansiblelint/schemas/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import json
import logging
import os
import sys
import time
import urllib.request
from collections import defaultdict
Expand Down Expand Up @@ -82,7 +81,7 @@ def refresh_schemas(min_age_seconds: int = 3600 * 24) -> int:
# never check for updated schemas more than once a day
if min_age_seconds > age:
return 0
if not os.access(store_file, os.W_OK):
if not os.access(store_file, os.W_OK): # pragma: no cover
_logger.debug(
"Skipping schema update due to lack of writing rights on %s", store_file
)
Expand Down Expand Up @@ -117,7 +116,7 @@ def refresh_schemas(min_age_seconds: int = 3600 * 24) -> int:
f_out.truncate()
os.fsync(f_out.fileno())
# unload possibly loaded schema
if kind in _schema_cache:
if kind in _schema_cache: # pragma: no cover
del _schema_cache[kind]
except (ConnectionError, OSError) as exc:
if (
Expand All @@ -129,7 +128,7 @@ def refresh_schemas(min_age_seconds: int = 3600 * 24) -> int:
# In case of networking issues, we just stop and use last-known good
_logger.debug("Skipped schema refresh due to unexpected exception: %s", exc)
break
if changed:
if changed: # pragma: no cover
with open(store_file, "w", encoding="utf-8") as f_out:
# formatting should match our .prettierrc.yaml
json.dump(JSON_SCHEMAS, f_out, indent=2, sort_keys=True)
Expand All @@ -140,13 +139,3 @@ def refresh_schemas(min_age_seconds: int = 3600 * 24) -> int:
store_file.touch()
changed = 1
return changed


if __name__ == "__main__":
if refresh_schemas():
# flake8: noqa: T201
print("Schemas were updated.")
sys.exit(1)
else:
# flake8: noqa: T201
print("Schemas not updated", 0)
3 changes: 3 additions & 0 deletions src/ansiblelint/testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ def run_ansible_lint(
# pollute the env, causing weird behaviors, so we pass only a safe list of
# vars.
safe_list = [
"COVERAGE_FILE",
"COVERAGE_PROCESS_START",
"HOME",
"LANG",
"LC_ALL",
Expand All @@ -123,6 +125,7 @@ def run_ansible_lint(
"PYTHONIOENCODING",
"PYTHONPATH",
"TERM",
"VIRTUAL_ENV",
]

if env is None:
Expand Down
9 changes: 9 additions & 0 deletions test/test_constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""Tests for constants module."""
from ansiblelint.constants import States


def test_states() -> None:
"""Test that states are evaluated as boolean false."""
assert bool(States.NOT_LOADED) is False
assert bool(States.LOAD_FAILED) is False
assert bool(States.UNKNOWN_DATA) is False
10 changes: 8 additions & 2 deletions test/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,18 @@
)
def test_call_from_outside_venv(expected_warning: bool) -> None:
"""Asserts ability to be called w/ or w/o venv activation."""
env = None

git_location = shutil.which("git")
assert git_location

if expected_warning:
env = {"HOME": str(Path.home()), "PATH": str(os.path.dirname(git_location))}
else:
env = os.environ.copy()

for v in ("COVERAGE_FILE", "COVERAGE_PROCESS_START"):
if v in os.environ:
env[v] = os.environ[v]

py_path = os.path.dirname(sys.executable)
# Passing custom env prevents the process from inheriting PATH or other
# environment variables from the current process, so we emulate being
Expand Down
13 changes: 13 additions & 0 deletions test/test_mockings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""Test mockings module."""
import pytest

from ansiblelint._mockings import _make_module_stub
from ansiblelint.constants import INVALID_CONFIG_RC


def test_make_module_stub() -> None:
"""Test make module stub."""
with pytest.raises(SystemExit) as exc:
_make_module_stub("")
assert exc.type == SystemExit
assert exc.value.code == INVALID_CONFIG_RC
24 changes: 23 additions & 1 deletion test/test_schemas.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
"""Test schemas modules."""
import logging
import subprocess
import sys
import urllib
from time import sleep
from typing import Any
from unittest.mock import DEFAULT, MagicMock, patch

import pytest

from ansiblelint.file_utils import Lintable
from ansiblelint.rules import RulesCollection
from ansiblelint.runner import Runner
from ansiblelint.schemas import refresh_schemas
from ansiblelint.schemas import refresh_schemas, validate_file_schema


def test_refresh_schemas() -> None:
Expand Down Expand Up @@ -75,3 +78,22 @@ def test_request_timeouterror_handling(
mock_request.urlopen.assert_called()
assert "Skipped schema refresh due to unexpected exception: " in caplog.text
assert error_msg in caplog.text


def test_schema_refresh_cli() -> None:
"""Ensure that we test the cli schema refresh command."""
proc = subprocess.run(
[sys.executable, "-m", "ansiblelint.schemas"],
check=False,
capture_output=True,
text=True,
)
assert proc.returncode == 0


def test_validate_file_schema() -> None:
"""Test file schema validation failure on unknown file kind."""
lintable = Lintable("foo.bar", kind="")
result = validate_file_schema(lintable)
assert len(result) == 1, result
assert "Unable to find JSON Schema" in result[0]
8 changes: 5 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,12 @@ setenv =
PRE_COMMIT_COLOR = always
FORCE_COLOR = 1
allowlist_externals =
find
git
pwd
rm
sh
tox
rm
pwd
# both options needed to workaround https://github.com/tox-dev/tox/issues/2197
usedevelop = false
skip_install = true
Expand Down Expand Up @@ -191,10 +192,11 @@ commands =
python3 -m pip uninstall -y ansible-lint

[testenv:clean]
description = Remove temporary files
skip_install = true
deps =
commands =
find . -type f -name '*.py[co]' -delete -o -type d -name __pycache__
find . -type f -name '*.py[co]' -delete -o -type d -name __pycache__ -name coverage.xml -name .coverage
rm -rf .mypy_cache

[testenv:coverage]
Expand Down

0 comments on commit b2e271a

Please sign in to comment.