|
1 | | -from functools import partial |
2 | | -import logging |
3 | 1 | import os |
4 | 2 | import pytest |
5 | 3 | import shutil |
6 | | -import subprocess |
7 | | -import sys |
8 | 4 | import textwrap |
9 | | -import toml |
10 | | -import uuid |
11 | 5 |
|
12 | | -from typing import Any, Callable, Dict, Optional |
| 6 | +from tests.lib.util import rand_str, create_file, execute |
13 | 7 |
|
14 | | -log = logging.getLogger(__name__) |
15 | 8 | root = os.path.dirname(os.path.dirname(__file__)) |
16 | 9 |
|
17 | 10 |
|
18 | | -def rand_str(): # type: () -> str |
19 | | - return str(uuid.uuid4()) |
20 | | - |
21 | | - |
22 | | -def execute(cwd, cmd, **kwargs): # type: (str, str, **Any) -> str |
23 | | - log.info(cwd) |
24 | | - return subprocess.check_output(cmd, cwd=cwd, shell=True, universal_newlines=True, **kwargs) # nosec |
25 | | - |
26 | | - |
27 | | -def create_file( |
28 | | - cwd, # type: str |
29 | | - name=None, # type: Optional[str] |
30 | | - content=None, # type: Optional[str] |
31 | | - add=True, # type: bool |
32 | | - commit=True, # type: bool |
33 | | - **kwargs # type: Any |
34 | | -): # type: (...) -> Optional[str] |
35 | | - result = None |
36 | | - |
37 | | - if not name: |
38 | | - name = rand_str() |
39 | | - if content is None: |
40 | | - content = rand_str() |
41 | | - |
42 | | - log.warning(content) |
43 | | - with open(os.path.join(cwd, name), "w") as f: |
44 | | - f.write(content) |
45 | | - |
46 | | - if add: |
47 | | - execute(cwd, "git add {name}".format(name=name)) |
48 | | - log.info(execute(cwd, "git status")) |
49 | | - log.info(execute(cwd, "git diff")) |
50 | | - |
51 | | - if commit: |
52 | | - execute(cwd, 'git commit -m "Add {name}"'.format(name=name)) |
53 | | - result = get_short_commit(cwd) |
54 | | - |
55 | | - return result |
56 | | - |
57 | | - |
58 | | -def create_pyproject_toml( |
59 | | - cwd, # type: str |
60 | | - config=None, # type: Optional[dict] |
61 | | - commit=True, # type: bool |
62 | | - **kwargs # type: Any |
63 | | -): # type: (...) -> Optional[str] |
64 | | - # well, using pyproject.toml+setup.cfg is more classic |
65 | | - # but it is not easy to check code coverage in such a case |
66 | | - # so we're using pyproject.toml+setup.py |
67 | | - create_file( |
68 | | - cwd, |
69 | | - "setup.py", |
70 | | - textwrap.dedent( |
71 | | - """ |
72 | | - from coverage.control import Coverage |
73 | | -
|
74 | | - coverage = Coverage() |
75 | | - coverage.start() |
76 | | -
|
77 | | - try: |
78 | | - import setuptools |
79 | | -
|
80 | | - setuptools.setup( |
81 | | - name="mypkg", |
82 | | - ) |
83 | | - finally: |
84 | | - coverage.stop() |
85 | | - coverage.save() |
86 | | - """ |
87 | | - ), |
88 | | - commit=False, |
89 | | - **kwargs |
90 | | - ) |
91 | | - |
92 | | - cfg = {} # type: Dict[str, Any] |
93 | | - cfg["build-system"] = { |
94 | | - "requires": [ |
95 | | - "setuptools>=41", |
96 | | - "wheel", |
97 | | - "setuptools-git-versioning", |
98 | | - ], |
99 | | - # with default "setuptools.build_meta" it is not possible to build package |
100 | | - # which uses its own source code to get version number, |
101 | | - # e.g. `version_callback` or `branch_formatter` |
102 | | - # mote details: https://github.com/pypa/setuptools/issues/1642#issuecomment-457673563 |
103 | | - "build-backend": "setuptools.build_meta:__legacy__", |
104 | | - } |
105 | | - |
106 | | - if config is None: |
107 | | - config = {"enabled": True} |
108 | | - |
109 | | - if config != NotImplemented: |
110 | | - cfg["tool"] = {"setuptools-git-versioning": config} |
111 | | - |
112 | | - return create_file(cwd, "pyproject.toml", toml.dumps(cfg), commit=commit, **kwargs) |
113 | | - |
114 | | - |
115 | | -def create_setup_py( |
116 | | - cwd, # type: str |
117 | | - config=None, # type: Optional[dict] |
118 | | - option="setuptools_git_versioning", # # type: str |
119 | | - **kwargs # type: Any |
120 | | -): # type: (...) -> Optional[str] |
121 | | - |
122 | | - if config is None: |
123 | | - config = {"enabled": True} |
124 | | - |
125 | | - if config == NotImplemented: |
126 | | - cfg = "" |
127 | | - else: |
128 | | - cfg = "{option}={config},".format(option=option, config=config) |
129 | | - |
130 | | - return create_file( |
131 | | - cwd, |
132 | | - "setup.py", |
133 | | - textwrap.dedent( |
134 | | - """ |
135 | | - from coverage.control import Coverage |
136 | | -
|
137 | | - coverage = Coverage() |
138 | | - coverage.start() |
139 | | -
|
140 | | - try: |
141 | | - import setuptools |
142 | | -
|
143 | | - setuptools.setup( |
144 | | - name="mypkg", |
145 | | - {cfg} |
146 | | - setup_requires=[ |
147 | | - "setuptools>=41", |
148 | | - "wheel", |
149 | | - "setuptools-git-versioning", |
150 | | - ] |
151 | | - ) |
152 | | - finally: |
153 | | - coverage.stop() |
154 | | - coverage.save() |
155 | | - """ |
156 | | - ).format(cfg=cfg), |
157 | | - **kwargs |
158 | | - ) |
159 | | - |
160 | | - |
161 | | -@pytest.fixture(params=[create_setup_py, create_pyproject_toml]) |
162 | | -def create_config(request): |
163 | | - return request.param |
164 | | - |
165 | | - |
166 | | -def typed_config( |
167 | | - repo, # type: str |
168 | | - config_creator, # type: Callable |
169 | | - config_type, # type: str |
170 | | - template=None, # type: Optional[str] |
171 | | - template_name=None, # type: Optional[str] |
172 | | - config=None, # type: Optional[dict] |
173 | | -): |
174 | | - if config_type == "tag": |
175 | | - cfg = {} |
176 | | - else: |
177 | | - cfg = {"version_file": "VERSION.txt", "count_commits_from_version_file": True} |
178 | | - |
179 | | - if template_name is None: |
180 | | - if config_type == "tag": |
181 | | - template_name = "template" |
182 | | - else: |
183 | | - template_name = "dev_template" |
184 | | - |
185 | | - if template: |
186 | | - cfg[template_name] = template |
187 | | - |
188 | | - if config: |
189 | | - cfg.update(config) |
190 | | - |
191 | | - config_creator(repo, cfg) |
192 | | - |
193 | | - if config_type == "tag": |
194 | | - execute(repo, "git tag 1.2.3") |
195 | | - else: |
196 | | - create_file(repo, "VERSION.txt", "1.2.3") |
197 | | - |
198 | | - |
199 | | -@pytest.fixture(params=["tag", "version_file"]) |
200 | | -def template_config(request): |
201 | | - return partial(typed_config, config_type=request.param) |
202 | | - |
203 | | - |
204 | | -def get_version_setup_py(cwd, **kwargs): # type: (str, **Any) -> str |
205 | | - return execute(cwd, "{python} setup.py --version".format(python=sys.executable), **kwargs).strip() |
206 | | - |
207 | | - |
208 | | -def get_version(cwd, isolated=False, **kwargs): # type: (str, bool, **Any) -> str |
209 | | - cmd = "{python} -m build -s".format(python=sys.executable) |
210 | | - if not isolated: |
211 | | - cmd += " --no-isolation" |
212 | | - execute(cwd, cmd, **kwargs) |
213 | | - |
214 | | - with open(os.path.join(cwd, "mypkg.egg-info/PKG-INFO")) as f: |
215 | | - content = f.read().splitlines() |
216 | | - |
217 | | - for line in content: |
218 | | - if line.startswith("Version: "): |
219 | | - return line.replace("Version: ", "").strip() |
220 | | - |
221 | | - raise RuntimeError("Cannot get package version") |
222 | | - |
223 | | - |
224 | | -def get_commit(cwd, **kwargs): # type: (str, **Any) -> str |
225 | | - return execute(cwd, "git rev-list -n 1 HEAD", **kwargs).strip() |
226 | | - |
227 | | - |
228 | | -def get_short_commit(cwd, **kwargs): # type: (str, *Any) -> str |
229 | | - return get_commit(cwd, **kwargs)[:8] |
230 | | - |
231 | | - |
232 | 11 | @pytest.fixture |
233 | 12 | def repo_dir(tmpdir): |
234 | 13 | repo_dir = str(tmpdir.mkdir(rand_str())) |
|
0 commit comments