File: conftest.py

package info (click to toggle)
hatch-vcs 0.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 200 kB
  • sloc: python: 489; sh: 6; makefile: 3
file content (208 lines) | stat: -rw-r--r-- 4,770 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# SPDX-FileCopyrightText: 2022-present Ofek Lev <oss@ofek.dev>
#
# SPDX-License-Identifier: MIT
import errno
import os
import stat
import tempfile
from contextlib import contextmanager
from functools import wraps
from sys import version_info

if version_info[:2] >= (3, 12):
    from shutil import rmtree
else:
    from shutil import rmtree as _rmtree

    # Backport the onexc keyword argument from Python 3.12
    @wraps(_rmtree)
    def rmtree(path, ignore_errors=False, onerror=None, *args, **kwds):
        if 'onexc' in kwds:
            kwds = dict(kwds)
            onexc = kwds.pop('onexc')

            def onerror(func, path, exc):
                return onexc(func, path, exc[1])

        return _rmtree(path, ignore_errors, onerror, *args, **kwds)


import pytest

from .utils import create_file, git, write_file


def handle_remove_readonly(func, path, exc):  # no cov
    # PermissionError: [WinError 5] Access is denied: '...\\.git\\...'
    if func in (os.rmdir, os.remove, os.unlink) and exc.errno == errno.EACCES:
        os.chmod(path, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)  # noqa: S103
        func(path)
    else:
        raise


@pytest.fixture
def temp_dir():
    directory = tempfile.mkdtemp()
    try:
        directory = os.path.realpath(directory)
        yield directory
    finally:
        rmtree(directory, ignore_errors=False, onexc=handle_remove_readonly)


@contextmanager
def create_project(directory, metadata, *, setup_vcs=True, nested=False):
    root_dir = project_dir = os.path.join(directory, 'my-app')
    os.mkdir(root_dir)

    gitignore_file = os.path.join(root_dir, '.gitignore')
    write_file(gitignore_file, '/my_app/version.py')

    if nested:
        project_dir = os.path.join(root_dir, 'project')
        os.mkdir(project_dir)

    project_file = os.path.join(project_dir, 'pyproject.toml')
    write_file(project_file, metadata)

    package_dir = os.path.join(project_dir, 'my_app')
    os.mkdir(package_dir)

    create_file(os.path.join(package_dir, '__init__.py'))
    create_file(os.path.join(package_dir, 'foo.py'))
    create_file(os.path.join(package_dir, 'bar.py'))
    create_file(os.path.join(package_dir, 'baz.py'))

    origin = os.getcwd()
    os.chdir(project_dir)
    try:
        if setup_vcs:
            if nested:
                os.chdir(root_dir)

            git('init')
            git('config', '--local', 'user.name', 'foo')
            git('config', '--local', 'user.email', 'foo@bar.baz')
            git('add', '.')
            git('commit', '-m', 'test')
            # TODO: Confirm that creating a tag without a message locally causes tests to hang
            git('tag', '1.2.3', '-m', 'test')

            if nested:
                os.chdir(project_dir)

        yield project_dir
    finally:
        os.chdir(origin)


@pytest.fixture
def new_project_basic(temp_dir):
    with create_project(
        temp_dir,
        """\
[build-system]
requires = ["hatchling", "hatch-vcs"]
build-backend = "hatchling.build"

[project]
name = "my-app"
dynamic = ["version"]

[tool.hatch.version]
source = "vcs"
""",
    ) as project:
        yield project


@pytest.fixture
def new_project_write(temp_dir):
    with create_project(
        temp_dir,
        """\
[build-system]
requires = ["hatchling", "hatch-vcs"]
build-backend = "hatchling.build"

[project]
name = "my-app"
dynamic = ["version"]

[tool.hatch.version]
source = "vcs"

[tool.hatch.build.targets.wheel.hooks.vcs]
version-file = "my_app/_version.py"
""",
    ) as project:
        yield project


@pytest.fixture
def new_project_fallback(temp_dir):
    with create_project(
        temp_dir,
        """\
[build-system]
requires = ["hatchling", "hatch-vcs"]
build-backend = "hatchling.build"

[project]
name = "my-app"
dynamic = ["version"]

[tool.hatch.version]
source = "vcs"
fallback-version = "7.8.9"
""",
        setup_vcs=False,
    ) as project:
        yield project


@pytest.fixture
def new_project_root_elsewhere(temp_dir):
    with create_project(
        temp_dir,
        """\
[build-system]
requires = ["hatchling", "hatch-vcs"]
build-backend = "hatchling.build"

[project]
name = "my-app"
dynamic = ["version"]

[tool.hatch.version]
source = "vcs"
raw-options = { root = ".." }
""",
        nested=True,
    ) as project:
        yield project


@pytest.fixture
def new_project_metadata(temp_dir):
    with create_project(
        temp_dir,
        """\
[build-system]
requires = ["hatchling", "hatch-vcs"]
build-backend = "hatchling.build"

[project]
name = "my-app"
dynamic = ["version", "urls"]

[tool.hatch.version]
source = "vcs"

[tool.hatch.metadata.hooks.vcs.urls]
Homepage = "https://www.google.com"
foo = "https://github.com/bar/baz#{commit_hash}"
""",
    ) as project:
        yield project