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

Add friendlier message if no noxfile.py #463

Merged
merged 3 commits into from
Jul 10, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion nox/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def load_nox_module(global_config: Namespace) -> Union[types.ModuleType, int]:
# Be sure to expand variables
os.path.expandvars(global_config.noxfile)
)
noxfile_parent_dir = os.path.realpath(os.path.dirname(global_config.noxfile))

# Check ``nox.needs_version`` by parsing the AST.
check_nox_version(global_config.noxfile)
Expand All @@ -60,14 +61,19 @@ def load_nox_module(global_config: Namespace) -> Union[types.ModuleType, int]:
# This will ensure that the Noxfile's path is on sys.path, and that
# import-time path resolutions work the way the Noxfile author would
# guess.
os.chdir(os.path.realpath(os.path.dirname(global_config.noxfile)))
os.chdir(noxfile_parent_dir)
return importlib.machinery.SourceFileLoader(
"user_nox_module", global_config.noxfile
).load_module()

except (VersionCheckFailed, InvalidVersionSpecifier) as error:
logger.error(str(error))
return 2
except FileNotFoundError:
logger.error(
f"Failed to load Noxfile {global_config.noxfile}, no such file exists."
)
return 2
except (IOError, OSError):
logger.exception("Failed to load Noxfile {}".format(global_config.noxfile))
return 2
Expand Down
44 changes: 42 additions & 2 deletions tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import json
import os
import platform
from pathlib import Path
from textwrap import dedent
from unittest import mock

Expand Down Expand Up @@ -76,9 +77,48 @@ def test_load_nox_module_expandvars():
assert noxfile_module.SIGIL == "123"


def test_load_nox_module_not_found():
config = _options.options.namespace(noxfile="bogus.py")
def test_load_nox_module_not_found(caplog, tmp_path):
bogus_noxfile = tmp_path / "bogus.py"
config = _options.options.namespace(noxfile=str(bogus_noxfile))

assert tasks.load_nox_module(config) == 2
assert (
f"Failed to load Noxfile {bogus_noxfile}, no such file exists." in caplog.text
)


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.machinery.SourceFileLoader.load_module"
) 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.machinery.SourceFileLoader.load_module"
) as mock_load:
mock_load.side_effect = OSError

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


@pytest.fixture
Expand Down