Skip to content

Commit 6aac69c

Browse files
committed
Add a real test
1 parent 9a71064 commit 6aac69c

File tree

5 files changed

+105
-5
lines changed

5 files changed

+105
-5
lines changed

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ include = squatter/*
2828
omit = squatter/tests/*
2929

3030
[coverage:report]
31-
fail_under = 70
31+
fail_under = 100
3232
precision = 1
3333
show_missing = True
3434
skip_covered = True

squatter/__main__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88

99
@click.group()
10-
def cli():
10+
def cli() -> None: # pragma: no cover
1111
pass
1212

1313

@@ -35,5 +35,5 @@ def generate(
3535
print("Rerun with --upload to upload")
3636

3737

38-
if __name__ == "__main__":
38+
if __name__ == "__main__": # pragma: no cover
3939
cli()

squatter/templates.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ def generate(
3939
data = SETUP_PY_TMPL.format(**locals())
4040
(Path(self.staging_directory) / "setup.py").write_text(data)
4141

42-
def upload(self) -> None:
42+
def sdist(self) -> None:
4343
check_call([sys.executable, "setup.py", "sdist"], cwd=self.staging_directory)
44-
check_call(["twine", "upload"] + glob.glob(f"{self.staging_directory}/dist/*.tar.gz"), cwd=self.staging_directory)
44+
45+
def upload(self) -> None:
46+
self.sdist()
47+
check_call(
48+
["twine", "upload"] + glob.glob(f"{self.staging_directory}/dist/*.tar.gz"),
49+
cwd=self.staging_directory,
50+
)

squatter/tests/__init__.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import unittest
2+
from pathlib import Path
3+
from subprocess import check_call
4+
from tempfile import TemporaryDirectory
5+
from typing import Any, List
6+
from unittest.mock import patch
7+
8+
from click.testing import CliRunner
9+
10+
from squatter.__main__ import generate
11+
from squatter.templates import Env
12+
13+
14+
class SquatterEnvTest(unittest.TestCase):
15+
def test_env_smoke(self) -> None:
16+
with TemporaryDirectory() as d:
17+
env = Env(d)
18+
env.generate("foo", "Author Name", "[email protected]")
19+
env.sdist()
20+
21+
self.assertEqual(
22+
[Path(d) / "dist" / "foo-0.0.0a1.tar.gz"],
23+
list(Path(d).rglob("*.tar.gz")),
24+
)
25+
26+
egg_info = list(Path(d).rglob("PKG-INFO"))
27+
self.assertEqual(1, len(egg_info))
28+
egg_info_text = egg_info[0].read_text()
29+
self.assertIn("\nName: foo\n", egg_info_text)
30+
self.assertIn("\nAuthor: Author Name\n", egg_info_text)
31+
self.assertIn("\nAuthor-email: [email protected]\n", egg_info_text)
32+
33+
@patch("squatter.templates.check_output")
34+
def test_env_git_prompts(self, check_output_mock: Any) -> None:
35+
tbl = {
36+
("git", "config", "user.name"): "Bob",
37+
("git", "config", "user.email"): "[email protected]",
38+
}
39+
check_output_mock.side_effect = lambda cmd, **kwargs: tbl[tuple(cmd)] # type: ignore
40+
41+
with TemporaryDirectory() as d:
42+
env = Env(d)
43+
env.generate("foo")
44+
env.sdist()
45+
46+
self.assertEqual(
47+
[Path(d) / "dist" / "foo-0.0.0a1.tar.gz"],
48+
list(Path(d).rglob("*.tar.gz")),
49+
)
50+
51+
egg_info = list(Path(d).rglob("PKG-INFO"))
52+
self.assertEqual(1, len(egg_info))
53+
egg_info_text = egg_info[0].read_text()
54+
self.assertIn("\nName: foo\n", egg_info_text)
55+
self.assertIn("\nAuthor: Bob\n", egg_info_text)
56+
self.assertIn("\nAuthor-email: [email protected]\n", egg_info_text)
57+
58+
@patch("squatter.templates.check_output")
59+
@patch("squatter.templates.check_call")
60+
def test_cli_functional(self, check_call_mock: Any, check_output_mock: Any) -> None:
61+
tbl = {
62+
("git", "config", "user.name"): "Bob",
63+
("git", "config", "user.email"): "[email protected]",
64+
}
65+
check_output_mock.side_effect = lambda cmd, **kwargs: tbl[tuple(cmd)] # type: ignore
66+
67+
uploads = 0
68+
69+
def patched_check_call(cmd: List[str], **kwargs: Any) -> Any:
70+
nonlocal uploads
71+
if cmd[0] != "twine":
72+
return check_call(cmd, **kwargs)
73+
else:
74+
assert cmd[-1].endswith(".tar.gz")
75+
uploads += 1
76+
77+
check_call_mock.side_effect = patched_check_call
78+
79+
runner = CliRunner()
80+
result = runner.invoke(generate, ["foo"])
81+
self.assertEqual("Rerun with --upload to upload\n", result.output)
82+
self.assertFalse(result.exception)
83+
self.assertEqual(0, uploads)
84+
85+
runner = CliRunner()
86+
result = runner.invoke(generate, ["--upload", "foo"])
87+
self.assertEqual("", result.output)
88+
self.assertFalse(result.exception)
89+
self.assertEqual(1, uploads)

squatter/tests/__main__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import unittest
2+
3+
from squatter.tests import * # noqa: F401, F403
4+
5+
unittest.main()

0 commit comments

Comments
 (0)