core/windows: fix get_files and its tests

This commit is contained in:
Dima Gerasimov 2021-02-16 06:11:05 +00:00 committed by karlicoss
parent 6d9bc2964b
commit f102101b39
3 changed files with 59 additions and 22 deletions

View file

@ -157,21 +157,21 @@ def get_files(
for src in sources: for src in sources:
if src.parts[0] == '~': if src.parts[0] == '~':
src = src.expanduser() src = src.expanduser()
if src.is_dir(): # note: glob handled first, because e.g. on Windows asterisk makes is_dir unhappy
gs = str(src)
if '*' in gs:
if glob != DEFAULT_GLOB:
warnings.warn(f"{caller()}: treating {gs} as glob path. Explicit glob={glob} argument is ignored!")
paths.extend(map(Path, do_glob(gs)))
elif src.is_dir():
gp: Iterable[Path] = src.glob(glob) # todo not sure if should be recursive? gp: Iterable[Path] = src.glob(glob) # todo not sure if should be recursive?
paths.extend(gp) paths.extend(gp)
else: else:
ss = str(src) if not src.is_file():
if '*' in ss: # todo not sure, might be race condition?
if glob != DEFAULT_GLOB: raise RuntimeError(f"Expected '{src}' to exist")
warnings.warn(f"{caller()}: treating {ss} as glob path. Explicit glob={glob} argument is ignored!") # todo assert matches glob??
paths.extend(map(Path, do_glob(ss))) paths.append(src)
else:
if not src.is_file():
# todo not sure, might be race condition?
raise RuntimeError(f"Expected '{src}' to exist")
# todo assert matches glob??
paths.append(src)
if sort: if sort:
paths = list(sorted(paths)) paths = list(sorted(paths))

View file

@ -60,3 +60,7 @@ else:
from typing import Union from typing import Union
# erm.. I guess as long as it's not crashing, whatever... # erm.. I guess as long as it's not crashing, whatever...
Literal = Union Literal = Union
import os
windows = os.name == 'nt'

View file

@ -1,10 +1,37 @@
import os
from pathlib import Path from pathlib import Path
from my.common import get_files from typing import TYPE_CHECKING
from my.core.compat import windows
from my.core.common import get_files
import pytest # type: ignore import pytest # type: ignore
def test_single_file(): # hack to replace all /tmp with 'real' tmp dir
# not ideal, but makes tests more concise
def _get_files(x, *args, **kwargs):
import my.core.common as C
def repl(x):
if isinstance(x, str):
return x.replace('/tmp', TMP)
elif isinstance(x, Path):
assert x.parts[:2] == (os.sep, 'tmp') # meh
return Path(TMP) / Path(*x.parts[2:])
else:
# iterable?
return [repl(i) for i in x]
x = repl(x)
res = C.get_files(x, *args, **kwargs)
return tuple(Path(str(i).replace(TMP, '/tmp')) for i in res) # hack back for asserts..
if not TYPE_CHECKING:
get_files = _get_files
def test_single_file() -> None:
''' '''
Regular file path is just returned as is. Regular file path is just returned as is.
''' '''
@ -27,12 +54,13 @@ def test_single_file():
"if the path starts with ~, we expand it" "if the path starts with ~, we expand it"
assert get_files('~/.bashrc') == ( if not windows: # windows dowsn't have bashrc.. ugh
Path('~').expanduser() / '.bashrc', assert get_files('~/.bashrc') == (
) Path('~').expanduser() / '.bashrc',
)
def test_multiple_files(): def test_multiple_files() -> None:
''' '''
If you pass a directory/multiple directories, it flattens the contents If you pass a directory/multiple directories, it flattens the contents
''' '''
@ -57,7 +85,7 @@ def test_multiple_files():
) )
def test_explicit_glob(): def test_explicit_glob() -> None:
''' '''
You can pass a glob to restrict the extensions You can pass a glob to restrict the extensions
''' '''
@ -78,7 +106,7 @@ def test_explicit_glob():
assert get_files('/tmp/hpi_test', glob='file_*.zip') == expected assert get_files('/tmp/hpi_test', glob='file_*.zip') == expected
def test_implicit_glob(): def test_implicit_glob() -> None:
''' '''
Asterisc in the path results in globing too. Asterisc in the path results in globing too.
''' '''
@ -98,7 +126,7 @@ def test_implicit_glob():
) )
def test_no_files(): def test_no_files() -> None:
''' '''
Test for empty matches. They work, but should result in warning Test for empty matches. They work, but should result in warning
''' '''
@ -112,7 +140,10 @@ def test_no_files():
# TODO not sure if should uniquify if the filenames end up same? # TODO not sure if should uniquify if the filenames end up same?
# TODO not sure about the symlinks? and hidden files? # TODO not sure about the symlinks? and hidden files?
test_path = Path('/tmp/hpi_test') import tempfile
TMP = tempfile.gettempdir()
test_path = Path(TMP) / 'hpi_test'
def setup(): def setup():
teardown() teardown()
test_path.mkdir() test_path.mkdir()
@ -125,6 +156,8 @@ def teardown():
def create(f: str) -> None: def create(f: str) -> None:
# in test body easier to use /tmp regardless the OS...
f = f.replace('/tmp', TMP)
if f.endswith('/'): if f.endswith('/'):
Path(f).mkdir() Path(f).mkdir()
else: else: