From 8fd797bfe3d47b47a1cbedd3dae8a55d8aa72662 Mon Sep 17 00:00:00 2001 From: James Souter Date: Mon, 2 Sep 2024 09:13:50 +0100 Subject: [PATCH] Check if autosave directory exists on configure call stop autosave IOC test from running forever --- softioc/autosave.py | 9 +++++++-- tests/test_autosave.py | 6 ++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/softioc/autosave.py b/softioc/autosave.py index 02fca417..4f02e8f5 100644 --- a/softioc/autosave.py +++ b/softioc/autosave.py @@ -47,7 +47,12 @@ def configure( enabled: boolean which enables or disables autosave, set to True by default, or False if configure not called. """ - Autosave.directory = Path(directory) + directory_path = Path(directory) + if not directory_path.is_dir(): + raise FileNotFoundError( + f"{directory} is not a valid autosave directory" + ) + Autosave.directory = directory_path Autosave.timestamped_backups = timestamped_backups Autosave.save_period = save_period Autosave.enabled = enabled @@ -136,7 +141,7 @@ def __exit__(self, A, B, C): @classmethod def __backup_sav_file(cls): - if not cls.directory and cls.directory.is_dir(): + if not cls.directory or not cls.directory.is_dir(): print( f"Could not back up autosave as {cls.directory} is" " not a valid directory", diff --git a/tests/test_autosave.py b/tests/test_autosave.py index a6c6718b..7d5e24b3 100644 --- a/tests/test_autosave.py +++ b/tests/test_autosave.py @@ -84,9 +84,8 @@ def test_autosave_defaults(): def test_configure_dir_doesnt_exist(tmp_path): DEVICE_NAME = "MY_DEVICE" builder.aOut("MY-RECORD", autosave=True) - autosave.configure(tmp_path / "subdir-doesnt-exist", DEVICE_NAME) with pytest.raises(FileNotFoundError): - autosave.load_autosave() + autosave.configure(tmp_path / "subdir-doesnt-exist", DEVICE_NAME) def test_returns_if_init_called_before_configure(): @@ -374,6 +373,8 @@ def check_all_record_types_save_properly(device_name, autosave_dir, conn): assert saved["Action"] == 1 assert (saved["WaveformIn"] == numpy.array([1, 2, 3, 4])).all() assert (saved["WaveformOut"] == numpy.array([1, 2, 3, 4])).all() + autosave.Autosave._stop() + # force autosave thread to stop to ensure pytest exits conn.send("D") @@ -403,6 +404,7 @@ def check_autosave_field_names_contain_device_prefix( saved = yaml.full_load(f) assert "BEFORE" in saved.keys() assert f"{device_name}:AFTER" in saved.keys() + autosave.Autosave._stop() conn.send("D")