Skip to content

Commit

Permalink
Refactor code to make it simpler
Browse files Browse the repository at this point in the history
No change in functionality except the error that is displayed when the
source file does not exist is a little diffferent.
  • Loading branch information
omarkohl committed Oct 7, 2018
1 parent 499e263 commit fbfee9f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 56 deletions.
106 changes: 51 additions & 55 deletions pytest_datafiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,31 @@
import pytest


def _copy(src, target):
if not src.exists():
raise ValueError("'%s' does not exist!" % src)
if src.isdir():
src.copy(target / src.basename)
else: # file
src.copy(target)


def _copy_all(entry_list, target_dir, on_duplicate):
for entry in entry_list:
target_entry = target_dir / entry.basename
if not target_entry.exists() or on_duplicate == 'overwrite':
_copy(entry, target_dir)
elif on_duplicate == 'exception':
raise ValueError(
"'%s' already exists (src %s)" % (
target_entry,
entry,
)
)
else: # ignore
continue


def _is_str(obj):
"""
Check if 'obj' is a string (both Python 2 and 3)
Expand All @@ -16,48 +41,30 @@ def _is_str(obj):
return isinstance(obj, str)


def _copy_file(target_dir, entry, on_duplicate):
basename = entry.basename
if on_duplicate == 'overwrite' or not (target_dir / basename).exists():
entry.copy(target_dir)
elif on_duplicate == 'exception':
raise ValueError(
"'%s' already exists (entry %s)" % (basename, entry)
)
else:
# on_duplicate == 'ignore': do nothing with entry
pass
def _to_py_path(entry_list):
converted = []
for entry in entry_list:
if _is_str(entry):
converted.append(path.local(entry))
else:
converted.append(entry)
return converted


def _get_all_entries(entry_list, keep_top_dir):
all_files = []

entry_list = _to_py_path(entry_list)

def _copy_dir(target_dir, entry, on_duplicate, keep_top_dir):
basename = entry.basename
if keep_top_dir:
if on_duplicate == 'overwrite' or not (target_dir / basename).exists():
entry.copy(target_dir / basename)
elif on_duplicate == 'exception':
raise ValueError(
"'%s' already exists (entry %s)" % (basename, entry)
)
# else: on_duplicate == 'ignore': do nothing with entry
all_files = entry_list
else:
# Regular directory (no keep_top_dir). Need to check every file
# for duplicates
if on_duplicate == 'overwrite':
entry.copy(target_dir)
return
for sub_entry in entry.listdir():
if not (target_dir / sub_entry.basename).exists():
sub_entry.copy(target_dir / sub_entry.basename)
continue
if on_duplicate == 'exception':
# target exists
raise ValueError(
"'%s' already exists (entry %s)" % (
(target_dir / sub_entry.basename),
sub_entry,
)
)
# on_duplicate == 'ignore': do nothing with e2
for entry in entry_list:
if entry.isdir():
all_files.extend(entry.listdir())
else:
all_files.append(entry)
return all_files


@pytest.fixture
Expand All @@ -66,35 +73,24 @@ def datafiles(request, tmpdir):
pytest fixture to define a 'tmpdir' containing files or directories
specified with a 'datafiles' mark.
"""
content = []
entry_list = []
options = {
'keep_top_dir': False,
'on_duplicate': 'exception', # ignore, overwrite
}
for mark in request.node.iter_markers('datafiles'):
content.extend(mark.args)
entry_list.extend(mark.args)
options.update(mark.kwargs)

on_duplicate = options['on_duplicate']
keep_top_dir = options['keep_top_dir']

if not content:
return tmpdir

if keep_top_dir not in (True, False):
raise ValueError("'keep_top_dir' must be True or False")
if on_duplicate not in ('exception', 'ignore', 'overwrite'):
raise ValueError("'on_duplicate' must be 'exception', 'ignore' or "
"'overwrite'")
for entry in content:
if _is_str(entry):
entry = path.local(entry)
if entry.isfile():
_copy_file(tmpdir, entry, on_duplicate)
elif entry.isdir():
_copy_dir(tmpdir, entry, on_duplicate, keep_top_dir)
else:
raise ValueError(
"entry '%s' is neither file nor dir. Possibly it doesn't "
"exist." % entry
)

all_entries = _get_all_entries(entry_list, keep_top_dir)
_copy_all(all_entries, tmpdir, on_duplicate)
return tmpdir
2 changes: 1 addition & 1 deletion tests/test_pytest_datafiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ def test_ode(datafiles):
'''.format(FIXTURE_DIR))
result = testdir.runpytest('-s')
result.stdout.fnmatch_lines([
"E*ValueError:*fileZZ'*is neither file nor dir.*",
"E*ValueError:*fileZZ'*does not exist!*",
])


Expand Down

0 comments on commit fbfee9f

Please sign in to comment.