|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | +import sys |
| 5 | +import tomli |
| 6 | +import pytest |
| 7 | +from partis.pyproj.cli.init_pyproj import _init_pyproj |
| 8 | +from partis.pyproj.cli import __main__ as cli |
| 9 | + |
| 10 | +#============================================================================== |
| 11 | + |
| 12 | +def _stub_metadata(_pkg: str): |
| 13 | + return {"version": "1.0.0"} |
| 14 | + |
| 15 | +#============================================================================== |
| 16 | + |
| 17 | +def test_init_pyproj_creates_files(tmp_path, monkeypatch): |
| 18 | + project_dir = tmp_path / "sample" |
| 19 | + project_dir.mkdir() |
| 20 | + pkg_dir = project_dir / "sample" |
| 21 | + pkg_dir.mkdir() |
| 22 | + (pkg_dir / "__init__.py").write_text("# pkg init\n") |
| 23 | + (project_dir / "extra.txt").write_text("data") |
| 24 | + |
| 25 | + monkeypatch.setattr("partis.pyproj.cli.init_pyproj.metadata", _stub_metadata) |
| 26 | + monkeypatch.setattr("builtins.input", lambda *args, **kwargs: "y") |
| 27 | + |
| 28 | + _init_pyproj(path=project_dir, project="sample", version="0.1.0", description="example") |
| 29 | + |
| 30 | + pptoml_path = project_dir / "pyproject.toml" |
| 31 | + assert pptoml_path.exists() |
| 32 | + data = tomli.loads(pptoml_path.read_text()) |
| 33 | + assert data["project"]["name"] == "sample" |
| 34 | + assert data["project"]["version"] == "0.1.0" |
| 35 | + assert (project_dir / "README.md").exists() |
| 36 | + assert (project_dir / "LICENSE.txt").exists() |
| 37 | + |
| 38 | +#============================================================================== |
| 39 | + |
| 40 | +def test_init_pyproj_abort(tmp_path, monkeypatch): |
| 41 | + project_dir = tmp_path / "abort" |
| 42 | + monkeypatch.setattr("partis.pyproj.cli.init_pyproj.metadata", _stub_metadata) |
| 43 | + monkeypatch.setattr("builtins.input", lambda *args, **kwargs: "n") |
| 44 | + |
| 45 | + _init_pyproj(path=project_dir, project="abort", version="0.1.0", description="") |
| 46 | + |
| 47 | + assert not (project_dir / "pyproject.toml").exists() |
| 48 | + assert not (project_dir / "README.md").exists() |
| 49 | + assert not (project_dir / "LICENSE.txt").exists() |
| 50 | + |
| 51 | +#============================================================================== |
| 52 | +def test_cli_main_help(tmp_path, monkeypatch): |
| 53 | + def _input(*args, **kwargs): |
| 54 | + assert False |
| 55 | + |
| 56 | + monkeypatch.setattr("builtins.input", _input) |
| 57 | + monkeypatch.setattr(sys, "argv", ["partis-pyproj"]) |
| 58 | + |
| 59 | + cli.main() |
| 60 | + |
| 61 | +#============================================================================== |
| 62 | +def test_cli_main_creates_project(tmp_path, monkeypatch): |
| 63 | + project_dir = tmp_path / "cli_proj" |
| 64 | + monkeypatch.setattr("partis.pyproj.cli.init_pyproj.metadata", _stub_metadata) |
| 65 | + monkeypatch.setattr("builtins.input", lambda *args, **kwargs: "y") |
| 66 | + monkeypatch.setattr(sys, "argv", [ |
| 67 | + "partis-pyproj", "init", "--name", "cli_proj", "--version", "2.0.0", "--desc", "cli", str(project_dir) |
| 68 | + ]) |
| 69 | + |
| 70 | + cli.main() |
| 71 | + |
| 72 | + pptoml_path = project_dir / "pyproject.toml" |
| 73 | + assert pptoml_path.exists() |
| 74 | + data = tomli.loads(pptoml_path.read_text()) |
| 75 | + assert data["project"]["name"] == "cli_proj" |
| 76 | + assert data["project"]["version"] == "2.0.0" |
| 77 | + |
| 78 | +#============================================================================== |
| 79 | + |
| 80 | + |
| 81 | +def test_init_pyproj_defaults(tmp_path, monkeypatch): |
| 82 | + project_dir = tmp_path / "auto" |
| 83 | + monkeypatch.setattr("partis.pyproj.cli.init_pyproj.metadata", _stub_metadata) |
| 84 | + monkeypatch.setattr("builtins.input", lambda *args, **kwargs: "y") |
| 85 | + |
| 86 | + _init_pyproj(path=project_dir, project=None, version="0.1.0", description="") |
| 87 | + |
| 88 | + data = tomli.loads((project_dir / "pyproject.toml").read_text()) |
| 89 | + assert data["project"]["name"] == "auto" |
| 90 | + assert data["project"]["description"] == "Package for auto" |
| 91 | + |
| 92 | + |
| 93 | +def test_init_pyproj_description_default_with_name(tmp_path, monkeypatch): |
| 94 | + project_dir = tmp_path / "desc_only" |
| 95 | + project_dir.mkdir() |
| 96 | + monkeypatch.setattr("partis.pyproj.cli.init_pyproj.metadata", _stub_metadata) |
| 97 | + monkeypatch.setattr("builtins.input", lambda *args, **kwargs: "y") |
| 98 | + |
| 99 | + _init_pyproj(path=project_dir, project="pkg", version="0.1.0", description="") |
| 100 | + |
| 101 | + data = tomli.loads((project_dir / "pyproject.toml").read_text()) |
| 102 | + assert data["project"]["name"] == "pkg" |
| 103 | + assert data["project"]["description"] == "Package for pkg" |
| 104 | + |
| 105 | + |
| 106 | +def test_init_pyproj_path_not_directory(tmp_path, monkeypatch): |
| 107 | + project_file = tmp_path / "notdir" |
| 108 | + project_file.write_text("data") |
| 109 | + monkeypatch.setattr("partis.pyproj.cli.init_pyproj.metadata", _stub_metadata) |
| 110 | + |
| 111 | + with pytest.raises(FileExistsError): |
| 112 | + _init_pyproj(path=project_file, project="x", version="0.1.0", description="x") |
| 113 | + |
| 114 | + |
| 115 | +def test_init_pyproj_existing_pyproject(tmp_path, monkeypatch): |
| 116 | + project_dir = tmp_path / "exists" |
| 117 | + project_dir.mkdir() |
| 118 | + (project_dir / "pyproject.toml").write_text("[project]\nname='exists'\n") |
| 119 | + monkeypatch.setattr("partis.pyproj.cli.init_pyproj.metadata", _stub_metadata) |
| 120 | + |
| 121 | + with pytest.raises(FileExistsError): |
| 122 | + _init_pyproj(path=project_dir, project="exists", version="0.1.0", description="") |
| 123 | + |
| 124 | + |
| 125 | +def test_init_pyproj_respects_gitignore(tmp_path, monkeypatch): |
| 126 | + project_dir = tmp_path / "gitignore" |
| 127 | + project_dir.mkdir() |
| 128 | + pkg_dir = project_dir / "gitignore" |
| 129 | + pkg_dir.mkdir() |
| 130 | + (pkg_dir / "__init__.py").write_text("# pkg init\n") |
| 131 | + (project_dir / ".gitignore").write_text("skip.txt\n") |
| 132 | + (project_dir / "skip.txt").write_text("data") |
| 133 | + (project_dir / "keep.txt").write_text("data") |
| 134 | + |
| 135 | + monkeypatch.setattr("partis.pyproj.cli.init_pyproj.metadata", _stub_metadata) |
| 136 | + monkeypatch.setattr("builtins.input", lambda *args, **kwargs: "y") |
| 137 | + |
| 138 | + _init_pyproj(path=project_dir, project="gitignore", version="0.1.0", description="gi") |
| 139 | + |
| 140 | + data = tomli.loads((project_dir / "pyproject.toml").read_text()) |
| 141 | + sources = data["tool"]["pyproj"]["dist"]["source"]["copy"] |
| 142 | + assert "keep.txt" in sources |
| 143 | + assert "skip.txt" not in sources |
| 144 | + |
| 145 | + |
| 146 | +def test_init_pyproj_preserves_existing_readme_and_license(tmp_path, monkeypatch): |
| 147 | + project_dir = tmp_path / "preserve" |
| 148 | + project_dir.mkdir() |
| 149 | + (project_dir / "README.md").write_text("existing readme") |
| 150 | + (project_dir / "LICENSE.txt").write_text("existing license") |
| 151 | + monkeypatch.setattr("partis.pyproj.cli.init_pyproj.metadata", _stub_metadata) |
| 152 | + monkeypatch.setattr("builtins.input", lambda *args, **kwargs: "y") |
| 153 | + |
| 154 | + _init_pyproj(path=project_dir, project="preserve", version="0.1.0", description="desc") |
| 155 | + |
| 156 | + assert (project_dir / "README.md").read_text() == "existing readme" |
| 157 | + assert (project_dir / "LICENSE.txt").read_text() == "existing license" |
| 158 | + |
| 159 | + |
0 commit comments