Skip to content

Commit f51c222

Browse files
authored
Merge pull request #2937 from pared/2896_add_windows
temporary_windows_drive fixture: move to dir_helpers
2 parents 39a9fd8 + d40588a commit f51c222

File tree

2 files changed

+49
-41
lines changed

2 files changed

+49
-41
lines changed

tests/conftest.py

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -101,41 +101,6 @@ def ssh(ssh_server):
101101
yield SSHConnection(**ssh_server.test_creds)
102102

103103

104-
@pytest.fixture
105-
def temporary_windows_drive(repo_dir):
106-
import string
107-
import win32api
108-
from ctypes import windll
109-
from win32con import DDD_REMOVE_DEFINITION
110-
111-
drives = [
112-
s[0].upper()
113-
for s in win32api.GetLogicalDriveStrings().split("\000")
114-
if len(s) > 0
115-
]
116-
117-
new_drive_name = [
118-
letter for letter in string.ascii_uppercase if letter not in drives
119-
][0]
120-
new_drive = "{}:".format(new_drive_name)
121-
122-
target_path = repo_dir.mkdtemp()
123-
124-
set_up_result = windll.kernel32.DefineDosDeviceW(0, new_drive, target_path)
125-
if set_up_result == 0:
126-
raise RuntimeError("Failed to mount windows drive!")
127-
128-
# NOTE: new_drive has form of `A:` and joining it with some relative
129-
# path might result in non-existing path (A:path\\to)
130-
yield os.path.join(new_drive, os.sep)
131-
132-
tear_down_result = windll.kernel32.DefineDosDeviceW(
133-
DDD_REMOVE_DEFINITION, new_drive, target_path
134-
)
135-
if tear_down_result == 0:
136-
raise RuntimeError("Could not unmount windows drive!")
137-
138-
139104
@pytest.fixture
140105
def erepo(repo_dir):
141106
repo = TestDvcGitFixture()

tests/func/test_add.py

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from mock import patch
1313

1414
import dvc
15+
from dvc.cache import Cache
1516
from dvc.exceptions import DvcException
1617
from dvc.exceptions import RecursiveAddingWhileUsingFilename
1718
from dvc.exceptions import StageFileCorruptedError
@@ -24,7 +25,8 @@
2425
from dvc.utils import file_md5
2526
from dvc.utils import LARGE_DIR_SIZE
2627
from dvc.utils import relpath
27-
from dvc.utils.compat import range
28+
from dvc.utils.compat import range, fspath
29+
from dvc.utils.fs import path_isin
2830
from dvc.utils.stage import load_stage_file
2931
from tests.basic_env import TestDvc
3032
from tests.utils import get_gitignore_content
@@ -511,15 +513,56 @@ def test(self):
511513
self.assertTrue(System.is_hardlink(self.FOO))
512514

513515

516+
@pytest.fixture
517+
def temporary_windows_drive(tmp_path_factory):
518+
import string
519+
import win32api
520+
from ctypes import windll
521+
from win32con import DDD_REMOVE_DEFINITION
522+
523+
drives = [
524+
s[0].upper()
525+
for s in win32api.GetLogicalDriveStrings().split("\000")
526+
if len(s) > 0
527+
]
528+
529+
new_drive_name = [
530+
letter for letter in string.ascii_uppercase if letter not in drives
531+
][0]
532+
new_drive = "{}:".format(new_drive_name)
533+
534+
target_path = tmp_path_factory.mktemp("tmp_windows_drive")
535+
536+
set_up_result = windll.kernel32.DefineDosDeviceW(
537+
0, new_drive, fspath(target_path)
538+
)
539+
if set_up_result == 0:
540+
raise RuntimeError("Failed to mount windows drive!")
541+
542+
# NOTE: new_drive has form of `A:` and joining it with some relative
543+
# path might result in non-existing path (A:path\\to)
544+
yield os.path.join(new_drive, os.sep)
545+
546+
tear_down_result = windll.kernel32.DefineDosDeviceW(
547+
DDD_REMOVE_DEFINITION, new_drive, fspath(target_path)
548+
)
549+
if tear_down_result == 0:
550+
raise RuntimeError("Could not unmount windows drive!")
551+
552+
514553
@pytest.mark.skipif(os.name != "nt", reason="Windows specific")
515554
def test_windows_should_add_when_cache_on_different_drive(
516-
dvc_repo, repo_dir, temporary_windows_drive
555+
tmp_dir, dvc, temporary_windows_drive
517556
):
518-
ret = main(["config", "cache.dir", temporary_windows_drive])
519-
assert ret == 0
557+
dvc.config.set("cache", "dir", temporary_windows_drive)
558+
dvc.cache = Cache(dvc)
559+
560+
stage, = tmp_dir.dvc_gen({"file": "file"})
561+
cache_path = stage.outs[0].cache_path
520562

521-
ret = main(["add", repo_dir.DATA])
522-
assert ret == 0
563+
assert path_isin(cache_path, temporary_windows_drive)
564+
assert os.path.isfile(cache_path)
565+
filecmp.cmp("file", cache_path)
523566

524567

525568
def test_readding_dir_should_not_unprotect_all(tmp_dir, dvc, mocker):

0 commit comments

Comments
 (0)