project_config.tests.pytest_plugin.plugin module

project-config pytest plugin.

project_config.tests.pytest_plugin.plugin.assert_data_report(monkeypatch: MonkeyPatch, tmp_path: Path) Any[source]

Pytest fixture to assert data reports.

Returns a function that can be used to assert a data report from a reporters module. See project_config.tests.pytest_plugin.plugin.project_config_data_report_asserter().

project_config.tests.pytest_plugin.plugin.assert_errors_report(monkeypatch: MonkeyPatch, tmp_path: Path) Any[source]

Pytest fixture to assert errors reports.

Returns a function that can be used to assert an errors report from a reporters module. See project_config.tests.pytest_plugin.plugin.project_config_errors_report_asserter().

project_config.tests.pytest_plugin.plugin.assert_project_config_plugin_action(tmp_path: Path) Any[source]

Pytest fixture to assert a plugin action.

Returns a function that can be used to assert a plugin action. See project_config.tests.pytest_plugin.plugin.project_config_plugin_action_asserter().

project_config.tests.pytest_plugin.plugin.project_config_data_report_asserter(monkeypatch: pytest.MonkeyPatch, rootdir: pathlib.Path, reporter_module: types.ModuleType, data_key: str, data: Any, expected_result: str, fmt: str | None = None) None[source]

Asserts a data report from a reporter module.

Parameters:
  • monkeypatch (pytest.MonkeyPatch) – Monkeypatch fixture. This is not needed to define in the fixture as is inserted before the execution.

  • rootdir (pathlib.Path) – Path to root directory. This is not needed to define in the fixture as is inserted before the execution.

  • reporter_module (ModuleType) – Module containing the reporters.

  • data_key (str) – Data key.

  • data (any) – Data content to report.

  • expected_result (str) – Expected reported result.

  • fmt (str, optional) – Format to use. Default value is None.

Example

import pytest

from project_config.reporters import default

@pytest.mark.parametrize(
    ("data_key", "data", "expected_result"),
    (
        pytest.param(
            "config",
            {
                "cache": "5 minutes",
                "style": "foo.json5",
            },
            '''cache: 5 minutes
style: foo.json5
''',
            id="config-style-string",
        ),
        pytest.param(
            "style",
            {
                "rules": [
                    {
                        "files": ["foo.ext", "bar.ext"],
                    },
                ],
            },
            '''rules:
  - files:
      - foo.ext
      - bar.ext
''',
            id="style-basic",
        ),
    ),
)
def test_default_data_report(
    data_key,
    data,
    expected_result,
    assert_data_report,
):
    assert_data_report(
        default,
        data_key,
        data,
        expected_result,
    )
project_config.tests.pytest_plugin.plugin.project_config_errors_report_asserter(monkeypatch: pytest.MonkeyPatch, rootdir: pathlib.Path, reporter_module: types.ModuleType, errors: list[ErrorDict], expected_result: str, fmt: str | None = None, color: bool = True) None[source]

Asserts an error report from a reporter module.

Parameters:
  • monkeypatch (pytest.MonkeyPatch) – Monkeypatch fixture. This is not needed to define in the fixture as is inserted before the execution.

  • rootdir (pathlib.Path) – Path to root directory. This is not needed to define in the fixture as is inserted before the execution.

  • reporter_module (types.ModuleType) – Module containing the reporters.

  • errors (list) – Errors.

  • expected_result (str) – Expected reported result.

  • fmt (str, optional) – Format to use. Default value is None.

  • color (bool, optional) – If True (default), the color version of the reporter will be tested also.

Example

import pytest

from project_config.reporters import default

@pytest.mark.parametrize(
    ("errors", "expected_result"),
    (
        pytest.param(
            [],
            "",
            id="empty",
        ),
        pytest.param(
            [
                {
                    "file": "foo.py",
                    "message": "message",
                    "definition": "definition",
                },
            ],
            "foo.py\n  - message definition",
            id="basic",
        ),
        pytest.param(
            [
                {
                    "file": "foo.py",
                    "message": "message 1",
                    "definition": "definition 1",
                },
                {
                    "file": "foo.py",
                    "message": "message 2",
                    "definition": "definition 2",
                },
                {
                    "file": "bar.py",
                    "message": "message 3",
                    "definition": "definition 3",
                },
            ],
            '''foo.py
  - message 1 definition 1
  - message 2 definition 2
bar.py
  - message 3 definition 3''',
            id="complex",
        ),
    ),
)
def test_default_errors_report(
    errors,
    expected_result,
    assert_errors_report,
):
    assert_errors_report(default, errors, expected_result)
project_config.tests.pytest_plugin.plugin.project_config_plugin_action_asserter(rootdir: RootdirType, plugin_class: type, plugin_method_name: str, files: FilesType, value: Any, rule: Rule, expected_results: list[StrictResultType], additional_files: FilesType | None = None, assert_case_method_name: bool = True, deprecated: bool = False, fix: bool = False, expected_files: FilesType | None = None) None[source]

Convenient function to test a plugin action.

Parameters:
  • rootdir (pathlib.Path) – Path to root directory. This is not needed to define in the fixture as is inserted before the execution.

  • plugin_class (type) – Plugin class.

  • plugin_method_name (str) – Plugin method name.

  • files (dict) – Dictionary of files to create. Must have the file paths as keys and the content as values. The keys will be passed to the files property of the rule. If the value is False, the file will not be created. If the value is None, the file will be created as a directory.

  • value (Any) – Value passed to the action.

  • rule (dict) – Rule definition.

  • expected_results (list) – Expected results.

  • additional_files (dict, optional) – Dictionary of additional files to create. These will not be defined inside the files property of the rule. Follows the same format as files.

  • assert_case_method_name (bool, optional) – If True (default), the method name will be checked to match against camelCase or PascalCase style.

  • deprecated (bool, optional) – If True, the action must raise a deprecation warning. Default value is False.

  • fix (bool, optional) – If True, the action will be called with the fix mode active. Use expected_files to check the content of the files after the fix is applyed.

  • expected_files (dict, optional) – Dictionary of expected files. Keys are the paths of the files and values are either strings for file contents (the content will be checked to be included as equality). an array of strings for partial file contents (will be checked to be included as subsets with an in operation), a boolean to skip the file (useful to pass files that doesn’t exist to plugins) or None to expect the existence of a directory.

Example

import pytest

from project_config import Error, InterruptingError, ResultValue
from project_config.plugins.include import IncludePlugin

@pytest.mark.parametrize(
    ("files", "value", "rule", "expected_results"),
    (
        pytest.param(
            {"foo.ext": "foo"},
            ["foo"],
            None,
            [],
            id="ok",
        ),
        pytest.param(
            {"foo.ext": "foo"},
            ["bar"],
            None,
            [
                (
                    Error,
                    {
                        "definition": ".includeLines[0]",
                        "file": "foo.ext",
                        "message": "Expected line 'bar' not found",
                    },
                ),
            ],
            id="error",
        ),
    ),
)
def test_includeLines(
    files,
    value,
    rule,
    expected_results,
    assert_project_config_plugin_action,
):
    assert_project_config_plugin_action(
        IncludePlugin,
        'includeLines',
        files,
        value,
        rule,
        expected_results,
    )