Skip to content

Commit

Permalink
Merge pull request #453 from akaihola/py37-win-tempdir-workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
akaihola authored Jan 7, 2023
2 parents 9db9eda + d0b14be commit dbc99c3
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ Added
- Black's ``target-version =`` configuration file option and ``-t`` /
``--target-version`` command line option


Fixed
-----
- Compatibility of highlighting unit tests with Pygments 2.14.0.
- In the CI test workflow, don't use environment variables to add a Black version
constraint to the ``pip`` command. This fixes the Windows builds.
- Pass Git errors to stderr correctly both in raw and encoded subprocess output mode.
- Add a work-around for cleaning up temporary directories. Needed for Python 3.7 on
Windows.


1.6.1_ - 2022-12-28
Expand Down
6 changes: 5 additions & 1 deletion src/darker/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from black import find_project_root as black_find_project_root

from darker.git import _git_check_output_lines, git_get_version
from darker.utils import fix_py37_win_tempdir_permissions


class GitRepoFixture:
Expand Down Expand Up @@ -114,7 +115,10 @@ def git_repo(tmp_path, monkeypatch):
# variables for any Git commands run by the fixture, let's explicitly remove
# `GIT_DIR` in case a test should call Git directly:
monkeypatch.delenv("GIT_DIR", raising=False)
return repository

yield repository

fix_py37_win_tempdir_permissions(repository.root)


@pytest.fixture
Expand Down
6 changes: 3 additions & 3 deletions src/darker/tests/test_linting.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"""Unit tests for :mod:`darker.linting`"""

import os
import sys
from pathlib import Path
from textwrap import dedent
from unittest.mock import call, patch
Expand All @@ -13,9 +12,10 @@
from darker import linting
from darker.git import WORKTREE, RevisionRange
from darker.tests.helpers import raises_if_exception
from darker.utils import WINDOWS

SKIP_ON_WINDOWS = [pytest.mark.skip] if sys.platform.startswith("win") else []
SKIP_ON_UNIX = [] if sys.platform.startswith("win") else [pytest.mark.skip]
SKIP_ON_WINDOWS = [pytest.mark.skip] if WINDOWS else []
SKIP_ON_UNIX = [] if WINDOWS else [pytest.mark.skip]


@pytest.mark.kwparametrize(
Expand Down
29 changes: 28 additions & 1 deletion src/darker/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@

import io
import logging
import os
import sys
import tokenize
from datetime import datetime
from itertools import chain
from pathlib import Path
from typing import Collection, Iterable, List, Tuple
from typing import Collection, Iterable, List, Tuple, Union

logger = logging.getLogger(__name__)

TextLines = Tuple[str, ...]


WINDOWS = sys.platform.startswith("win")
GIT_DATEFORMAT = "%Y-%m-%d %H:%M:%S.%f +0000"


Expand Down Expand Up @@ -237,3 +240,27 @@ def glob_any(path: Path, patterns: Collection[str]) -> bool:
"""
return any(path.glob(pattern) for pattern in patterns)


def fix_py37_win_tempdir_permissions(dirpath: Union[str, Path]) -> None:
"""Work around a `tempfile` clean-up issue on Windows with Python 3.7
Call this before exiting a ``with TemporaryDirectory():`` block or in teardown for
a Pytest fixture which creates a temporary directory.
See discussion in https://github.com/akaihola/darker/pull/393
Solution borrowed from https://github.com/python/cpython/pull/10320
:param dirpath: The root path of the temporary directory
"""
if not WINDOWS or sys.version_info >= (3, 8):
return
for root, dirs, files in os.walk(dirpath):
for name in dirs + files:
path = os.path.join(root, name)
try:
os.chflags(path, 0) # type: ignore[attr-defined]
except AttributeError:
pass
os.chmod(path, 0o700)

0 comments on commit dbc99c3

Please sign in to comment.