Skip to content

Commit d1992dc

Browse files
authored
Reach full unit test coverage (#16)
* Reach full unit test coverage * Cover paged output scenario * Update changelog * Reformat files * Bump version and changelog * Add coverage threshold
1 parent 51e613c commit d1992dc

14 files changed

Lines changed: 336 additions & 52 deletions

docs/CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
## [0.0.6] - 2023-12-10
11+
1012
### Added
1113

1214
- `init` command to create a new config file
1315
- `edit` command to edit a config file manually
1416
- `implode` command to remove configuration
1517
- `sniff` command to inspect configuration and issues
1618
- More confirmations for exceptional cases
17-
- A start to unit tests
19+
- Unit tests
1820

1921
### Changed
2022

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

2629
## [0.0.5] - 2023-12-09

setup.cfg

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = repo-man
3-
version = 0.0.5
3+
version = 0.0.6
44
description = Manage repositories of different flavors.
55
long_description = file: README.md
66
long_description_content_type = text/markdown
@@ -73,7 +73,7 @@ source = repo_man
7373
branch = True
7474

7575
[coverage:report]
76-
fail_under = 0.00
76+
fail_under = 90.00
7777
show_missing = True
7878
skip_covered = True
7979

@@ -87,6 +87,8 @@ envlist = py39,py312
8787
isolated_build = True
8888

8989
[testenv]
90+
package = wheel
91+
wheel_build_env = .pkg
9092
deps =
9193
pytest
9294
pytest-cov

src/repo_man/cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
@click.group(context_settings={"help_option_names": ["-h", "--help"]})
1616
@click.version_option(package_name="repo-man")
1717
@click.pass_context
18-
def cli(context):
18+
def cli(context): # pragma: no cover
1919
"""Manage repositories of different types"""
2020

2121
config = configparser.ConfigParser()
2222
config.read(REPO_TYPES_CFG)
2323
context.obj = config
2424

2525

26-
def main():
26+
def main(): # pragma: no cover
2727
cli.add_command(add)
2828
cli.add_command(edit)
2929
cli.add_command(flavors)

src/repo_man/commands/edit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ def edit():
1111

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

1616
click.edit(filename=REPO_TYPES_CFG)

src/repo_man/commands/flavors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def flavors(config: configparser.ConfigParser, repo: str):
1515

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

2020
found = set()
2121

src/repo_man/commands/list_repos.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def list_repos(config: configparser.ConfigParser, repo_type: str):
1515

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

2020
valid_repo_types = parse_repo_types(config)
2121

test/conftest.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import configparser
2+
3+
import pytest
4+
from click.testing import CliRunner
5+
6+
from repo_man.consts import REPO_TYPES_CFG
7+
8+
9+
@pytest.fixture
10+
def runner():
11+
return CliRunner()
12+
13+
14+
@pytest.fixture
15+
def get_config():
16+
def func():
17+
config = configparser.ConfigParser()
18+
config.read(REPO_TYPES_CFG)
19+
return config
20+
21+
return func

test/test_add.py

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
1-
import configparser
21
from pathlib import Path
32

4-
from click.testing import CliRunner
5-
63
from repo_man.commands.add import add
74

85

9-
def test_add_clean_confirm():
10-
runner = CliRunner()
11-
6+
def test_add_clean_confirm(runner, get_config):
127
with runner.isolated_filesystem():
138
(Path(".") / "some-repo").mkdir()
14-
config = configparser.ConfigParser()
15-
config.read("repo-man.cfg")
9+
config = get_config()
1610
result = runner.invoke(add, ["some-repo", "-t", "some-type"], input="Y\nY\n", obj=config)
1711
assert result.exit_code == 0
1812
assert (
@@ -37,13 +31,10 @@ def test_add_clean_confirm():
3731
)
3832

3933

40-
def test_add_clean_no_confirm_new_file():
41-
runner = CliRunner()
42-
34+
def test_add_clean_no_confirm_new_file(runner, get_config):
4335
with runner.isolated_filesystem():
4436
(Path(".") / "some-repo").mkdir()
45-
config = configparser.ConfigParser()
46-
config.read("repo-man.cfg")
37+
config = get_config()
4738
result = runner.invoke(add, ["some-repo", "-t", "some-type"], input="\n", obj=config)
4839
assert result.exit_code == 1
4940
assert (
@@ -54,9 +45,7 @@ def test_add_clean_no_confirm_new_file():
5445
)
5546

5647

57-
def test_add_with_existing_file():
58-
runner = CliRunner()
59-
48+
def test_add_with_existing_file(runner, get_config):
6049
with runner.isolated_filesystem():
6150
with open("repo-man.cfg", "w") as config_file:
6251
config_file.write(
@@ -67,8 +56,7 @@ def test_add_with_existing_file():
6756
)
6857

6958
(Path(".") / "some-repo").mkdir()
70-
config = configparser.ConfigParser()
71-
config.read("repo-man.cfg")
59+
config = get_config()
7260
result = runner.invoke(add, ["some-repo", "-t", "some-type"], input="Y\n", obj=config)
7361
assert result.exit_code == 0
7462
assert (
@@ -96,9 +84,7 @@ def test_add_with_existing_file():
9684
)
9785

9886

99-
def test_add_with_existing_file_and_type():
100-
runner = CliRunner()
101-
87+
def test_add_with_existing_file_and_type(runner, get_config):
10288
with runner.isolated_filesystem():
10389
with open("repo-man.cfg", "w") as config_file:
10490
config_file.write(
@@ -109,8 +95,7 @@ def test_add_with_existing_file_and_type():
10995
)
11096

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

129114

130-
def test_add_multiple_types():
131-
runner = CliRunner()
132-
115+
def test_add_multiple_types(runner, get_config):
133116
with runner.isolated_filesystem():
134117
with open("repo-man.cfg", "w") as config_file:
135118
config_file.write(
@@ -140,8 +123,7 @@ def test_add_multiple_types():
140123
)
141124

142125
(Path(".") / "some-repo").mkdir()
143-
config = configparser.ConfigParser()
144-
config.read("repo-man.cfg")
126+
config = get_config()
145127
result = runner.invoke(add, ["some-repo", "-t", "some-type", "-t", "some-other-type"], input="Y\n", obj=config)
146128
assert result.exit_code == 0
147129
assert (
@@ -170,9 +152,7 @@ def test_add_multiple_types():
170152
)
171153

172154

173-
def test_add_no_action_needed():
174-
runner = CliRunner()
175-
155+
def test_add_no_action_needed(runner, get_config):
176156
with runner.isolated_filesystem():
177157
with open("repo-man.cfg", "w") as config_file:
178158
config_file.write(
@@ -183,8 +163,7 @@ def test_add_no_action_needed():
183163
)
184164

185165
(Path(".") / "some-repo").mkdir()
186-
config = configparser.ConfigParser()
187-
config.read("repo-man.cfg")
166+
config = get_config()
188167
result = runner.invoke(add, ["some-repo", "-t", "some-type"], obj=config)
189168
assert result.exit_code == 0
190169
assert result.output == ""

test/test_edit.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from pathlib import Path
2+
from unittest.mock import patch
3+
4+
from repo_man.commands import edit
5+
6+
7+
def test_edit_clean(runner):
8+
with runner.isolated_filesystem():
9+
result = runner.invoke(edit.edit)
10+
assert result.exit_code == 1
11+
assert result.output == "No repo-man.cfg file found.\n"
12+
13+
14+
@patch("repo_man.commands.edit.click.edit")
15+
def test_edit_when_config_present(mock_edit, runner):
16+
with runner.isolated_filesystem():
17+
Path("repo-man.cfg").touch()
18+
result = runner.invoke(edit.edit)
19+
assert result.exit_code == 0
20+
assert result.output == ""
21+
mock_edit.assert_called_once_with(filename="repo-man.cfg")

test/test_flavors.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
from pathlib import Path
2+
3+
from repo_man.commands.flavors import flavors
4+
5+
6+
def test_flavors_clean(runner, get_config):
7+
with runner.isolated_filesystem():
8+
Path("some-repo").mkdir()
9+
config = get_config()
10+
result = runner.invoke(flavors, ["some-repo"], obj=config)
11+
assert result.exit_code == 1
12+
assert result.output == "No repo-man.cfg file found.\n"
13+
14+
15+
def test_flavors_when_configured(runner, get_config):
16+
with runner.isolated_filesystem():
17+
Path("some-repo").mkdir()
18+
19+
with open("repo-man.cfg", "w") as config_file:
20+
config_file.write(
21+
"""[foo]
22+
known =
23+
some-repo
24+
25+
[ignore]
26+
known =
27+
some-other-repo
28+
29+
"""
30+
)
31+
32+
config = get_config()
33+
result = runner.invoke(flavors, ["some-repo"], obj=config)
34+
assert result.exit_code == 0
35+
assert result.output == "foo\n"
36+
37+
38+
def test_flavors_when_not_configured(runner, get_config):
39+
with runner.isolated_filesystem():
40+
Path("some-repo").mkdir()
41+
42+
with open("repo-man.cfg", "w") as config_file:
43+
config_file.write(
44+
"""[foo]
45+
known =
46+
some-other-repo
47+
48+
"""
49+
)
50+
51+
config = get_config()
52+
result = runner.invoke(flavors, ["some-repo"], obj=config)
53+
assert result.exit_code == 0
54+
assert result.output == ""
55+
56+
57+
def test_flavors_when_ignored(runner, get_config):
58+
with runner.isolated_filesystem():
59+
Path("some-repo").mkdir()
60+
61+
with open("repo-man.cfg", "w") as config_file:
62+
config_file.write(
63+
"""[ignore]
64+
known =
65+
some-repo
66+
67+
"""
68+
)
69+
70+
config = get_config()
71+
result = runner.invoke(flavors, ["some-repo"], obj=config)
72+
assert result.exit_code == 0
73+
assert result.output == ""

0 commit comments

Comments
 (0)