Debian Codegraph @

src/pytest_datadir/plugin.py

raw symbols SCIP
1 import os
2 import shutil
3 import sys
4 from pathlib import Path
5
6 import pytest
7
8
9 def _win32_longpath(path):
10 """
11 Helper function to add the long path prefix for Windows, so that shutil.copytree
12 won't fail while working with paths with 255+ chars.
13 """
14 if sys.platform == "win32":
15 # The use of os.path.normpath here is necessary since "the "\\?\" prefix
16 # to a path string tells the Windows APIs to disable all string parsing
17 # and to send the string that follows it straight to the file system".
18 # (See https://docs.microsoft.com/pt-br/windows/desktop/FileIO/naming-a-file)
19 return "\\\\?\\" + os.path.normpath(path)
20 else:
21 return path
22
23
24 @pytest.fixture
25 def shared_datadir(request, tmp_path):
26 original_shared_path = os.path.join(request.fspath.dirname, "data")
27 temp_path = tmp_path / "data"
28 shutil.copytree(
29 _win32_longpath(original_shared_path), _win32_longpath(str(temp_path))
30 )
31 return temp_path
32
33
34 @pytest.fixture
35 def original_datadir(request):
36 return Path(os.path.splitext(request.module.__file__)[0])
37
38
39 @pytest.fixture
40 def datadir(original_datadir, tmp_path):
41 result = tmp_path / original_datadir.stem
42 if original_datadir.is_dir():
43 shutil.copytree(
44 _win32_longpath(str(original_datadir)), _win32_longpath(str(result))
45 )
46 else:
47 result.mkdir()
48 return result