|
1 | 1 | import subprocess |
2 | 2 | import sys |
3 | | -from configparser import ConfigParser |
4 | | -from os import makedirs |
5 | | -from pathlib import Path |
6 | 3 |
|
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) |
| 4 | +from python3_pip_skeleton import __version__ |
19 | 5 |
|
20 | 6 |
|
21 | 7 | 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 | | - "Instructions on how to develop this module are in CONTRIBUTING.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 | | - versiongit_lines = [ |
48 | | - line |
49 | | - for line in (module / "docs" / "reference" / "api.rst").read_text().splitlines() |
50 | | - if "setuptools" in line |
51 | | - ] |
52 | | - assert ( |
53 | | - " Version number as calculated by https://github.com/pypa/setuptools_scm" |
54 | | - in versiongit_lines |
55 | | - ) |
56 | | - assert (module / "src" / "my_module").is_dir() |
57 | | - assert check_output("git", "branch", cwd=module).strip() == "* main" |
58 | | - check_output("virtualenv", ".venv", cwd=module) |
59 | | - check_output(".venv/bin/pip", "install", ".[dev]", cwd=module) |
60 | | - check_output( |
61 | | - ".venv/bin/python", |
62 | | - "-m", |
63 | | - "sphinx", |
64 | | - "-EWT", |
65 | | - "--keep-going", |
66 | | - "docs", |
67 | | - "build/html", |
68 | | - cwd=module, |
69 | | - ) |
70 | | - with pytest.raises(ValueError) as ctx: |
71 | | - check_output(".venv/bin/python", "-m", "pytest", module / "tests", cwd=module) |
72 | | - out = ctx.value.args[0] |
73 | | - print(out) |
74 | | - assert "4 failed, 1 passed" in out |
75 | | - assert "Please change description in ./setup.cfg" in out |
76 | | - assert "Please change ./README.rst" in out |
77 | | - assert "Please delete ./docs/explanations/why-is-something-so.rst" in out |
78 | | - |
79 | | - |
80 | | -def test_new_module_existing_dir(tmp_path: Path): |
81 | | - module = tmp_path / "my-module" |
82 | | - makedirs(module / "existing_dir") |
83 | | - |
84 | | - with pytest.raises(Exception) as excinfo: |
85 | | - check_output( |
86 | | - sys.executable, |
87 | | - "-m", |
88 | | - "python3_pip_skeleton", |
89 | | - "new", |
90 | | - "--org=myorg", |
91 | | - "--package=my_module", |
92 | | - "--full-name=Firstname Lastname", |
93 | | - |
94 | | - str(module), |
95 | | - ) |
96 | | - assert "to not exist, or be an empty dir" in str(excinfo.value) |
97 | | - |
98 | | - |
99 | | -def test_existing_module(tmp_path: Path): |
100 | | - module = tmp_path / "scanspec" |
101 | | - __main__.git( |
102 | | - "clone", |
103 | | - "--depth", |
104 | | - "1", |
105 | | - "--branch", |
106 | | - "0.5.3", |
107 | | - "https://github.com/dls-controls/scanspec", |
108 | | - str(module), |
109 | | - ) |
110 | | - output = check_output( |
111 | | - sys.executable, |
112 | | - "-m", |
113 | | - "python3_pip_skeleton", |
114 | | - "existing", |
115 | | - "--org=epics-containers", |
116 | | - str(module), |
117 | | - ) |
118 | | - assert output.endswith( |
119 | | - """ |
120 | | -Automatic merge failed; fix conflicts and then commit the result. |
121 | | -
|
122 | | -Please fix the conflicts above, then you can run: |
123 | | - git branch -d skeleton-merge-branch |
124 | | -Instructions on how to develop this module are in CONTRIBUTING.rst |
125 | | -""" |
126 | | - ) |
127 | | - __main__.git("merge", "--abort", cwd=str(module)) |
128 | | - MERGE_BRANCH = "skeleton-merge-branch" |
129 | | - |
130 | | - with pytest.raises(Exception) as excinfo: |
131 | | - output = check_output( |
132 | | - sys.executable, |
133 | | - "-m", |
134 | | - "python3_pip_skeleton", |
135 | | - "existing", |
136 | | - "--org=epics-containers", |
137 | | - str(module), |
138 | | - ) |
139 | | - assert ( |
140 | | - f"{MERGE_BRANCH} already exists. \ |
141 | | - Please run 'python3-pip-skeleton clean' to remove it." |
142 | | - in str(excinfo.value) |
143 | | - ) |
144 | | - |
145 | | - branches = __main__.list_branches(module) |
146 | | - assert MERGE_BRANCH in branches |
147 | | - output = check_output( |
148 | | - sys.executable, |
149 | | - "-m", |
150 | | - "python3_pip_skeleton", |
151 | | - "clean", |
152 | | - ".", |
153 | | - cwd=str(module), |
154 | | - ) |
155 | | - assert output.strip("\n") == f"{MERGE_BRANCH} deleted from existing repo" |
156 | | - branches = __main__.list_branches(module) |
157 | | - assert MERGE_BRANCH not in branches |
158 | | - |
159 | | - |
160 | | -def test_existing_module_already_adopted(tmp_path: Path): |
161 | | - module = tmp_path / "scanspec" |
162 | | - __main__.git( |
163 | | - "clone", |
164 | | - "--branch", |
165 | | - "0.5.4", # dls-python3-skeleton was adopted in this release |
166 | | - "https://github.com/dls-controls/scanspec", |
167 | | - str(module), |
168 | | - ) |
169 | | - with pytest.raises(Exception) as excinfo: |
170 | | - check_output( |
171 | | - sys.executable, |
172 | | - "-m", |
173 | | - "python3_pip_skeleton", |
174 | | - "existing", |
175 | | - "--org=epics-containers", |
176 | | - str(module), |
177 | | - ) |
178 | | - assert "already adopted skeleton" in str(excinfo.value) |
| 8 | + cmd = [sys.executable, "-m", "python3_pip_skeleton", "--version"] |
| 9 | + assert subprocess.check_output(cmd).decode().strip() == __version__ |
0 commit comments