Skip to content

diffcone 0.5.0

Choose a tag to compare

@github-actions github-actions released this 21 Oct 22:57
· 90 commits to main since this release

Planning

  • diffcone.toml takes [index] exclude, patterns for .py files to
    leave out of the analysis (deliberately invalid test data, as in a
    formatter's or a linter's test suite). A matching file is read as a data
    file: plans no longer degrade, collect records the checkout, and a
    change to one selects everything in a plan without a recording; with
    one, the tests that read the file as data, and every test when a test
    imported or ran its code. What an excluded file
    runs is not read, so code that imports or runs one (an import,
    import_module, runpy, a subprocess, a file loader) is selected on any
    change, with a reason naming the file; with a recording, a change to an
    excluded file a test imported selects every test. Discovery's notes on
    an excluded plugin, base class or benchmark file name the exclusion. The
    report lists each snapshot's patterns (analysis.<side>.excluded).

Discovery

  • Under --doctest-modules, a docstring set when the module is imported
    (f.__doc__ = ..., a string built from parts) is read when its text can
    be worked out. Otherwise (a decorator of yours rewrites it, or the value
    is imported or computed) a function whose written docstring has
    examples has its doctest always selected, and one without is reported
    (exit 3).
  • Tests bound by a call (test_one = make(1), functools.partial), a
    lambda, globals() or setattr, and pytest-bdd's scenarios(), are
    reported (exit 3). Module data that merely has a test's name is not.
  • A usefixtures mark discovery cannot read (a non-literal argument, a
    decorator or factory that applies it, pytestmark = marks()) counts as
    every visible fixture.
  • Test classes pytest does not collect (an inherited __init__ or
    __new__, a dataclass or attrs class) are no longer targets: run
    reported them as selected tests pytest did not collect.

Report

  • schema_version 4: selected and unselected target records no longer
    list lifecycle_dependencies. Each target repeated every fixture,
    hook and module it depends on, nine tenths of a large suite's plan
    (850 MB for one of 25 000 tests, now 40 MB). Discovery's output still lists them, and
    the ones that selected a target are in its explanation.

Execution evidence

  • collect warns when none of the tests it recorded is among the tests
    discovered at that commit: pytest's rootdir lies outside the repository
    (a configuration file in a directory above it), so plans would find no
    recording for any test and select them all.
  • A test not all of whose cases ran in the recording (deselected by
    -k, -m or --deselect, or cut short by -x or --maxfail, in the
    arguments, addopts or PYTEST_ADDOPTS) gets no record, so every plan
    selects it until it is recorded whole; collect says how many tests
    have no record. A record of only some cases was taken for the whole
    test, so a later break in a case it never ran went unselected.
  • run --collect runs with --maxfail=0, as collect does, and keeps
    every flag of the record it replaces.
  • PYTEST_ADDOPTS is part of the recorded environment: diffcone run
    with different options runs the plan from the code instead of the
    recording.
  • The evidence store format is 7: record again after upgrading.
  • With a recording, more of the state tests share is followed:
    • a method, property setter or __setattr__ called on an object whose
      class is known (ENGINE = Engine(), Engine().run(k), a local or an
      attribute holding one), both from a change up to what stores its result
      and from a store down to what it writes; an object of unknown type is
      still not followed;
    • an attribute bound on an object (self.value = compute(),
      setattr(self, name, v)), reaching the code that reads it off the
      object (CONF.value);
    • a store through what a method returns (self._table()[k] = v);
    • what an initialiser binds on an object that calling its class may give
      back again (a metaclass __call__, a class decorator, __new__, a
      library base);
    • a function reached through an alias (_put = _impl,
      staticmethod(_impl)), __class_getitem__, and a memoiser applied by
      a call (get = cached(cache={})(_get));
    • project code storing onto a library module during a test, which was
      taken for test code;
    • a changed data file a test read: what that test ran stored is seeded,
      so a later test served what it loaded is selected. Lines read only to
      show a traceback are not data.
  • The recorder clears a functools cache over another wrapper
    (singledispatch, staticmethod, functools.partial, a class, a
    callable object); one it cannot clear is planned as a memoiser.

Running and checking

  • diffcone run --allow-incomplete-discovery starts pytest even when
    nothing is selected, with every target deselected, so the tests
    discovery does not know about still run (--dry-run says so).
  • diffcone run no longer refuses to run because of its own results
    (diffcone-results/) in a flat layout, or a generated, git-ignored
    _version.py that only binds constants.
  • diffcone check (and the check action) says when it compared the plan
    only, without diffcone's own run.
  • diffcone report counts a pull-request run that ran none of its
    selected tests as a problem (a plan that failed, a run that was refused,
    or one that failed after a good plan), so comment: on-problem comments
    on a pull request that ran no tests.
  • diffcone run exits 3 when the selection plugin did not run (a command
    or wrapper that drops its arguments, a tox or nox target without
    {posargs}): the selected tests never ran, and it exited 0.
  • diffcone validate and corpus compare outcomes case by case: a case
    that changed to an outcome another case of the same test already had
    was no change.
  • The run action's selected output and the check action use what
    ran (ran.json), not the plan, when they differ.

Fixed

  • Planning a large suite took far more memory than it needed: every test
    carried its own copy of the dependencies all tests share (code run
    while pytest collects), in the dependency graph and in the cached
    discovery results. On a suite of 25 000 tests a plan peaked at 21 GB,
    more than a standard CI runner has, and took up to 90 minutes; it now
    peaks at 5.5 GB and takes under two minutes, and a cached discovery
    result is 9 MB instead of 680 MB. Plans are unchanged. Discovery
    cached by an earlier version is redone once.
  • With a recording, a module cache filled by a function the cache's
    caller calls (_put(key, compute(key)), a method, a chain of helpers,
    an append, or a write under a branch on the result) was not followed:
    a test that read what an earlier test stored there, without running
    compute, was not selected when compute changed. Every in-place write
    of the functions that code calls is now followed, where diffcone can
    tell which function a call runs.
  • With a recording, a cache kept on an object, a class, a closure or a
    function attribute counted a store only when the stored value named the
    changed code directly. A value computed through a variable, a table or
    super(), read off a changed constant, or the writer's own literal could
    leave a later test that read the cache unselected. Every store counts
    now, as for module variables, including stores through __dict__ and
    vars(). What __init__ sets on the object it is building does not
    count; calling __init__ again on an object in use does.
  • With a recording, a module the analysis does not read (outside the
    source roots) that a test imported was credited to that test alone,
    though later tests use it without running any of it: a change to it now
    selects every test. A recording made by an earlier version does not say
    which such files a test imported, so any .py file a test touched
    counts.
  • A write to state held on a class (Config.mode = m,
    Config.items.append(x), setattr(Config, ...), cls._instance = ...,
    type(self)._cache[k] = v) did not make the writer a dependency of the
    class's readers; nor did a write to a module variable under another
    name: a local alias (cache = _CACHE), an item of it, a bound method,
    globals()["_CACHE"], or what one of your functions returns
    (registry()[k] = v). Both are followed now, with and without a
    recording. Copies (dict(_CACHE), .copy()) and an instance's own
    attributes are not shared state.
  • A write into state a class holds, made through a module-level instance
    of it (STORE._cache[k] = v, or STORE.cache()[k] = v where cache
    returns the class-level _cache), did not make the writer a dependency
    of the code that reads that state through the class. It does now, with
    and without a recording.
  • The same held for an instance a function makes itself: a change in
    place through it (s = Store(); s._items.append(x), an instance made
    with cls.__new__(cls) or returned by a factory, or one handed to a
    function that changes it) did not reach the code that reads the
    class-level state, when the class or one of its bases holds some. It
    does now. Setting an attribute on a new instance (new = cls.__new__(cls); new._key = None, as copy and replace methods do) is
    not a change to the class, unless a __new__ in the class's hierarchy,
    a library base, or a decorator or metaclass may hand out an instance in
    use (a singleton). In __new__, __init_subclass__
    and __class_getitem__, cls is the class.
  • A class whose body binds only constants and methods was taken to hold
    no state even when a decorator, a metaclass or a library base may give
    it some (a decorator keeping a singleton or a registry on the class),
    so changes made through its instances reached none of its readers. Such
    a class now counts as holding state, unless the decorator or metaclass
    is one that adds no state (dataclasses.dataclass,
    functools.total_ordering, typing.final, abc.ABCMeta).
  • A module constant that cannot change in place (a string, a number, a
    tuple of such, re.compile(...), textwrap.dedent(...)) and that no
    code binds again gained a writer from every function that passed it to
    a call or used what its methods return (SQL handed to
    conn.executescript, PATTERN.match(s)). Such false writers made every
    reader of the constant depend on unrelated code. Binding the name again
    (global, module.NAME = ..., setattr) still counts.
  • The same holds for a class attribute your class body binds once to
    such a value (DAYS = "days", _MONTHS = 12) and that no code binds
    again: handing TimeDelta.DAYS to a function, or m = min(m, self._MONTHS), is no longer a change to the class, and neither is
    calling a method on an item of a class-level table of strings and
    numbers that nothing changes (self.timeframes[k].format(n)). When
    such a table can change, a method called on its items counts as a
    change.
  • An object handed to a constructor of your class (Tokenizer(source, rules=RULES)) is part of the new instance: changing the instance in
    place (tok.rules["x"] = 1, a method changing what __init__ stored)
    makes that code a dependency of the readers of RULES. Setting or
    counting up the instance's own attributes (tok.position += 1) does
    not, unless the class can run code when they are set (__new__,
    __setattr__, __delattr__, __getattr__, __getattribute__, a
    class attribute of that name, a decorator, a metaclass or a library
    base) or the attribute is a special one (__dict__).
  • A write onto the class of an object whose type is not known
    (type(obj).x = v, obj.__class__.items.append(v), setattr(type(obj), name, v), or type(obj) handed to a function that sets c.x) only
    counted as a change to the object. It now makes the writer a dependency
    of the readers of every class that may have that attribute: the classes
    that define or set it, classes deriving from a library class, and their
    subclasses (every class, for a special attribute, a computed name or an
    attribute no class sets). A mock's class is its own.
  • State held on a class was followed only through the class's own name and
    the attributes its body binds. Now:
    • a class reached by another name is written through it: an alias, a
      dict, tuple or loop over classes, a parameter default, getattr(module, "C"), import_module, sys.modules, vars(module), type.__setattr__,
      an item of __mro__ or __bases__, or the result of a call given the
      class (HANDLERS.get(kind, Default).calls.append(v); a mock made with
      the class as its spec is not the class); calling an alias,
      functools.partial(C) or a class a call hands back
      (getattr(module, "C")()) makes an instance;
    • an attribute a class is given anywhere (its module's top-level code,
      another module, a decorator, a metaclass, setattr) is class state,
      shared by its instances;
    • a nested class's state, staticmethod, classmethod or property of
      something other than a function, and an attribute __init__ binds to
      an object that may be shared (a module variable, a class attribute, a
      mutable default, self.__dict__ = shared) are followed;
    • register on an abstract base class, other library methods called on
      a class of yours with a library base, decorator or metaclass
      (addClassCleanup, model_rebuild), and type(name, bases, ns) with
      a base of yours, write the class.
  • A project function that writes module state through an object it is
    handed or called on (SETTINGS.configure(debug=1) writing
    self.opts) is a writer of that state itself: called from a test, the
    write was left to the test, which planning assumes isolated, so a change
    to the function reached no reader.
  • An object is taken for new only when it surely is. A memoised
    function's result (functools.cache, lru_cache, a memoiser of yours),
    a deep copy or pickle round trip (which may hand back a class, a function,
    an enum member or the object itself), a copy method that may return its
    object, the result of a lambda, closure or functools.partial over a
    module container, and sum, abs, round, pow and divmod (which
    may return what they are handed) may be shared state.
  • A lazily initialised module value that is then changed in place
    (if _STATE is None: _STATE = {} followed by _STATE["mode"] = m) is no
    constant: its readers depend on the code that fills it.
  • Project code that stores onto a library module (json.mode = m,
    setattr, vars() or __dict__, a change in place of what a library
    attribute holds) or into os.environ is a dependency of the code that
    reads it there (os.getenv included). Library code and child processes
    reading the environment are not followed.
  • A write inside a lambda (_add = lambda v: _ITEMS.append(v)) is a
    write of whoever calls it.
  • A project module that writes process state when it is imported was
    taken for test code, so its writes were assumed isolated.
  • pytest.importorskip("pkg.mod"), importlib.util.find_spec,
    pkgutil.resolve_name, pydoc.locate and pkgutil.iter_modules or
    walk_packages over a package were not imports: a change that made the
    module fail to import, or changed what it does when imported, did not
    select the test. They are now.
  • Discovery missed fixtures a test requests outside its own signature:
    request handed to a helper, an alias of request.getfixturevalue,
    lf()/lazy_fixture() in a parametrization, a hook adding to
    item.fixturenames or to the usefixtures option; an autouse fixture
    under another name for the decorator (from pytest import fixture as fx); and a module stored into doctest_namespace by subscript
    (doctest_namespace["pkg"] = pkg). They are dependencies now.
  • The run action planned only the last commit (HEAD^1): a push of
    several commits, a merge-queue group or a pull request checked out at
    its head planned part of the change. It plans the event's own change
    now (a pull request on top of its target branch, every commit of a
    push, the whole merge-queue group), and a branch name, tag or HEAD~n
    as base works in a shallow checkout. With no base (a new branch, a
    manual run) or an event base that cannot be fetched, it runs every test
    and says why.
  • The record action's check counted any failed plan as an unusable
    recording and passed; it fails now when the plan fails without the
    recording too.
  • The run and record actions deepen a shallow checkout with an
    absolute depth. They used git fetch --deepen, which GitHub serves only
    from a commit a branch or pull request still points at: a re-run, or a
    run whose pull request had moved on, checked out a merge commit since
    replaced, the base never arrived and the job failed with no plan (and
    blamed the recording). A base still missing is no longer reported as an
    unusable recording.
  • diffcone report (and the report action) kept every plan it read
    whole. A plan made from a recording explains each target, tens of
    megabytes for a large suite, so a day of such plans could exhaust the
    runner's memory and stop the report. It keeps only what it reads.
  • On Windows, the recorder no longer treats a system query (locale -a,
    git rev-parse and the other read-only git queries, ps, uname) as
    a process it cannot follow. It did, so a recording of a suite that runs
    one outside every test (locale tests that query at import, pytest-benchmark
    in every run) planned every change as select-everything there. POSIX
    was not affected.
  • A test runner called from project code with a directory of non-Python
    files (pytest.main(["--verbose", "docs"]) over doctest text files),
    a word after an option diffcone does not know, or no path at all, is
    taken to run anything. The directory was taken for the option's
    argument and its tests were never selected.
  • The module cache was written but never read when the cache directory
    was relative (the default --repo ., and every action): planning again
    re-read every file.
  • A cached index kept no decorator symbols, so the same plan run twice could
    select more the second time; a cached module lost whether a method's
    receiver is its class, and a change in a base class's own bases did not
    refresh cached resolution of its subclasses. The index cache format
    changes: the first plan after upgrading rebuilds it.