Skip to content

Commit

Permalink
Reach full unit test coverage (#16)
Browse files Browse the repository at this point in the history
* Reach full unit test coverage

* Cover paged output scenario

* Update changelog

* Reformat files

* Bump version and changelog

* Add coverage threshold
  • Loading branch information
daneah committed Dec 10, 2023
1 parent 51e613c commit d1992dc
Show file tree
Hide file tree
Showing 14 changed files with 336 additions and 52 deletions.
5 changes: 4 additions & 1 deletion docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

## [0.0.6] - 2023-12-10

### Added

- `init` command to create a new config file
- `edit` command to edit a config file manually
- `implode` command to remove configuration
- `sniff` command to inspect configuration and issues
- More confirmations for exceptional cases
- A start to unit tests
- Unit tests

### Changed

- Move root options to new `sniff` command
- Move subcommands and utilities to individual modules
- Updated error and confirmation messaging
- Exit with status code 1 in more cases
- Open long repo lists in pager

## [0.0.5] - 2023-12-09
Expand Down
6 changes: 4 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = repo-man
version = 0.0.5
version = 0.0.6
description = Manage repositories of different flavors.
long_description = file: README.md
long_description_content_type = text/markdown
Expand Down Expand Up @@ -73,7 +73,7 @@ source = repo_man
branch = True

[coverage:report]
fail_under = 0.00
fail_under = 90.00
show_missing = True
skip_covered = True

Expand All @@ -87,6 +87,8 @@ envlist = py39,py312
isolated_build = True

[testenv]
package = wheel
wheel_build_env = .pkg
deps =
pytest
pytest-cov
Expand Down
4 changes: 2 additions & 2 deletions src/repo_man/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
@click.group(context_settings={"help_option_names": ["-h", "--help"]})
@click.version_option(package_name="repo-man")
@click.pass_context
def cli(context):
def cli(context): # pragma: no cover
"""Manage repositories of different types"""

config = configparser.ConfigParser()
config.read(REPO_TYPES_CFG)
context.obj = config


def main():
def main(): # pragma: no cover
cli.add_command(add)
cli.add_command(edit)
cli.add_command(flavors)
Expand Down
2 changes: 1 addition & 1 deletion src/repo_man/commands/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ def edit():

if not Path(REPO_TYPES_CFG).exists():
click.echo(click.style(f"No {REPO_TYPES_CFG} file found.", fg="red"))
return
raise SystemExit(1)

click.edit(filename=REPO_TYPES_CFG)
2 changes: 1 addition & 1 deletion src/repo_man/commands/flavors.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def flavors(config: configparser.ConfigParser, repo: str):

if not Path(REPO_TYPES_CFG).exists():
click.echo(click.style(f"No {REPO_TYPES_CFG} file found.", fg="red"))
return
raise SystemExit(1)

found = set()

Expand Down
2 changes: 1 addition & 1 deletion src/repo_man/commands/list_repos.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def list_repos(config: configparser.ConfigParser, repo_type: str):

if not Path(REPO_TYPES_CFG).exists():
click.echo(click.style(f"No {REPO_TYPES_CFG} file found.", fg="red"))
return
raise SystemExit(1)

valid_repo_types = parse_repo_types(config)

Expand Down
21 changes: 21 additions & 0 deletions test/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import configparser

import pytest
from click.testing import CliRunner

from repo_man.consts import REPO_TYPES_CFG


@pytest.fixture
def runner():
return CliRunner()


@pytest.fixture
def get_config():
def func():
config = configparser.ConfigParser()
config.read(REPO_TYPES_CFG)
return config

return func
45 changes: 12 additions & 33 deletions test/test_add.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import configparser
from pathlib import Path

from click.testing import CliRunner

from repo_man.commands.add import add


def test_add_clean_confirm():
runner = CliRunner()

def test_add_clean_confirm(runner, get_config):
with runner.isolated_filesystem():
(Path(".") / "some-repo").mkdir()
config = configparser.ConfigParser()
config.read("repo-man.cfg")
config = get_config()
result = runner.invoke(add, ["some-repo", "-t", "some-type"], input="Y\nY\n", obj=config)
assert result.exit_code == 0
assert (
Expand All @@ -37,13 +31,10 @@ def test_add_clean_confirm():
)


def test_add_clean_no_confirm_new_file():
runner = CliRunner()

def test_add_clean_no_confirm_new_file(runner, get_config):
with runner.isolated_filesystem():
(Path(".") / "some-repo").mkdir()
config = configparser.ConfigParser()
config.read("repo-man.cfg")
config = get_config()
result = runner.invoke(add, ["some-repo", "-t", "some-type"], input="\n", obj=config)
assert result.exit_code == 1
assert (
Expand All @@ -54,9 +45,7 @@ def test_add_clean_no_confirm_new_file():
)


def test_add_with_existing_file():
runner = CliRunner()

def test_add_with_existing_file(runner, get_config):
with runner.isolated_filesystem():
with open("repo-man.cfg", "w") as config_file:
config_file.write(
Expand All @@ -67,8 +56,7 @@ def test_add_with_existing_file():
)

(Path(".") / "some-repo").mkdir()
config = configparser.ConfigParser()
config.read("repo-man.cfg")
config = get_config()
result = runner.invoke(add, ["some-repo", "-t", "some-type"], input="Y\n", obj=config)
assert result.exit_code == 0
assert (
Expand Down Expand Up @@ -96,9 +84,7 @@ def test_add_with_existing_file():
)


def test_add_with_existing_file_and_type():
runner = CliRunner()

def test_add_with_existing_file_and_type(runner, get_config):
with runner.isolated_filesystem():
with open("repo-man.cfg", "w") as config_file:
config_file.write(
Expand All @@ -109,8 +95,7 @@ def test_add_with_existing_file_and_type():
)

(Path(".") / "some-repo").mkdir()
config = configparser.ConfigParser()
config.read("repo-man.cfg")
config = get_config()
result = runner.invoke(add, ["some-repo", "-t", "some-type"], obj=config)
assert result.exit_code == 0
assert result.output == ""
Expand All @@ -127,9 +112,7 @@ def test_add_with_existing_file_and_type():
)


def test_add_multiple_types():
runner = CliRunner()

def test_add_multiple_types(runner, get_config):
with runner.isolated_filesystem():
with open("repo-man.cfg", "w") as config_file:
config_file.write(
Expand All @@ -140,8 +123,7 @@ def test_add_multiple_types():
)

(Path(".") / "some-repo").mkdir()
config = configparser.ConfigParser()
config.read("repo-man.cfg")
config = get_config()
result = runner.invoke(add, ["some-repo", "-t", "some-type", "-t", "some-other-type"], input="Y\n", obj=config)
assert result.exit_code == 0
assert (
Expand Down Expand Up @@ -170,9 +152,7 @@ def test_add_multiple_types():
)


def test_add_no_action_needed():
runner = CliRunner()

def test_add_no_action_needed(runner, get_config):
with runner.isolated_filesystem():
with open("repo-man.cfg", "w") as config_file:
config_file.write(
Expand All @@ -183,8 +163,7 @@ def test_add_no_action_needed():
)

(Path(".") / "some-repo").mkdir()
config = configparser.ConfigParser()
config.read("repo-man.cfg")
config = get_config()
result = runner.invoke(add, ["some-repo", "-t", "some-type"], obj=config)
assert result.exit_code == 0
assert result.output == ""
Expand Down
21 changes: 21 additions & 0 deletions test/test_edit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from pathlib import Path
from unittest.mock import patch

from repo_man.commands import edit


def test_edit_clean(runner):
with runner.isolated_filesystem():
result = runner.invoke(edit.edit)
assert result.exit_code == 1
assert result.output == "No repo-man.cfg file found.\n"


@patch("repo_man.commands.edit.click.edit")
def test_edit_when_config_present(mock_edit, runner):
with runner.isolated_filesystem():
Path("repo-man.cfg").touch()
result = runner.invoke(edit.edit)
assert result.exit_code == 0
assert result.output == ""
mock_edit.assert_called_once_with(filename="repo-man.cfg")
73 changes: 73 additions & 0 deletions test/test_flavors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from pathlib import Path

from repo_man.commands.flavors import flavors


def test_flavors_clean(runner, get_config):
with runner.isolated_filesystem():
Path("some-repo").mkdir()
config = get_config()
result = runner.invoke(flavors, ["some-repo"], obj=config)
assert result.exit_code == 1
assert result.output == "No repo-man.cfg file found.\n"


def test_flavors_when_configured(runner, get_config):
with runner.isolated_filesystem():
Path("some-repo").mkdir()

with open("repo-man.cfg", "w") as config_file:
config_file.write(
"""[foo]
known =
some-repo
[ignore]
known =
some-other-repo
"""
)

config = get_config()
result = runner.invoke(flavors, ["some-repo"], obj=config)
assert result.exit_code == 0
assert result.output == "foo\n"


def test_flavors_when_not_configured(runner, get_config):
with runner.isolated_filesystem():
Path("some-repo").mkdir()

with open("repo-man.cfg", "w") as config_file:
config_file.write(
"""[foo]
known =
some-other-repo
"""
)

config = get_config()
result = runner.invoke(flavors, ["some-repo"], obj=config)
assert result.exit_code == 0
assert result.output == ""


def test_flavors_when_ignored(runner, get_config):
with runner.isolated_filesystem():
Path("some-repo").mkdir()

with open("repo-man.cfg", "w") as config_file:
config_file.write(
"""[ignore]
known =
some-repo
"""
)

config = get_config()
result = runner.invoke(flavors, ["some-repo"], obj=config)
assert result.exit_code == 0
assert result.output == ""
35 changes: 35 additions & 0 deletions test/test_implode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from pathlib import Path

from repo_man.commands.implode import implode


def test_implode_when_config_present_confirm(runner):
with runner.isolated_filesystem():
Path("repo-man.cfg").touch()

result = runner.invoke(implode, ["."], input="Y\n")
assert result.exit_code == 0
assert result.output == "Are you sure you want to do this? [y/N]: Y\n"
assert not Path("repo-man.cfg").exists()


def test_implode_when_config_not_present_confirm(runner):
with runner.isolated_filesystem():
result = runner.invoke(implode, ["."], input="Y\n")
assert result.exit_code == 0
assert result.output == "Are you sure you want to do this? [y/N]: Y\n"


def test_implode_when_config_present_no_confirm(runner):
with runner.isolated_filesystem():
Path("repo-man.cfg").touch()

result = runner.invoke(implode, ["."], input="\n")
assert result.exit_code == 1
assert (
result.output
== """Are you sure you want to do this? [y/N]:
Aborted!
"""
)
assert Path("repo-man.cfg").exists()
Loading

0 comments on commit d1992dc

Please sign in to comment.