Skip to content

Commit

Permalink
style(tests): add additional typing to test args
Browse files Browse the repository at this point in the history
  • Loading branch information
codejedi365 committed Feb 13, 2024
1 parent 621c33e commit 3a012a7
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 53 deletions.
15 changes: 9 additions & 6 deletions tests/command_line/test_changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
from tests.fixtures.example_project import ExProjectDir, UseReleaseNotesTemplateFn


changelog_subcmd = changelog.name or changelog.__name__


@pytest.mark.parametrize(
"repo,tag",
[
Expand Down Expand Up @@ -86,7 +89,7 @@ def test_changelog_noop_is_noop(
return_value=session,
), requests_mock.Mocker(session=session) as mocker:
result = cli_runner.invoke(
main, ["--noop", changelog.name or "changelog", *args]
main, ["--noop", changelog_subcmd, *args]
)

assert result.exit_code == 0
Expand Down Expand Up @@ -123,7 +126,7 @@ def test_changelog_content_regenerated(
# Remove the changelog and then check that we can regenerate it
os.remove(str(example_changelog_md.resolve()))

result = cli_runner.invoke(main, [changelog.name or "changelog"])
result = cli_runner.invoke(main, [changelog_subcmd])
assert result.exit_code == 0

# Check that the changelog file was re-created
Expand All @@ -150,7 +153,7 @@ def test_changelog_release_tag_not_in_history(
remove_dir_tree(tempdir.resolve(), force=True)
shutil.copytree(src=str(example_project_dir.resolve()), dst=tempdir)

result = cli_runner.invoke(main, [changelog.name or "changelog", *args])
result = cli_runner.invoke(main, [changelog_subcmd, *args])
assert result.exit_code == 2
assert "not in release history" in result.stderr.lower()

Expand Down Expand Up @@ -189,7 +192,7 @@ def test_changelog_post_to_release(
) as mocker, monkeypatch.context() as m:
m.delenv("GITHUB_REPOSITORY", raising=False)
m.delenv("CI_PROJECT_NAMESPACE", raising=False)
result = cli_runner.invoke(main, [changelog.name or "changelog", *args])
result = cli_runner.invoke(main, [changelog_subcmd, *args])

assert result.exit_code == 0

Expand Down Expand Up @@ -228,15 +231,15 @@ def test_custom_release_notes_template(
release = release_history.released[version]

# Act
resp = cli_runner.invoke(main, [changelog.name or "changelog", "--post-to-release-tag", tag])
resp = cli_runner.invoke(main, [changelog_subcmd, "--post-to-release-tag", tag])
expected_release_notes = runtime_context_with_tags.template_environment.from_string(
EXAMPLE_RELEASE_NOTES_TEMPLATE
).render(version=version, release=release) + '\n'

# Assert
assert resp.exit_code == 0, (
"Unexpected failure in command "
f"'semantic-release {changelog.name} --post-to-release-tag {tag}': "
f"'semantic-release {changelog_subcmd} --post-to-release-tag {tag}': "
+ resp.stderr
)
assert post_mocker.call_count == 1 and post_mocker.last_request is not None
Expand Down
38 changes: 23 additions & 15 deletions tests/command_line/test_main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from __future__ import annotations

import json
import os
from pathlib import Path
from textwrap import dedent
from typing import TYPE_CHECKING

Expand All @@ -10,32 +13,37 @@
from semantic_release.cli import main

if TYPE_CHECKING:
from pathlib import Path

from click.testing import CliRunner
from git import Repo

from tests.fixtures.example_project import UpdatePyprojectTomlFn


def test_main_prints_version_and_exits(cli_runner):
def test_main_prints_version_and_exits(cli_runner: CliRunner):
result = cli_runner.invoke(main, ["--version"])
assert result.exit_code == 0
assert result.output == f"semantic-release, version {__version__}\n"


@pytest.mark.parametrize("args", [[], ["--help"]])
def test_main_prints_help_text(cli_runner, args):
def test_main_prints_help_text(cli_runner: CliRunner, args: list[str]):
result = cli_runner.invoke(main, args)
assert result.exit_code == 0


def test_not_a_release_branch_exit_code(repo_with_git_flow_angular_commits, cli_runner):
def test_not_a_release_branch_exit_code(
repo_with_git_flow_angular_commits: Repo, cli_runner: CliRunner
):
# Run anything that doesn't trigger the help text
repo_with_git_flow_angular_commits.git.checkout("-b", "branch-does-not-exist")
result = cli_runner.invoke(main, ["version", "--no-commit"])
assert result.exit_code == 0


def test_not_a_release_branch_exit_code_with_strict(
repo_with_git_flow_angular_commits, cli_runner
repo_with_git_flow_angular_commits: Repo, cli_runner: CliRunner
):
# Run anything that doesn't trigger the help text
repo_with_git_flow_angular_commits.git.checkout("-b", "branch-does-not-exist")
Expand All @@ -44,7 +52,7 @@ def test_not_a_release_branch_exit_code_with_strict(


def test_not_a_release_branch_detached_head_exit_code(
repo_with_git_flow_angular_commits, cli_runner
repo_with_git_flow_angular_commits: Repo, cli_runner: CliRunner
):
expected_err_msg = (
"Detached HEAD state cannot match any release groups; no release will be made"
Expand All @@ -60,7 +68,7 @@ def test_not_a_release_branch_detached_head_exit_code(


@pytest.fixture
def toml_file_with_no_configuration_for_psr(tmp_path):
def toml_file_with_no_configuration_for_psr(tmp_path: Path) -> Path:
path = tmp_path / "config.toml"
path.write_text(
dedent(
Expand All @@ -76,7 +84,7 @@ def toml_file_with_no_configuration_for_psr(tmp_path):


@pytest.fixture
def json_file_with_no_configuration_for_psr(tmp_path):
def json_file_with_no_configuration_for_psr(tmp_path: Path) -> Path:
path = tmp_path / "config.json"
path.write_text(json.dumps({"foo": [1, 2, 3]}))

Expand All @@ -85,8 +93,8 @@ def json_file_with_no_configuration_for_psr(tmp_path):

@pytest.mark.usefixtures("repo_with_git_flow_angular_commits")
def test_default_config_is_used_when_none_in_toml_config_file(
cli_runner,
toml_file_with_no_configuration_for_psr,
cli_runner: CliRunner,
toml_file_with_no_configuration_for_psr: Path,
):
result = cli_runner.invoke(
main,
Expand All @@ -98,8 +106,8 @@ def test_default_config_is_used_when_none_in_toml_config_file(

@pytest.mark.usefixtures("repo_with_git_flow_angular_commits")
def test_default_config_is_used_when_none_in_json_config_file(
cli_runner,
json_file_with_no_configuration_for_psr,
cli_runner: CliRunner,
json_file_with_no_configuration_for_psr: Path,
):
result = cli_runner.invoke(
main,
Expand All @@ -111,7 +119,7 @@ def test_default_config_is_used_when_none_in_json_config_file(

@pytest.mark.usefixtures("repo_with_git_flow_angular_commits")
def test_errors_when_config_file_does_not_exist_and_passed_explicitly(
cli_runner,
cli_runner: CliRunner,
):
result = cli_runner.invoke(
main,
Expand All @@ -124,7 +132,7 @@ def test_errors_when_config_file_does_not_exist_and_passed_explicitly(

@pytest.mark.usefixtures("repo_with_no_tags_angular_commits")
def test_errors_when_config_file_invalid_configuration(
cli_runner: "CliRunner", update_pyproject_toml: "UpdatePyprojectTomlFn"
cli_runner: CliRunner, update_pyproject_toml: UpdatePyprojectTomlFn
):
update_pyproject_toml("tool.semantic_release.remote.type", "invalidType")
result = cli_runner.invoke(main, ["--config", "pyproject.toml", "version"])
Expand All @@ -136,8 +144,8 @@ def test_errors_when_config_file_invalid_configuration(


def test_uses_default_config_when_no_config_file_found(
tmp_path,
cli_runner,
tmp_path: Path,
cli_runner: CliRunner,
):
# We have to initialise an empty git repository, as the example projects
# all have pyproject.toml configs which would be used by default
Expand Down
52 changes: 32 additions & 20 deletions tests/command_line/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
)


version_subcmd = version.name or version.__name__


@pytest.mark.parametrize(
"repo",
[
Expand All @@ -49,7 +52,12 @@
lazy_fixture("repo_with_git_flow_and_release_channels_angular_commits"),
],
)
def test_version_noop_is_noop(tmp_path_factory, example_project_dir, repo, cli_runner):
def test_version_noop_is_noop(
tmp_path_factory: pytest.TempPathFactory,
example_project_dir: ExProjectDir,
repo: Repo,
cli_runner: CliRunner,
):
# Make a commit to ensure we have something to release
# otherwise the "no release will be made" logic will kick in first
new_file = example_project_dir / "temp.txt"
Expand All @@ -65,7 +73,7 @@ def test_version_noop_is_noop(tmp_path_factory, example_project_dir, repo, cli_r
head_before = repo.head.commit
tags_before = sorted(repo.tags, key=lambda tag: tag.name)

result = cli_runner.invoke(main, ["--noop", version.name])
result = cli_runner.invoke(main, ["--noop", version_subcmd])

tags_after = sorted(repo.tags, key=lambda tag: tag.name)
head_after = repo.head.commit
Expand Down Expand Up @@ -216,7 +224,12 @@ def test_version_noop_is_noop(tmp_path_factory, example_project_dir, repo, cli_r
],
)
def test_version_print(
repo, cli_args, expected_stdout, example_project_dir, tmp_path_factory, cli_runner
repo: Repo,
cli_args: list[str],
expected_stdout: str,
example_project_dir: ExProjectDir,
tmp_path_factory: pytest.TempPathFactory,
cli_runner: CliRunner,
):
# Make a commit to ensure we have something to release
# otherwise the "no release will be made" logic will kick in first
Expand All @@ -232,7 +245,7 @@ def test_version_print(
head_before = repo.head.commit
tags_before = sorted(repo.tags, key=lambda tag: tag.name)

result = cli_runner.invoke(main, [version.name, *cli_args, "--print"])
result = cli_runner.invoke(main, [version_subcmd, *cli_args, "--print"])

tags_after = sorted(repo.tags, key=lambda tag: tag.name)
head_after = repo.head.commit
Expand All @@ -258,11 +271,11 @@ def test_version_print(
lazy_fixture("repo_with_git_flow_and_release_channels_angular_commits"),
],
)
def test_version_already_released_no_push(repo, cli_runner):
def test_version_already_released_no_push(repo: Repo, cli_runner: CliRunner):
# In these tests, unless arguments are supplied then the latest version
# has already been released, so we expect an exit code of 2 with the message
# to indicate that no release will be made
result = cli_runner.invoke(main, ["--strict", version.name, "--no-push"])
result = cli_runner.invoke(main, ["--strict", version_subcmd, "--no-push"])
assert result.exit_code == 2
assert "no release will be made" in result.stderr.lower()

Expand Down Expand Up @@ -410,7 +423,7 @@ def test_version_no_push_force_level(
tags_before = sorted(repo.tags, key=lambda tag: tag.name)

result = cli_runner.invoke(
main, [version.name or "version", *cli_args, "--no-push"]
main, [version_subcmd or "version", *cli_args, "--no-push"]
)

tags_after = sorted(repo.tags, key=lambda tag: tag.name)
Expand Down Expand Up @@ -481,17 +494,16 @@ def test_version_no_push_force_level(
],
)
def test_version_build_metadata_triggers_new_version(repo: Repo, cli_runner: CliRunner):
version_cmd_name = version.name or "version"
# Verify we get "no version to release" without build metadata
no_metadata_result = cli_runner.invoke(
main, ["--strict", version_cmd_name, "--no-push"]
main, ["--strict", version_subcmd, "--no-push"]
)
assert no_metadata_result.exit_code == 2
assert "no release will be made" in no_metadata_result.stderr.lower()

metadata_suffix = "build.abc-12345"
result = cli_runner.invoke(
main, [version_cmd_name, "--no-push", "--build-metadata", metadata_suffix]
main, [version_subcmd, "--no-push", "--build-metadata", metadata_suffix]
)
assert result.exit_code == 0
assert repo.git.tag(l=f"*{metadata_suffix}")
Expand All @@ -500,7 +512,7 @@ def test_version_build_metadata_triggers_new_version(repo: Repo, cli_runner: Cli
def test_version_prints_current_version_if_no_new_version(
repo_with_git_flow_angular_commits: Repo, cli_runner: CliRunner
):
result = cli_runner.invoke(main, [version.name or "version", "--no-push"])
result = cli_runner.invoke(main, [version_subcmd or "version", "--no-push"])
assert result.exit_code == 0
assert "no release will be made" in result.stderr.lower()
assert result.stdout == "1.2.0-alpha.2\n"
Expand All @@ -527,7 +539,7 @@ def test_version_runs_build_command(
):
# ACT: run & force a new version that will trigger the build command
result = cli_runner.invoke(
main, [version.name or "version", "--patch", "--no-push"]
main, [version_subcmd or "version", "--patch", "--no-push"]
)
assert result.exit_code == 0

Expand All @@ -543,7 +555,7 @@ def test_version_skips_build_command_with_skip_build(
"subprocess.run", return_value=CompletedProcess(args=(), returncode=0)
) as patched_subprocess_run:
result = cli_runner.invoke(
main, [version.name, "--patch", "--no-push", "--skip-build"]
main, [version_subcmd, "--patch", "--no-push", "--skip-build"]
) # force a new version
assert result.exit_code == 0

Expand All @@ -555,7 +567,7 @@ def test_version_writes_github_actions_output(
):
mock_output_file = tmp_path / "action.out"
monkeypatch.setenv("GITHUB_OUTPUT", str(mock_output_file.resolve()))
result = cli_runner.invoke(main, [version.name, "--patch", "--no-push"])
result = cli_runner.invoke(main, [version_subcmd, "--patch", "--no-push"])
assert result.exit_code == 0

action_outputs = actions_output_to_dict(
Expand All @@ -570,15 +582,15 @@ def test_version_writes_github_actions_output(


def test_version_exit_code_when_strict(repo_with_git_flow_angular_commits, cli_runner):
result = cli_runner.invoke(main, ["--strict", version.name, "--no-push"])
result = cli_runner.invoke(main, ["--strict", version_subcmd, "--no-push"])
assert result.exit_code != 0


def test_version_exit_code_when_not_strict(
repo_with_git_flow_angular_commits, cli_runner
):
# Testing "no release will be made"
result = cli_runner.invoke(main, [version.name, "--no-push"])
result = cli_runner.invoke(main, [version_subcmd, "--no-push"])
assert result.exit_code == 0


Expand All @@ -599,7 +611,7 @@ def test_custom_release_notes_template(

# Act
resp = cli_runner.invoke(
main, [version.name or "version", "--skip-build", "--vcs-release"]
main, [version_subcmd, "--skip-build", "--vcs-release"]
)
release_history = get_release_history_from_context(runtime_context_with_no_tags)
tag = runtime_context_with_no_tags.repo.tags[-1].name
Expand All @@ -620,7 +632,7 @@ def test_custom_release_notes_template(
assert mocked_git_push.call_count == 2 # 1 for commit, 1 for tag
assert resp.exit_code == 0, (
"Unexpected failure in command "
f"'semantic-release {version.name} --skip-build --vcs-release': " + resp.stderr
f"'semantic-release {version_subcmd} --skip-build --vcs-release': " + resp.stderr
)
assert post_mocker.call_count == 1
assert post_mocker.last_request is not None
Expand All @@ -640,7 +652,7 @@ def test_version_tag_only_push(
head_before = runtime_context_with_no_tags.repo.head.commit

# Act
args = [version.name, "--tag", "--no-commit", "--skip-build", "--no-vcs-release"]
args = [version_subcmd, "--tag", "--no-commit", "--skip-build", "--no-vcs-release"]
resp = cli_runner.invoke(main, args)

tag_after = runtime_context_with_no_tags.repo.tags[-1].name
Expand Down Expand Up @@ -682,7 +694,7 @@ def test_version_only_update_files_no_git_actions(
tags_before = runtime_context_with_tags.repo.tags

# Act
args = [version.name, "--minor", "--no-tag", "--no-commit", "--skip-build"]
args = [version_subcmd, "--minor", "--no-tag", "--no-commit", "--skip-build"]
resp = cli_runner.invoke(main, args)

tags_after = runtime_context_with_tags.repo.tags
Expand Down

0 comments on commit 3a012a7

Please sign in to comment.