|
| 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) |
0 commit comments