Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better error messages and tests for them #66

Merged
merged 1 commit into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions cosmosis/runtime/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,25 +281,23 @@ def load_library(filepath):

"""

if filepath.endswith('so') or filepath.endswith('dylib'):
if not os.path.exists(filepath):
raise SetupError(f"You specified a path {filepath} for a module. "
"This file does not exist.")

elif filepath.endswith('so') or filepath.endswith('dylib'):
language = MODULE_LANG_DYLIB
try:
library = ctypes.cdll.LoadLibrary(filepath)
except OSError as error:
exists = os.path.exists(filepath)
if exists:
raise SetupError("You specified a path %s for a module. "
"File exists, but could not be opened. "
"Error was %s" % (filepath, error))
else:
raise SetupError("You specified a path %s for a module. "
"File does not exist. Error was %s" %
(filepath, error))
raise SetupError(f"You specified a path {filepath} for a module. "
f"File exists, but could not be opened. "
f"Error was {error}")
elif filepath.endswith(".jl"):
from . import julia_modules
library = julia_modules.JuliaModule(filepath)
language = MODULE_LANG_JULIA
else:
elif filepath.endswith(".py"):
language = MODULE_LANG_PYTHON
dirname, filename = os.path.split(filepath)
# allows .pyc and .py modules to be used
Expand All @@ -313,6 +311,12 @@ def load_library(filepath):
"was unable to load it. Error was %s" %
(filepath, error))
sys.path.pop(0)
else:
raise SetupError(f"You specified a path {filepath} for a module. "
"I do not know what kind of module this is "
"because the suffix is not .so, .dylib, .jl, or .py"
)


return library, language

Expand Down
17 changes: 17 additions & 0 deletions cosmosis/test/test_modules.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import cosmosis
import pytest
import tempfile

def test_missing_module():
with pytest.raises(cosmosis.runtime.SetupError) as error_info:
m = cosmosis.Module("pretend", "./this_file_does_not_exist.py")

assert "file does not exist" in str(error_info.value)


def test_wrong_path_type():
with tempfile.NamedTemporaryFile(suffix=".x") as f:
with pytest.raises(cosmosis.runtime.SetupError) as error_info:
m = cosmosis.Module("pretend", f.name)

assert "do not know what kind of module this is" in str(error_info.value)