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
|
# SPDX-FileCopyrightText: 2022-present Angus Hollands <goosey15@gmail.com>
# SPDX-FileCopyrightText: 2022-present Ofek Lev <oss@ofek.dev>
#
# SPDX-License-Identifier: MIT
import errno
import os
import shutil
import stat
import tempfile
from contextlib import contextmanager
import pathlib
import pytest
def touch_file(path):
with open(path, "a"):
os.utime(path, None)
def create_file(path, contents):
with open(path, "w") as f:
f.write(contents)
@contextmanager
def create_project(directory):
project_dir = directory / "my-app"
os.mkdir(project_dir)
package_dir = project_dir / "my_app"
os.mkdir(package_dir)
touch_file(package_dir / "__init__.py")
touch_file(package_dir / "foo.py")
touch_file(package_dir / "bar.py")
touch_file(package_dir / "baz.py")
origin = os.getcwd()
os.chdir(project_dir)
try:
yield project_dir
finally:
os.chdir(origin)
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[1].errno == errno.EACCES:
os.chmod(path, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
func(path)
else:
raise
@pytest.fixture
def temp_dir():
directory = tempfile.mkdtemp()
try:
directory = pathlib.Path(os.path.realpath(directory))
yield directory
finally:
shutil.rmtree(directory, ignore_errors=False, onerror=handle_remove_readonly)
@pytest.fixture
def project(temp_dir):
with create_project(temp_dir) as project:
yield project
|