Skip to content

Commit

Permalink
Add friendlier message if no noxfile.py (#463)
Browse files Browse the repository at this point in the history
* Add friendlier message if no noxfile.py

Fixes #462.

Add a friendlier message on the specific case that user is calling
nox from within a directory with no noxfile.

Existing test for this case modified and two additional tests
added to ensure lower level errors are still handled further down.

* Standardise no noxfile found message for noxfile with different names

Co-authored-by: Claudio Jolowicz <mail@claudiojolowicz.com>
  • Loading branch information
FollowTheProcess and cjolowicz authored Jul 10, 2021
1 parent 98e8c24 commit 8ee3ebe
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
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

0 comments on commit 8ee3ebe

Please sign in to comment.