|
| 1 | +import subprocess |
| 2 | +import sys |
| 3 | +from configparser import ConfigParser |
| 4 | +from os import makedirs |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from python3_pip_skeleton import __main__, __version__ |
| 10 | + |
| 11 | + |
| 12 | +def check_output(*args, cwd=None) -> str: |
| 13 | + try: |
| 14 | + return subprocess.check_output( |
| 15 | + args, stderr=subprocess.STDOUT, text=True, cwd=cwd |
| 16 | + ) |
| 17 | + except subprocess.CalledProcessError as e: |
| 18 | + raise ValueError(e.output) |
| 19 | + |
| 20 | + |
| 21 | +def test_cli_version(): |
| 22 | + output = check_output(sys.executable, "-m", "python3_pip_skeleton", "--version") |
| 23 | + assert output.strip() == __version__ |
| 24 | + |
| 25 | + |
| 26 | +def test_new_module(tmp_path: Path): |
| 27 | + module = tmp_path / "my-module" |
| 28 | + output = check_output( |
| 29 | + sys.executable, |
| 30 | + "-m", |
| 31 | + "python3_pip_skeleton", |
| 32 | + "new", |
| 33 | + "--org=myorg", |
| 34 | + "--package=my_module", |
| 35 | + "--full-name=Firstname Lastname", |
| 36 | + |
| 37 | + str(module), |
| 38 | + ) |
| 39 | + assert output.strip().endswith( |
| 40 | + "Developer instructions in docs/developer/tutorials/dev-install.rst" |
| 41 | + ) |
| 42 | + |
| 43 | + conf = ConfigParser() |
| 44 | + conf.read(module / "setup.cfg") |
| 45 | + assert conf["metadata"]["author"] == "Firstname Lastname" |
| 46 | + assert conf[ "metadata"][ "author_email"] == "[email protected]" |
| 47 | + api_rst = module / "docs" / "user" / "reference" / "api.rst" |
| 48 | + assert ( |
| 49 | + "Version number as calculated by https://github.com/pypa/setuptools_scm" |
| 50 | + in api_rst.read_text() |
| 51 | + ) |
| 52 | + assert (module / "src" / "my_module").is_dir() |
| 53 | + assert check_output("git", "branch", cwd=module).strip() == "* main" |
| 54 | + check_output("python", "-m", "venv", "venv", cwd=module) |
| 55 | + check_output("venv/bin/pip", "install", ".[dev]", cwd=module) |
| 56 | + check_output( |
| 57 | + "venv/bin/python", |
| 58 | + "-m", |
| 59 | + "sphinx", |
| 60 | + "-EWT", |
| 61 | + "--keep-going", |
| 62 | + "docs", |
| 63 | + "build/html", |
| 64 | + cwd=module, |
| 65 | + ) |
| 66 | + with pytest.raises(ValueError) as ctx: |
| 67 | + check_output("venv/bin/python", "-m", "pytest", module / "tests", cwd=module) |
| 68 | + out = ctx.value.args[0] |
| 69 | + print(out) |
| 70 | + assert "4 failed, 1 passed" in out |
| 71 | + assert "Please change description in ./setup.cfg" in out |
| 72 | + assert "Please change ./README.rst" in out |
| 73 | + |
| 74 | + |
| 75 | +def test_new_module_existing_dir(tmp_path: Path): |
| 76 | + module = tmp_path / "my-module" |
| 77 | + makedirs(module / "existing_dir") |
| 78 | + |
| 79 | + with pytest.raises(Exception) as excinfo: |
| 80 | + check_output( |
| 81 | + sys.executable, |
| 82 | + "-m", |
| 83 | + "python3_pip_skeleton", |
| 84 | + "new", |
| 85 | + "--org=myorg", |
| 86 | + "--package=my_module", |
| 87 | + "--full-name=Firstname Lastname", |
| 88 | + |
| 89 | + str(module), |
| 90 | + ) |
| 91 | + assert "to not exist, or be an empty dir" in str(excinfo.value) |
| 92 | + |
| 93 | + |
| 94 | +def test_existing_module(tmp_path: Path): |
| 95 | + module = tmp_path / "scanspec" |
| 96 | + __main__.git( |
| 97 | + "clone", |
| 98 | + "--depth", |
| 99 | + "1", |
| 100 | + "--branch", |
| 101 | + "0.5.3", |
| 102 | + "https://github.com/dls-controls/scanspec", |
| 103 | + str(module), |
| 104 | + ) |
| 105 | + output = check_output( |
| 106 | + sys.executable, |
| 107 | + "-m", |
| 108 | + "python3_pip_skeleton", |
| 109 | + "existing", |
| 110 | + "--org=epics-containers", |
| 111 | + str(module), |
| 112 | + ) |
| 113 | + assert output.endswith( |
| 114 | + """ |
| 115 | +Automatic merge failed; fix conflicts and then commit the result. |
| 116 | +
|
| 117 | +Please fix the conflicts above, then you can run: |
| 118 | + git branch -d skeleton-merge-branch |
| 119 | +Developer instructions in docs/developer/tutorials/dev-install.rst |
| 120 | +""" |
| 121 | + ) |
| 122 | + __main__.git("merge", "--abort", cwd=str(module)) |
| 123 | + MERGE_BRANCH = "skeleton-merge-branch" |
| 124 | + |
| 125 | + with pytest.raises(Exception) as excinfo: |
| 126 | + output = check_output( |
| 127 | + sys.executable, |
| 128 | + "-m", |
| 129 | + "python3_pip_skeleton", |
| 130 | + "existing", |
| 131 | + "--org=epics-containers", |
| 132 | + str(module), |
| 133 | + ) |
| 134 | + assert ( |
| 135 | + f"{MERGE_BRANCH} already exists. \ |
| 136 | + Please run 'python3-pip-skeleton clean' to remove it." |
| 137 | + in str(excinfo.value) |
| 138 | + ) |
| 139 | + |
| 140 | + branches = __main__.list_branches(module) |
| 141 | + assert MERGE_BRANCH in branches |
| 142 | + output = check_output( |
| 143 | + sys.executable, |
| 144 | + "-m", |
| 145 | + "python3_pip_skeleton", |
| 146 | + "clean", |
| 147 | + ".", |
| 148 | + cwd=str(module), |
| 149 | + ) |
| 150 | + assert output.strip("\n") == f"{MERGE_BRANCH} deleted from existing repo" |
| 151 | + branches = __main__.list_branches(module) |
| 152 | + assert MERGE_BRANCH not in branches |
| 153 | + |
| 154 | + |
| 155 | +def test_existing_module_already_adopted(tmp_path: Path): |
| 156 | + module = tmp_path / "scanspec" |
| 157 | + __main__.git( |
| 158 | + "clone", |
| 159 | + "--branch", |
| 160 | + "0.5.4", # dls-python3-skeleton was adopted in this release |
| 161 | + "https://github.com/dls-controls/scanspec", |
| 162 | + str(module), |
| 163 | + ) |
| 164 | + with pytest.raises(Exception) as excinfo: |
| 165 | + check_output( |
| 166 | + sys.executable, |
| 167 | + "-m", |
| 168 | + "python3_pip_skeleton", |
| 169 | + "existing", |
| 170 | + "--org=epics-containers", |
| 171 | + str(module), |
| 172 | + ) |
| 173 | + assert "already adopted skeleton" in str(excinfo.value) |
0 commit comments