Skip to content

Commit

Permalink
Implemented assert_file_not_in_use
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiisoup committed Nov 11, 2024
1 parent d550c0d commit c0b5276
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
Empty file removed testings/corrupt_data2/tmp.sif
Empty file.
44 changes: 27 additions & 17 deletions testings/test_open.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
files = os.listdir(d)
corrupt_filenames = [d + f for f in files if f[-4:] in [".sif", ".SIF"]]

corrupt_filenames2 = [THIS_DIR + '/corrupt_data2/tmp.sif']

d = THIS_DIR + "/spool_data/"
spool_file_dirs = []
for e in [d + "/data_corrupted/"]:
Expand All @@ -51,6 +49,32 @@
)


def assert_file_not_in_use(filename):
platform = sys.platform
if platform == "linux":
widlcard = "/proc/*/fd/*"
lfds = glob.glob(widlcard)
for fds in lfds:
try:
file = os.readlink(fds)
if file == filename:
return True
except OSError as err:
if err.errno == 2:
file = None
else:
raise(err)
raise ValueError("file is in use.")
elif platform == "darwin":
raise NotImplementedError('Testing in Mac is not supported.')
elif platform == "win32":
# for windows:
os.rename(filename, filename)
else:
raise Exception("OS not supported")



def test_step_and_glue():
filename = THIS_DIR + "/step_and_glue/step_and_glue.sif"
with open(filename, "rb") as f:
Expand Down Expand Up @@ -194,11 +218,7 @@ def test_corrupt_file(filename):
data, info = sif_parser.np_open(filename)

# try open with a write mode to make sure the file is closed
try:
data, info = sif_parser.np_open(filename)
except ValueError:
with open(filename, 'a') as f:
assert f.writable()
assert_file_not_in_use(filename)

with pytest.warns(UserWarning, match="corrupt."):
data, info = sif_parser.np_open(filename, ignore_corrupt=True)
Expand All @@ -210,16 +230,6 @@ def test_corrupt_file(filename):
data = sif_parser.xr_open(filename, ignore_corrupt=True)


@pytest.mark.parametrize("filename", corrupt_filenames2)
def test_corrupt_file2(filename):
# try open with a write mode to make sure the file is closed
try:
data, info = sif_parser.np_open(filename)
except (ValueError, SyntaxError):
with open(filename, 'a') as f:
assert f.writable()


@pytest.mark.parametrize(
("filename", "raman_wavelength"),
[
Expand Down

0 comments on commit c0b5276

Please sign in to comment.