|
| 1 | +"""What discovery scans when it's given a package, or nothing at all. |
| 2 | +
|
| 3 | +A package name covers everything under it. No names means the working |
| 4 | +directory: its top-level modules and packages, less the private ones and |
| 5 | +the conventional non-targets that tend to blow up on import. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import importlib |
| 11 | +import sys |
| 12 | +import textwrap |
| 13 | + |
| 14 | +import pytest |
| 15 | + |
| 16 | +from coflux.discovery import discover_targets |
| 17 | + |
| 18 | +_TASK = textwrap.dedent( |
| 19 | + """ |
| 20 | + import coflux as cf |
| 21 | +
|
| 22 | + @cf.task() |
| 23 | + def {name}(): |
| 24 | + return 1 |
| 25 | + """ |
| 26 | +) |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture |
| 30 | +def project(tmp_path, monkeypatch): |
| 31 | + """A throwaway project as the working directory, cleaned out of |
| 32 | + ``sys.modules`` afterwards so its names don't leak between tests.""" |
| 33 | + monkeypatch.chdir(tmp_path) |
| 34 | + monkeypatch.syspath_prepend(str(tmp_path)) |
| 35 | + importlib.invalidate_caches() |
| 36 | + before = set(sys.modules) |
| 37 | + |
| 38 | + def write(path, source): |
| 39 | + file = tmp_path / path |
| 40 | + file.parent.mkdir(parents=True, exist_ok=True) |
| 41 | + file.write_text(source) |
| 42 | + |
| 43 | + yield write |
| 44 | + |
| 45 | + for name in set(sys.modules) - before: |
| 46 | + sys.modules.pop(name, None) |
| 47 | + |
| 48 | + |
| 49 | +def _names(targets): |
| 50 | + return sorted((t["module"], t["name"]) for t in targets) |
| 51 | + |
| 52 | + |
| 53 | +def test_a_package_covers_its_submodules(project): |
| 54 | + project("dpkg/__init__.py", "") |
| 55 | + project("dpkg/flows.py", _TASK.format(name="flow")) |
| 56 | + project("dpkg/deep/__init__.py", "") |
| 57 | + project("dpkg/deep/jobs.py", _TASK.format(name="job")) |
| 58 | + project("dpkg/_private.py", _TASK.format(name="hidden")) |
| 59 | + |
| 60 | + targets, errors = discover_targets(["dpkg"]) |
| 61 | + |
| 62 | + assert errors == [] |
| 63 | + assert _names(targets) == [("dpkg.deep.jobs", "job"), ("dpkg.flows", "flow")] |
| 64 | + |
| 65 | + |
| 66 | +def test_no_modules_scans_the_working_directory(project): |
| 67 | + project("dwd_app/__init__.py", "") |
| 68 | + project("dwd_app/flows.py", _TASK.format(name="flow")) |
| 69 | + project("dwd_scratch.py", _TASK.format(name="scratch")) |
| 70 | + project("_dwd_hidden.py", _TASK.format(name="hidden")) |
| 71 | + # Skipped by name, so importing them is never attempted |
| 72 | + project("conftest.py", "raise RuntimeError('conftest imported')") |
| 73 | + project("setup.py", "raise RuntimeError('setup imported')") |
| 74 | + project("tests/__init__.py", "raise RuntimeError('tests imported')") |
| 75 | + # Not a package without __init__.py, and not a module at all |
| 76 | + project("dwd_data/flows.py", _TASK.format(name="orphan")) |
| 77 | + project("notes.txt", "") |
| 78 | + |
| 79 | + targets, errors = discover_targets([]) |
| 80 | + |
| 81 | + assert errors == [] |
| 82 | + assert _names(targets) == [("dwd_app.flows", "flow"), ("dwd_scratch", "scratch")] |
| 83 | + |
| 84 | + |
| 85 | +def test_an_empty_working_directory_finds_nothing(project): |
| 86 | + project("notes.txt", "") |
| 87 | + |
| 88 | + assert discover_targets([]) == ([], []) |
| 89 | + |
| 90 | + |
| 91 | +def test_a_broken_module_in_the_working_directory_is_reported(project): |
| 92 | + project("dwd_ok.py", _TASK.format(name="ok")) |
| 93 | + project("dwd_broken.py", "import does_not_exist") |
| 94 | + |
| 95 | + targets, errors = discover_targets([]) |
| 96 | + |
| 97 | + assert _names(targets) == [("dwd_ok", "ok")] |
| 98 | + assert [e.module for e in errors] == ["dwd_broken"] |
0 commit comments