| 1 |
import os |
| 2 |
import sys |
| 3 |
from importlib.util import spec_from_file_location |
| 4 |
from types import ModuleType |
| 5 |
from pathlib import Path |
| 6 |
|
| 7 |
from pytest import raises |
| 8 |
|
| 9 |
from invoke import Config |
| 10 |
from invoke.loader import Loader, FilesystemLoader as FSLoader |
| 11 |
from invoke.exceptions import CollectionNotFound |
| 12 |
|
| 13 |
from _util import support |
| 14 |
|
| 15 |
|
| 16 |
class _BasicLoader(Loader): |
| 17 |
""" |
| 18 |
Tests top level Loader behavior with basic finder stub. |
| 19 |
|
| 20 |
Used when we want to make sure we're testing Loader.load and not e.g. |
| 21 |
FilesystemLoader's specific implementation. |
| 22 |
""" |
| 23 |
|
| 24 |
def find(self, name): |
| 25 |
path = os.path.join(support, name) |
| 26 |
if os.path.exists(f"{path}.py"): |
| 27 |
path = f"{path}.py" |
| 28 |
elif os.path.exists(path): |
| 29 |
path = os.path.join(path, "__init__.py") |
| 30 |
spec = spec_from_file_location(name, path) |
| 31 |
return spec |
| 32 |
|
| 33 |
|
| 34 |
class Loader_: |
| 35 |
def exhibits_default_config_object(self): |
| 36 |
loader = _BasicLoader() |
| 37 |
assert isinstance(loader.config, Config) |
| 38 |
assert loader.config.tasks.collection_name == "tasks" |
| 39 |
|
| 40 |
def returns_module_and_location(self): |
| 41 |
mod, path = _BasicLoader().load("namespacing") |
| 42 |
assert isinstance(mod, ModuleType) |
| 43 |
assert path == support |
| 44 |
|
| 45 |
def may_configure_config_via_constructor(self): |
| 46 |
config = Config({"tasks": {"collection_name": "mytasks"}}) |
| 47 |
loader = _BasicLoader(config=config) |
| 48 |
assert loader.config.tasks.collection_name == "mytasks" |
| 49 |
|
| 50 |
def adds_module_parent_dir_to_sys_path(self): |
| 51 |
# Crummy doesn't-explode test. |
| 52 |
_BasicLoader().load("namespacing") |
| 53 |
|
| 54 |
def doesnt_duplicate_parent_dir_addition(self): |
| 55 |
_BasicLoader().load("namespacing") |
| 56 |
_BasicLoader().load("namespacing") |
| 57 |
# If the bug is present, this will be 2 at least (and often more, since |
| 58 |
# other tests will pollute it (!). |
| 59 |
assert sys.path.count(support) == 1 |
| 60 |
|
| 61 |
def can_load_package(self): |
| 62 |
loader = _BasicLoader() |
| 63 |
# Load itself doesn't explode (tests 'from . import xxx' internally) |
| 64 |
mod, enclosing_dir = loader.load("package") |
| 65 |
# Properties of returned values look as expected |
| 66 |
# (enclosing dir is always the one above the module-or-package) |
| 67 |
assert enclosing_dir == support |
| 68 |
assert mod.__file__ == str(Path(support) / "package" / "__init__.py") |
| 69 |
|
| 70 |
def load_name_defaults_to_config_tasks_collection_name(self): |
| 71 |
"load() name defaults to config.tasks.collection_name" |
| 72 |
|
| 73 |
class MockLoader(_BasicLoader): |
| 74 |
def find(self, name): |
| 75 |
# Sanity |
| 76 |
assert name == "simple_ns_list" |
| 77 |
return super().find(name) |
| 78 |
|
| 79 |
config = Config({"tasks": {"collection_name": "simple_ns_list"}}) |
| 80 |
loader = MockLoader(config=config) |
| 81 |
# More sanity: expect simple_ns_list.py (not tasks.py) |
| 82 |
mod, path = loader.load() |
| 83 |
assert mod.__file__ == os.path.join(support, "simple_ns_list.py") |
| 84 |
|
| 85 |
|
| 86 |
class FilesystemLoader_: |
| 87 |
def setup_method(self): |
| 88 |
self.loader = FSLoader(start=support) |
| 89 |
|
| 90 |
def discovery_start_point_defaults_to_cwd(self): |
| 91 |
assert FSLoader().start == os.getcwd() |
| 92 |
|
| 93 |
def exposes_start_point_as_attribute(self): |
| 94 |
assert FSLoader().start == os.getcwd() |
| 95 |
|
| 96 |
def start_point_is_configurable_via_kwarg(self): |
| 97 |
start = "/tmp" |
| 98 |
assert FSLoader(start=start).start == start |
| 99 |
|
| 100 |
def start_point_is_configurable_via_config(self): |
| 101 |
config = Config({"tasks": {"search_root": "nowhere"}}) |
| 102 |
assert FSLoader(config=config).start == "nowhere" |
| 103 |
|
| 104 |
def raises_CollectionNotFound_if_not_found(self): |
| 105 |
with raises(CollectionNotFound): |
| 106 |
self.loader.load("nope") |
| 107 |
|
| 108 |
def raises_ImportError_if_found_collection_cannot_be_imported(self): |
| 109 |
# Instead of masking with a CollectionNotFound |
| 110 |
with raises(ModuleNotFoundError): |
| 111 |
self.loader.load("oops") |
| 112 |
|
| 113 |
# TODO: Need CollectionImportError here |
| 114 |
|
| 115 |
def searches_towards_root_of_filesystem(self): |
| 116 |
# Loaded while root is in same dir as .py |
| 117 |
directly = self.loader.load("foo") |
| 118 |
# Loaded while root is multiple dirs deeper than the .py |
| 119 |
deep = os.path.join(support, "ignoreme", "ignoremetoo") |
| 120 |
indirectly = FSLoader(start=deep).load("foo") |
| 121 |
assert directly[0].__file__ == indirectly[0].__file__ |
| 122 |
assert directly[0].__spec__ == indirectly[0].__spec__ |
| 123 |
assert directly[1] == indirectly[1] |