Skip to content

Commit

Permalink
Fixed test logging setup
Browse files Browse the repository at this point in the history
  • Loading branch information
coordt committed Apr 26, 2024
1 parent ec3cd99 commit 3777f27
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
3 changes: 0 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@
from click.testing import CliRunner
from pathlib import Path
from typing import Generator
from bumpversion.ui import setup_logging

import pytest

setup_logging(1)


@pytest.fixture
def tests_path() -> Path:
Expand Down
28 changes: 19 additions & 9 deletions tests/test_files.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""File processing tests."""

import logging
import os
import shutil
from datetime import datetime, timezone
Expand All @@ -15,6 +16,7 @@
from bumpversion.config.models import FileChange
from bumpversion.context import get_context
from bumpversion.exceptions import VersionNotFoundError
from bumpversion.ui import setup_logging, get_indented_logger
from bumpversion.version_part import VersionConfig
from tests.conftest import get_config_data, inside_dir

Expand Down Expand Up @@ -84,6 +86,10 @@ def test_raises_exception_if_file_not_found(self, tmp_path: Path) -> None:
def test_logs_missing_file_when_ignore_missing_file_set(self, tmp_path: Path, caplog) -> None:
"""The make_file_change method logs missing file when ignore_missing_file set to True."""
# Assemble
setup_logging(1)
caplog.set_level(logging.INFO)
logger = get_indented_logger(__name__)
logger.reset()
filepath = tmp_path / "file.md"
overrides = {
"current_version": "1.2.3",
Expand All @@ -99,14 +105,18 @@ def test_logs_missing_file_when_ignore_missing_file_set(self, tmp_path: Path, ca

# Assert
logs = caplog.messages
assert logs[0] == " Reading configuration"
assert logs[1] == " Configuration file not found: missing."
assert logs[2].endswith("file.md: replace `{current_version}` with `{new_version}`")
assert logs[3] == " File not found, but ignoring"
assert logs[0] == "Reading configuration"
assert logs[1] == " Configuration file not found: missing."
assert logs[2] == f"\nFile {filepath}: replace `{{current_version}}` with `{{new_version}}`"
assert logs[3] == " File not found, but ignoring"

def test_dedents_properly_when_file_does_not_contain_pattern(self, fixtures_path: Path, caplog) -> None:
"""The make_file_change method dedents the logging context when it does not contain a pattern."""
# Assemble
setup_logging(1)
caplog.set_level(logging.INFO)
logger = get_indented_logger(__name__)
logger.reset()
globs = fixtures_path / "glob" / "**/*.txt"
overrides = {
"current_version": "1.2.3",
Expand All @@ -126,16 +136,16 @@ def test_dedents_properly_when_file_does_not_contain_pattern(self, fixtures_path

# Assert
logs = caplog.messages
assert logs[0] == " Reading configuration"
assert logs[1] == " Configuration file not found: missing."
assert logs[0] == "Reading configuration"
assert logs[1] == " Configuration file not found: missing."
assert logs[2] == (
f" \n File {configured_file1.file_change.filename}: replace `{{current_version}}` with `{{new_version}}`"
f"\nFile {configured_file1.file_change.filename}: replace `{{current_version}}` with `{{new_version}}`"
)
assert logs[3] == (
f" \n File {configured_file2.file_change.filename}: replace `{{current_version}}` with `{{new_version}}`"
f"\nFile {configured_file2.file_change.filename}: replace `{{current_version}}` with `{{new_version}}`"
)
assert logs[4] == (
f" \n File {configured_file3.file_change.filename}: replace `{{current_version}}` with `{{new_version}}`"
f"\nFile {configured_file3.file_change.filename}: replace `{{current_version}}` with `{{new_version}}`"
)


Expand Down

0 comments on commit 3777f27

Please sign in to comment.