def test_check_nox_version_succeeds(temp_noxfile, specifiers: str) -> None: """It does not raise if the version specifiers are satisfied.""" text = dedent(f""" import nox nox.needs_version = "{specifiers}" """) check_nox_version(temp_noxfile(text))
def test_check_nox_version_fails(temp_noxfile, specifiers: str) -> None: """It raises an exception if the version specifiers are not satisfied.""" text = dedent(f""" import nox nox.needs_version = "{specifiers}" """) with pytest.raises(VersionCheckFailed): check_nox_version(temp_noxfile(text))
def test_check_nox_version_invalid(temp_noxfile, specifiers: str) -> None: """It raises an exception if the version specifiers cannot be parsed.""" text = dedent(f""" import nox nox.needs_version = "{specifiers}" """) with pytest.raises(InvalidVersionSpecifier): check_nox_version(temp_noxfile(text))
def load_nox_module(global_config: Namespace) -> types.ModuleType | int: """Load the user's Noxfile and return the module object for it. .. note:: This task has two side effects; it makes ``global_config.noxfile`` an absolute path, and changes the working directory of the process. Args: global_config (.nox.main.GlobalConfig): The global config. Returns: module: The module designated by the Noxfile path. """ try: # Save the absolute path to the Noxfile. # This will inoculate it if Nox changes paths because of an implicit # or explicit chdir (like the one below). global_config.noxfile = os.path.realpath( # 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) # Move to the path where the Noxfile is. # 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. The original working directory (the directory that Nox was # invoked from) gets stored by the .invoke_from "option" in _options. os.chdir(noxfile_parent_dir) 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 OSError: logger.exception(f"Failed to load Noxfile {global_config.noxfile}") return 2 else: return _load_and_exec_nox_module(global_config)
def load_nox_module(global_config: Namespace) -> Union[types.ModuleType, int]: """Load the user's noxfile and return the module object for it. .. note:: This task has two side effects; it makes ``global_config.noxfile`` an absolute path, and changes the working directory of the process. Args: global_config (.nox.main.GlobalConfig): The global config. Returns: module: The module designated by the Noxfile path. """ try: # Save the absolute path to the Noxfile. # This will inoculate it if nox changes paths because of an implicit # or explicit chdir (like the one below). global_config.noxfile = os.path.realpath( # Be sure to expand variables os.path.expandvars(global_config.noxfile)) # Check ``nox.needs_version`` by parsing the AST. check_nox_version(global_config.noxfile) # Move to the path where the Noxfile is. # 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))) return importlib.machinery.SourceFileLoader( "user_nox_module", global_config.noxfile).load_module() # type: ignore except (VersionCheckFailed, InvalidVersionSpecifier) as error: logger.error(str(error)) return 2 except (IOError, OSError): logger.exception("Failed to load Noxfile {}".format( global_config.noxfile)) return 2