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

Fix incorrect FileNotFoundError in load_nox_module #571

Merged
merged 2 commits into from
Feb 15, 2022
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
13 changes: 5 additions & 8 deletions nox/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,15 @@ def _load_and_exec_nox_module(global_config: Namespace) -> types.ModuleType:
spec = importlib.util.spec_from_file_location(
"user_nox_module", global_config.noxfile
)
if not spec:
raise OSError(f"Could not get module spec from {global_config.noxfile}")
assert spec is not None # If None, fatal importlib error, would crash anyway

module = importlib.util.module_from_spec(spec)
if not module:
raise OSError(f"Noxfile {global_config.noxfile} is not a valid python module.")
assert module is not None # If None, fatal importlib error, would crash anyway

sys.modules["user_nox_module"] = module

loader = spec.loader
if not loader: # pragma: no cover
raise OSError(f"Could not get module loader for {global_config.noxfile}")
assert loader is not None # If None, fatal importlib error, would crash anyway
# See https://docs.python.org/3/library/importlib.html#importing-a-source-file-directly
loader.exec_module(module)
return module
Expand Down Expand Up @@ -101,8 +98,6 @@ def load_nox_module(global_config: Namespace) -> types.ModuleType | int:
# invoked from) gets stored by the .invoke_from "option" in _options.
os.chdir(noxfile_parent_dir)

return _load_and_exec_nox_module(global_config)

except (VersionCheckFailed, InvalidVersionSpecifier) as error:
logger.error(str(error))
return 2
Expand All @@ -114,6 +109,8 @@ def load_nox_module(global_config: Namespace) -> types.ModuleType | int:
except OSError:
logger.exception(f"Failed to load Noxfile {global_config.noxfile}")
return 2
else:
return _load_and_exec_nox_module(global_config)


def merge_noxfile_options(
Expand Down
56 changes: 6 additions & 50 deletions tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import json
import os
import platform
from pathlib import Path
from textwrap import dedent
from unittest import mock

Expand Down Expand Up @@ -89,56 +88,13 @@ def test_load_nox_module_not_found(caplog, tmp_path):
)


def test_load_nox_module_IOError(caplog):

# Need to give it a noxfile that exists so load_nox_module can progress
# past FileNotFoundError
# use our own noxfile.py for this
our_noxfile = Path(__file__).parent.parent.joinpath("noxfile.py")
config = _options.options.namespace(noxfile=str(our_noxfile))

with mock.patch("nox.tasks.importlib.util.module_from_spec") as mock_load:
mock_load.side_effect = IOError

assert tasks.load_nox_module(config) == 2
assert "Failed to load Noxfile" in caplog.text


def test_load_nox_module_OSError(caplog):

# Need to give it a noxfile that exists so load_nox_module can progress
# past FileNotFoundError
# use our own noxfile.py for this
our_noxfile = Path(__file__).parent.parent.joinpath("noxfile.py")
config = _options.options.namespace(noxfile=str(our_noxfile))

with mock.patch("nox.tasks.importlib.util.module_from_spec") as mock_load:
mock_load.side_effect = OSError

def test_load_nox_module_os_error(caplog):
noxfile = os.path.join(RESOURCES, "noxfile.py")
config = _options.options.namespace(noxfile=noxfile)
with mock.patch("nox.tasks.check_nox_version", autospec=True) as version_checker:
version_checker.side_effect = OSError
assert tasks.load_nox_module(config) == 2
assert "Failed to load Noxfile" in caplog.text


def test_load_nox_module_invalid_spec():
our_noxfile = Path(__file__).parent.parent.joinpath("noxfile.py")
config = _options.options.namespace(noxfile=str(our_noxfile))

with mock.patch("nox.tasks.importlib.util.spec_from_file_location") as mock_spec:
mock_spec.return_value = None

with pytest.raises(IOError):
tasks._load_and_exec_nox_module(config)


def test_load_nox_module_invalid_module():
our_noxfile = Path(__file__).parent.parent.joinpath("noxfile.py")
config = _options.options.namespace(noxfile=str(our_noxfile))

with mock.patch("nox.tasks.importlib.util.module_from_spec") as mock_spec:
mock_spec.return_value = None

with pytest.raises(IOError):
tasks._load_and_exec_nox_module(config)
assert f"Failed to load Noxfile {noxfile}" in caplog.text


@pytest.fixture
Expand Down