Skip to content

test(fixtures): always run e2e tests in very verbose mode for test failure debugging #1258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,11 @@ def _run_cli(

cli_runner = CliRunner(mix_stderr=False)
env_vars = {**clean_os_environment, **(env or {})}
args = ["-vv", *(argv or [])]

with mock.patch.dict(os.environ, env_vars, clear=True):
# run the CLI with the provided arguments
return cli_runner.invoke(main, args=(argv or []), **(invoke_kwargs or {}))
return cli_runner.invoke(main, args=args, **(invoke_kwargs or {}))

return _run_cli

Expand Down
7 changes: 5 additions & 2 deletions tests/e2e/cmd_version/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from requests_mock import Mocker

from tests.conftest import RunCliFn
from tests.e2e.conftest import StripLoggingMessagesFn
from tests.fixtures.example_project import GetWheelFileFn, UpdatePyprojectTomlFn
from tests.fixtures.git_repo import BuiltRepoResult, GetVersionsFromRepoBuildDefFn

Expand Down Expand Up @@ -151,6 +152,7 @@ def test_version_on_nonrelease_branch(
run_cli: RunCliFn,
mocked_git_push: MagicMock,
post_mocker: Mocker,
strip_logging_messages: StripLoggingMessagesFn,
):
"""
Given repo is on a non-release branch,
Expand All @@ -175,7 +177,7 @@ def test_version_on_nonrelease_branch(
# Evaluate (expected -> actual)
assert_successful_exit_code(result, cli_cmd)
assert not result.stdout
assert expected_error_msg == result.stderr
assert expected_error_msg == strip_logging_messages(result.stderr)

# assert nothing else happened (no code changes, no commit, no tag, no push, no vcs release)
tags_after = sorted([tag.name for tag in repo.tags])
Expand All @@ -196,6 +198,7 @@ def test_version_on_last_release(
run_cli: RunCliFn,
mocked_git_push: MagicMock,
post_mocker: Mocker,
strip_logging_messages: StripLoggingMessagesFn,
):
"""
Given repo is on the last release version,
Expand Down Expand Up @@ -229,7 +232,7 @@ def test_version_on_last_release(
# Evaluate (expected -> actual)
assert_successful_exit_code(result, cli_cmd)
assert f"{latest_release_version}\n" == result.stdout
assert f"{expected_error_msg}\n" == result.stderr
assert f"{expected_error_msg}\n" == strip_logging_messages(result.stderr)

# assert nothing else happened (no code changes, no commit, no tag, no push, no vcs release)
assert repo_status_before == repo_status_after
Expand Down
31 changes: 21 additions & 10 deletions tests/e2e/cmd_version/test_version_print.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from requests_mock import Mocker

from tests.conftest import RunCliFn
from tests.e2e.conftest import StripLoggingMessagesFn
from tests.fixtures.git_repo import (
BuiltRepoResult,
GetCfgValueFromDefFn,
Expand Down Expand Up @@ -442,6 +443,7 @@ def test_version_print_last_released_prints_version(
run_cli: RunCliFn,
mocked_git_push: MagicMock,
post_mocker: Mocker,
strip_logging_messages: StripLoggingMessagesFn,
):
repo = repo_result["repo"]
latest_release_version = get_versions_from_repo_build_def(
Expand All @@ -465,7 +467,7 @@ def test_version_print_last_released_prints_version(

# Evaluate
assert_successful_exit_code(result, cli_cmd)
assert not result.stderr
assert not strip_logging_messages(result.stderr)
assert f"{latest_release_version}\n" == result.stdout

# assert nothing else happened (no code changes, no commit, no tag, no push, no vcs release)
Expand Down Expand Up @@ -493,6 +495,7 @@ def test_version_print_last_released_prints_released_if_commits(
mocked_git_push: MagicMock,
post_mocker: Mocker,
file_in_repo: str,
strip_logging_messages: StripLoggingMessagesFn,
):
repo = repo_result["repo"]
latest_release_version = get_versions_from_repo_build_def(
Expand Down Expand Up @@ -520,7 +523,7 @@ def test_version_print_last_released_prints_released_if_commits(

# Evaluate
assert_successful_exit_code(result, cli_cmd)
assert not result.stderr
assert not strip_logging_messages(result.stderr)
assert f"{latest_release_version}\n" == result.stdout

# assert nothing else happened (no code changes, no commit, no tag, no push, no vcs release)
Expand Down Expand Up @@ -585,6 +588,7 @@ def test_version_print_last_released_on_detached_head(
run_cli: RunCliFn,
mocked_git_push: MagicMock,
post_mocker: Mocker,
strip_logging_messages: StripLoggingMessagesFn,
):
repo = repo_result["repo"]
latest_release_version = get_versions_from_repo_build_def(
Expand All @@ -611,7 +615,7 @@ def test_version_print_last_released_on_detached_head(

# Evaluate (expected -> actual)
assert_successful_exit_code(result, cli_cmd)
assert not result.stderr
assert not strip_logging_messages(result.stderr)
assert f"{latest_release_version}\n" == result.stdout

# assert nothing else happened (no code changes, no commit, no tag, no push, no vcs release)
Expand All @@ -632,6 +636,7 @@ def test_version_print_last_released_on_nonrelease_branch(
run_cli: RunCliFn,
mocked_git_push: MagicMock,
post_mocker: Mocker,
strip_logging_messages: StripLoggingMessagesFn,
):
repo = repo_result["repo"]
latest_release_version = get_versions_from_repo_build_def(
Expand All @@ -658,7 +663,7 @@ def test_version_print_last_released_on_nonrelease_branch(

# Evaluate (expected -> actual)
assert_successful_exit_code(result, cli_cmd)
assert not result.stderr
assert not strip_logging_messages(result.stderr)
assert f"{latest_release_version}\n" == result.stdout

# assert nothing else happened (no code changes, no commit, no tag, no push, no vcs release)
Expand Down Expand Up @@ -688,6 +693,7 @@ def test_version_print_last_released_tag_prints_correct_tag(
run_cli: RunCliFn,
mocked_git_push: MagicMock,
post_mocker: Mocker,
strip_logging_messages: StripLoggingMessagesFn,
):
repo = repo_result["repo"]
repo_def = repo_result["definition"]
Expand All @@ -712,7 +718,7 @@ def test_version_print_last_released_tag_prints_correct_tag(

# Evaluate
assert_successful_exit_code(result, cli_cmd)
assert not result.stderr
assert not strip_logging_messages(result.stderr)
assert f"{latest_release_tag}\n" == result.stdout

# assert nothing else happened (no code changes, no commit, no tag, no push, no vcs release)
Expand Down Expand Up @@ -748,6 +754,7 @@ def test_version_print_last_released_tag_prints_released_if_commits(
mocked_git_push: MagicMock,
post_mocker: Mocker,
file_in_repo: str,
strip_logging_messages: StripLoggingMessagesFn,
):
repo = repo_result["repo"]
repo_def = repo_result["definition"]
Expand Down Expand Up @@ -776,7 +783,7 @@ def test_version_print_last_released_tag_prints_released_if_commits(

# Evaluate
assert_successful_exit_code(result, cli_cmd)
assert not result.stderr
assert not strip_logging_messages(result.stderr)
assert f"{latest_release_tag}\n" == result.stdout

# assert nothing else happened (no code changes, no commit, no tag, no push, no vcs release)
Expand Down Expand Up @@ -850,6 +857,7 @@ def test_version_print_last_released_tag_on_detached_head(
run_cli: RunCliFn,
mocked_git_push: MagicMock,
post_mocker: Mocker,
strip_logging_messages: StripLoggingMessagesFn,
):
repo = repo_result["repo"]
repo_def = repo_result["definition"]
Expand Down Expand Up @@ -877,7 +885,7 @@ def test_version_print_last_released_tag_on_detached_head(

# Evaluate (expected -> actual)
assert_successful_exit_code(result, cli_cmd)
assert not result.stderr
assert not strip_logging_messages(result.stderr)
assert f"{latest_release_tag}\n" == result.stdout

# assert nothing else happened (no code changes, no commit, no tag, no push, no vcs release)
Expand Down Expand Up @@ -907,6 +915,7 @@ def test_version_print_last_released_tag_on_nonrelease_branch(
run_cli: RunCliFn,
mocked_git_push: MagicMock,
post_mocker: Mocker,
strip_logging_messages: StripLoggingMessagesFn,
):
repo = repo_result["repo"]
repo_def = repo_result["definition"]
Expand Down Expand Up @@ -934,7 +943,7 @@ def test_version_print_last_released_tag_on_nonrelease_branch(

# Evaluate (expected -> actual)
assert_successful_exit_code(result, cli_cmd)
assert not result.stderr
assert not strip_logging_messages(result.stderr)
assert f"{last_release_tag}\n" == result.stdout

# assert nothing else happened (no code changes, no commit, no tag, no push, no vcs release)
Expand All @@ -961,6 +970,7 @@ def test_version_print_next_version_fails_on_detached_head(
get_commit_def_fn: GetCommitDefFn,
mocked_git_push: MagicMock,
post_mocker: Mocker,
strip_logging_messages: StripLoggingMessagesFn,
):
repo = repo_result["repo"]
expected_error_msg = (
Expand Down Expand Up @@ -994,7 +1004,7 @@ def test_version_print_next_version_fails_on_detached_head(
# Evaluate (expected -> actual)
assert_exit_code(1, result, cli_cmd)
assert not result.stdout
assert f"{expected_error_msg}\n" == result.stderr
assert f"{expected_error_msg}\n" == strip_logging_messages(result.stderr)

# assert nothing else happened (no code changes, no commit, no tag, no push, no vcs release)
assert repo_status_before == repo_status_after
Expand All @@ -1020,6 +1030,7 @@ def test_version_print_next_tag_fails_on_detached_head(
get_commit_def_fn: GetCommitDefFn,
mocked_git_push: MagicMock,
post_mocker: Mocker,
strip_logging_messages: StripLoggingMessagesFn,
):
repo = repo_result["repo"]
expected_error_msg = (
Expand Down Expand Up @@ -1053,7 +1064,7 @@ def test_version_print_next_tag_fails_on_detached_head(
# Evaluate (expected -> actual)
assert_exit_code(1, result, cli_cmd)
assert not result.stdout
assert f"{expected_error_msg}\n" == result.stderr
assert f"{expected_error_msg}\n" == strip_logging_messages(result.stderr)

# assert nothing else happened (no code changes, no commit, no tag, no push, no vcs release)
assert repo_status_before == repo_status_after
Expand Down
7 changes: 5 additions & 2 deletions tests/e2e/cmd_version/test_version_strict.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from requests_mock import Mocker

from tests.conftest import RunCliFn
from tests.e2e.conftest import StripLoggingMessagesFn
from tests.fixtures.git_repo import BuiltRepoResult, GetVersionsFromRepoBuildDefFn


Expand All @@ -30,6 +31,7 @@ def test_version_already_released_when_strict(
run_cli: RunCliFn,
mocked_git_push: MagicMock,
post_mocker: Mocker,
strip_logging_messages: StripLoggingMessagesFn,
):
"""
Given repo has no new changes since the last release,
Expand Down Expand Up @@ -60,7 +62,7 @@ def test_version_already_released_when_strict(
# Evaluate
assert_exit_code(2, result, cli_cmd)
assert f"{latest_release_version}\n" == result.stdout
assert f"{expected_error_msg}\n" == result.stderr
assert f"{expected_error_msg}\n" == strip_logging_messages(result.stderr)

# assert nothing else happened (no code changes, no commit, no tag, no push, no vcs release)
assert repo_status_before == repo_status_after
Expand All @@ -78,6 +80,7 @@ def test_version_on_nonrelease_branch_when_strict(
run_cli: RunCliFn,
mocked_git_push: MagicMock,
post_mocker: Mocker,
strip_logging_messages: StripLoggingMessagesFn,
):
"""
Given repo is on a non-release branch,
Expand All @@ -103,7 +106,7 @@ def test_version_on_nonrelease_branch_when_strict(
# Evaluate
assert_exit_code(2, result, cli_cmd)
assert not result.stdout
assert expected_error_msg == result.stderr
assert expected_error_msg == strip_logging_messages(result.stderr)

# assert nothing else happened (no code changes, no commit, no tag, no push, no vcs release)
tags_after = sorted([tag.name for tag in repo.tags])
Expand Down
21 changes: 20 additions & 1 deletion tests/e2e/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import os
from pathlib import Path
from re import IGNORECASE, compile as regexp
from re import IGNORECASE, MULTILINE, compile as regexp
from typing import TYPE_CHECKING
from unittest.mock import MagicMock

Expand Down Expand Up @@ -47,6 +47,9 @@ class RetrieveRuntimeContextFn(Protocol):

def __call__(self, repo: Repo) -> RuntimeContext: ...

class StripLoggingMessagesFn(Protocol):
def __call__(self, log: str) -> str: ...


@pytest.hookimpl(tryfirst=True)
def pytest_collection_modifyitems(items: list[pytest.Item]) -> None:
Expand Down Expand Up @@ -116,6 +119,22 @@ def _retrieve_runtime_context(repo: Repo) -> RuntimeContext:
return _retrieve_runtime_context


@pytest.fixture(scope="session")
def strip_logging_messages() -> StripLoggingMessagesFn:
"""Fixture to strip logging messages from the output."""
# Log levels match SemanticReleaseLogLevel enum values
logger_msg_pattern = regexp(
r"^\s*(?:\[\d\d:\d\d:\d\d\])?\s*(FATAL|CRITICAL|ERROR|WARNING|INFO|DEBUG|SILLY).*?\n(?:\s+\S.*?\n)*(?!\n[ ]{11})",
MULTILINE,
)

def _strip_logging_messages(log: str) -> str:
# Make sure it ends with a newline
return logger_msg_pattern.sub("", log.rstrip("\n") + "\n")

return _strip_logging_messages


@pytest.fixture(scope="session")
def long_hash_pattern() -> Pattern:
return regexp(r"\b([0-9a-f]{40})\b", IGNORECASE)
Expand Down
17 changes: 13 additions & 4 deletions tests/e2e/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import git
import pytest
from click.testing import CliRunner
from pytest_lazy_fixtures.lazy_fixture import lf as lazy_fixture

from semantic_release import __version__
Expand All @@ -20,6 +21,7 @@
from pathlib import Path

from tests.conftest import RunCliFn
from tests.e2e.conftest import StripLoggingMessagesFn
from tests.fixtures.example_project import ExProjectDir, UpdatePyprojectTomlFn
from tests.fixtures.git_repo import BuiltRepoResult

Expand Down Expand Up @@ -59,8 +61,13 @@ def test_main_prints_version_and_exits(run_cli: RunCliFn):
assert result.output == f"semantic-release, version {__version__}\n"


def test_main_no_args_prints_help_text(run_cli: RunCliFn):
assert_successful_exit_code(run_cli(), [MAIN_PROG_NAME])
def test_main_no_args_passes_w_help_text():
from semantic_release.cli.commands.main import main

cli_cmd = [MAIN_PROG_NAME]
result = CliRunner().invoke(main, prog_name=cli_cmd[0])
assert_successful_exit_code(result, cli_cmd)
assert "Usage: " in result.output


@pytest.mark.parametrize(
Expand Down Expand Up @@ -210,7 +217,9 @@ def test_errors_when_config_file_does_not_exist_and_passed_explicitly(

@pytest.mark.usefixtures(repo_w_no_tags_conventional_commits.__name__)
def test_errors_when_config_file_invalid_configuration(
run_cli: RunCliFn, update_pyproject_toml: UpdatePyprojectTomlFn
run_cli: RunCliFn,
update_pyproject_toml: UpdatePyprojectTomlFn,
strip_logging_messages: StripLoggingMessagesFn,
):
# Setup
update_pyproject_toml("tool.semantic_release.remote.type", "invalidType")
Expand All @@ -220,7 +229,7 @@ def test_errors_when_config_file_invalid_configuration(
result = run_cli(cli_cmd[1:])

# preprocess results
stderr_lines = result.stderr.splitlines()
stderr_lines = strip_logging_messages(result.stderr).splitlines()

# Evaluate
assert_exit_code(1, result, cli_cmd)
Expand Down
Loading