Skip to content

Commit 6316fc8

Browse files
Add uv badge and badge tests (#460)
* Add uv badge and badge tests * Add test coverage for adding uv badge in `usethis readme`
1 parent 8708d10 commit 6316fc8

File tree

5 files changed

+76
-2
lines changed

5 files changed

+76
-2
lines changed

src/usethis/_core/badge.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ def equivalent_to(self, other: Self) -> bool:
3232
return self.name == other.name
3333

3434

35+
def get_pre_commit_badge() -> Badge:
36+
return Badge(
37+
markdown="[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit)](https://github.com/pre-commit/pre-commit)"
38+
)
39+
40+
3541
def get_pypi_badge() -> Badge:
3642
try:
3743
name = get_name()
@@ -53,9 +59,9 @@ def get_ruff_badge() -> Badge:
5359
)
5460

5561

56-
def get_pre_commit_badge() -> Badge:
62+
def get_uv_badge() -> Badge:
5763
return Badge(
58-
markdown="[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit)](https://github.com/pre-commit/pre-commit)"
64+
markdown="[![uv](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/uv/main/assets/badge/v0.json)](https://github.com/astral-sh/uv)"
5965
)
6066

6167

@@ -68,6 +74,7 @@ def get_usethis_badge() -> Badge:
6874
def get_badge_order() -> list[Badge]:
6975
return [
7076
get_pypi_badge(),
77+
get_uv_badge(),
7178
get_ruff_badge(),
7279
get_pre_commit_badge(),
7380
get_usethis_badge(),

src/usethis/_interface/badge.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
get_pypi_badge,
99
get_ruff_badge,
1010
get_usethis_badge,
11+
get_uv_badge,
1112
remove_badge,
1213
)
1314

@@ -68,3 +69,16 @@ def usethis(
6869
add_badge(get_usethis_badge())
6970
else:
7071
remove_badge(get_usethis_badge())
72+
73+
74+
@app.command(help="Add a badge for the uv package manager.")
75+
def uv(
76+
remove: bool = remove_opt,
77+
offline: bool = offline_opt,
78+
quiet: bool = quiet_opt,
79+
) -> None:
80+
with usethis_config.set(offline=offline, quiet=quiet), files_manager():
81+
if not remove:
82+
add_badge(get_uv_badge())
83+
else:
84+
remove_badge(get_uv_badge())

src/usethis/_interface/readme.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
get_pre_commit_badge,
1010
get_ruff_badge,
1111
get_usethis_badge,
12+
get_uv_badge,
1213
)
1314
from usethis._core.readme import add_readme
15+
from usethis._integrations.uv.used import is_uv_used
1416
from usethis._tool import PreCommitTool, RuffTool
1517

1618

@@ -28,4 +30,7 @@ def readme(
2830
if PreCommitTool().is_used():
2931
add_badge(get_pre_commit_badge())
3032

33+
if is_uv_used():
34+
add_badge(get_uv_badge())
35+
3136
add_badge(get_usethis_badge())

tests/usethis/_interface/test_interface_badge.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,49 @@ def test_remove(self, tmp_path: Path):
7373

7474
# Assert
7575
assert result.exit_code == 0, result.output
76+
77+
78+
class TestUsethis:
79+
def test_add(self, tmp_path: Path):
80+
# Act
81+
runner = CliRunner()
82+
with change_cwd(tmp_path):
83+
result = runner.invoke(app, ["usethis"])
84+
85+
# Assert
86+
assert result.exit_code == 0, result.output
87+
88+
def test_remove(self, tmp_path: Path):
89+
# Arrange
90+
(tmp_path / "README.md").write_text("")
91+
92+
# Act
93+
runner = CliRunner()
94+
with change_cwd(tmp_path):
95+
result = runner.invoke(app, ["usethis", "--remove"])
96+
97+
# Assert
98+
assert result.exit_code == 0, result.output
99+
100+
101+
class TestUV:
102+
def test_add(self, tmp_path: Path):
103+
# Act
104+
runner = CliRunner()
105+
with change_cwd(tmp_path):
106+
result = runner.invoke(app, ["uv"])
107+
108+
# Assert
109+
assert result.exit_code == 0, result.output
110+
111+
def test_remove(self, tmp_path: Path):
112+
# Arrange
113+
(tmp_path / "README.md").write_text("")
114+
115+
# Act
116+
runner = CliRunner()
117+
with change_cwd(tmp_path):
118+
result = runner.invoke(app, ["uv", "--remove"])
119+
120+
# Assert
121+
assert result.exit_code == 0, result.output

tests/usethis/_interface/test_interface_readme.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def test_badges(self, tmp_path: Path):
2323
# Arrange
2424
(tmp_path / "ruff.toml").touch()
2525
(tmp_path / ".pre-commit-config.yaml").touch()
26+
(tmp_path / "uv.lock").write_text("")
2627

2728
# Act
2829
runner = CliRunner()
@@ -35,3 +36,4 @@ def test_badges(self, tmp_path: Path):
3536
# and check the badges get created
3637
assert "ruff" in (tmp_path / "README.md").read_text()
3738
assert "pre-commit" in (tmp_path / "README.md").read_text()
39+
assert "uv" in (tmp_path / "README.md").read_text()

0 commit comments

Comments
 (0)