Skip to content

Commit da3157d

Browse files
committed
Move util functions from conftest to separated module
1 parent eb283b6 commit da3157d

17 files changed

+380
-314
lines changed

.gitignore

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
!.vscode/extensions.json
2-
!.vscode/launch.json
3-
!.vscode/settings.json
4-
!.vscode/tasks.json
51
*$py.class
62
*.egg
73
*.egg-info/
@@ -11,6 +7,10 @@
117
.idea/
128
.installed.cfg
139
.vscode/*
10+
!.vscode/extensions.json
11+
!.vscode/launch.json
12+
!.vscode/settings.json
13+
!.vscode/tasks.json
1414
MANIFEST
1515
__pycache__/
1616
build/
@@ -20,6 +20,7 @@ docs/_build/
2020
downloads/
2121
eggs/
2222
lib/
23+
!tests/lib/
2324
lib64/
2425
parts/
2526
pip-wheel-metadata/

.pre-commit-config.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,14 @@ repos:
88
- id: check-merge-conflict
99
- id: check-vcs-permalinks
1010
- id: check-yaml
11-
- id: file-contents-sorter
12-
files: ^\.gitignore$
1311
- id: requirements-txt-fixer
1412
files: ^(requirements.*\.txt)$
1513
- id: end-of-file-fixer
1614
- id: fix-byte-order-marker
1715
- id: fix-encoding-pragma
1816
args: [--remove]
1917
- id: name-tests-test
20-
files: ^tests/.*\.py$
18+
files: ^tests\/test_(unit|integration)\/.*\.py$
2119
args: [--django]
2220
- id: trailing-whitespace
2321
- id: detect-private-key

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"restructuredtext.confPath": "${workspaceFolder}/docs"
3+
}

docs/schemas/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.. _versioning-schemas:
2+
13
Versioning schemas
24
------------------
35

setuptools_git_versioning.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
DEFAULT_DEV_TEMPLATE = "{tag}.post{ccount}+git.{sha}"
2020
DEFAULT_DIRTY_TEMPLATE = "{tag}.post{ccount}+git.{sha}.dirty"
2121
DEFAULT_STARTING_VERSION = "0.0.1"
22+
DEFAULT_SORT_BY = "creatordate"
2223
ENV_VARS_REGEXP = re.compile(r"\{env:(?P<name>[^:}]+):?(?P<default>[^}]+\}*)?\}", re.IGNORECASE | re.UNICODE)
2324
TIMESTAMP_REGEXP = re.compile(r"\{timestamp:?(?P<fmt>[^:}]+)?\}", re.IGNORECASE | re.UNICODE)
2425

@@ -32,7 +33,7 @@
3233
"count_commits_from_version_file": False,
3334
"tag_formatter": None,
3435
"branch_formatter": None,
35-
"sort_by": None,
36+
"sort_by": DEFAULT_SORT_BY,
3637
}
3738

3839
log = logging.getLogger(__name__)
@@ -61,14 +62,14 @@ def get_branch(): # type: () -> Optional[str]
6162
return None
6263

6364

64-
def get_all_tags(sort_by="creatordate"): # type: (str) -> List[str]
65+
def get_all_tags(sort_by=DEFAULT_SORT_BY): # type: (str) -> List[str]
6566
tags = _exec("git tag --sort=-{}".format(sort_by))
6667
if tags:
6768
return tags
6869
return []
6970

7071

71-
def get_branch_tags(sort_by="creatordate"): # type: (str) -> List[str]
72+
def get_branch_tags(sort_by=DEFAULT_SORT_BY): # type: (str) -> List[str]
7273
tags = _exec("git tag --sort=-{} --merged".format(sort_by))
7374
if tags:
7475
return tags
@@ -380,7 +381,7 @@ def version_from_git(
380381
count_commits_from_version_file=False, # type: bool
381382
tag_formatter=None, # type: Optional[Callable[[str], str]]
382383
branch_formatter=None, # type: Optional[Callable[[str], str]]
383-
sort_by=None, # type: Optional[str]
384+
sort_by=DEFAULT_SORT_BY, # type: str
384385
):
385386
# type: (...) -> str
386387

@@ -402,7 +403,7 @@ def version_from_git(
402403
return line[8:].strip()
403404

404405
from_file = False
405-
tag = get_tag(sort_by) if sort_by else get_tag()
406+
tag = get_tag(sort_by=sort_by)
406407

407408
if tag is None:
408409
# TODO: raise exception if both version_callback and version_file are set

tests/conftest.py

Lines changed: 1 addition & 222 deletions
Original file line numberDiff line numberDiff line change
@@ -1,234 +1,13 @@
1-
from functools import partial
2-
import logging
31
import os
42
import pytest
53
import shutil
6-
import subprocess
7-
import sys
84
import textwrap
9-
import toml
10-
import uuid
115

12-
from typing import Any, Callable, Dict, Optional
6+
from tests.lib.util import rand_str, create_file, execute
137

14-
log = logging.getLogger(__name__)
158
root = os.path.dirname(os.path.dirname(__file__))
169

1710

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-
23211
@pytest.fixture
23312
def repo_dir(tmpdir):
23413
repo_dir = str(tmpdir.mkdir(rand_str()))

tests/lib/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)