-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tests load_for_restart, next_path + force_suffix
- Added slow tests for load_for_restart function - Function next_path moves to a module snek5000.util.files and gets a force_suffix parameter
- Loading branch information
Showing
11 changed files
with
203 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,6 +55,7 @@ tests = | |
pytest-cov | ||
pytest-datadir | ||
ipython | ||
pymech | ||
|
||
hpc = | ||
%(tests)s | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import re | ||
from pathlib import Path | ||
|
||
from .. import logger | ||
|
||
|
||
def next_path(old_path, force_suffix=False): | ||
"""Generate a new path with an integer suffix | ||
Parameters | ||
---------- | ||
old_path: str or path-like | ||
Path to check for existence | ||
force_suffix: | ||
If true, will not check if the `old_path` can be used and adds | ||
a suffix in the end. | ||
Returns | ||
------- | ||
new_path: Path | ||
A path (with an integer suffix) which does not yet exist in the | ||
filesystem. | ||
Example | ||
------- | ||
>>> import os | ||
>>> os.chdir("/tmp") | ||
>>> next_path("test.txt") # path does not exist | ||
PosixPath('test.txt') | ||
>>> next_path("test.txt", force_suffix=True) # path does not exists | ||
PosixPath('test_00.txt') | ||
>>> Path("test.txt").touch() | ||
>>> next_path("test.txt") # path exists | ||
PosixPath('test_00.txt') | ||
>>> Path("test_00.txt").touch() | ||
>>> next_path("test.txt") # path and the next one both exists | ||
PosixPath('test_01.txt') | ||
>>> Path("test.txt").unlink() # cleanup | ||
>>> Path("test_00.txt").unlink() | ||
""" | ||
|
||
def int_suffix(p, integer): | ||
stem = p.stem | ||
# for example: remove .tar from the end, if any | ||
for suffix in p.suffixes: | ||
stem = re.sub(f"{suffix}$", "", stem) | ||
|
||
return p.parent / "".join([stem, f"_{integer:02d}", *p.suffixes]) | ||
|
||
old_path = Path(old_path) | ||
|
||
if not force_suffix and not old_path.exists(): | ||
return old_path | ||
|
||
i = 0 | ||
new_path = int_suffix(old_path, i) | ||
|
||
while new_path.exists(): | ||
logger.debug(f"Checking if path exists: {new_path}") | ||
new_path = int_suffix(old_path, i) | ||
i += 1 | ||
|
||
logger.debug(f"Next path available: {new_path}") | ||
|
||
return new_path |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
from pathlib import Path | ||
|
||
from snek5000.util import files | ||
|
||
|
||
def test_next_path_no_files(tmpdir): | ||
tmpdir = Path(tmpdir) | ||
|
||
# Create no files, multiple suffixes | ||
target = tmpdir / "test.tar.gz" | ||
|
||
assert str(files.next_path(target, force_suffix=True)) == str( | ||
tmpdir / "test_00.tar.gz" | ||
) | ||
|
||
|
||
def test_next_path_one_file(tmpdir): | ||
tmpdir = Path(tmpdir) | ||
|
||
# Create a file | ||
target = tmpdir / "test.txt" | ||
target.touch() # Not necessary, but to see what happens. | ||
|
||
assert str(files.next_path(target, force_suffix=True)) == str( | ||
tmpdir / "test_00.txt" | ||
) | ||
|
||
|
||
def test_next_path_one_file_no_suffix(tmpdir): | ||
tmpdir = Path(tmpdir) | ||
|
||
# Create a file | ||
target = tmpdir / "test.txt" | ||
target.touch() | ||
(tmpdir / "test_00.txt").touch() | ||
|
||
assert str(files.next_path(target)) == str(tmpdir / "test_01.txt") | ||
|
||
|
||
def test_next_path_two_files(tmpdir): | ||
tmpdir = Path(tmpdir) | ||
|
||
# Create multple files | ||
target = tmpdir / "test.txt" | ||
(tmpdir / "test_00.txt").touch() | ||
(tmpdir / "test_01.txt").touch() | ||
|
||
assert str(files.next_path(target, force_suffix=True)) == str( | ||
tmpdir / "test_02.txt" | ||
) | ||
|
||
|
||
def test_next_path_dir_no_suffix(tmpdir): | ||
tmpdir = Path(tmpdir) | ||
|
||
# Create a directory | ||
target = tmpdir / "test_dir" | ||
target.mkdir() | ||
|
||
assert str(files.next_path(target)) == str(tmpdir / "test_dir_00") | ||
|
||
|
||
def test_next_path_dir(tmpdir): | ||
tmpdir = Path(tmpdir) | ||
|
||
target = tmpdir / "test_dir" | ||
|
||
assert str(files.next_path(target, force_suffix=True)) == str( | ||
tmpdir / "test_dir_00" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters