Juggernaut.Coverage.Index (Juggernaut v0.4.0)

Copy Markdown View Source

The coverage index (design notes ยง10.5, _build/juggernaut/coverage/index.etf): what each probed test file hit, kept between runs so that a run probes only the test files whose coverage may have changed.

One record per test file: the file's hash, the hash of each mutated source file it was probed against (sources), the fingerprint of each source file's mutant keys it was probed for (keys), the files of the sites it hit (hit_sources), its hits by mutant key (stable across builds, unlike switches), and its clock. Labels name tests by strings (Juggernaut.Coverage.Selection.string_id/1), so reading the index creates no atoms. The index also holds the hash of the inputs every test depends on (globals: mix.lock, config/, and every file under the test paths that is not a test file, such as test_helper.exs and test support), and the keys behind each fingerprint (key_sets).

Hits are stored only under the keys of the mutants a probe ran for, so the keys matter as much as the hashes: a run over unchanged sources with mutants the index has never seen (another operator set, a Juggernaut upgrade, an earlier run over a subset of a file's mutants) probes again, or those mutants would find no labels and be reported as no_coverage.

stale/2 says what to probe again (:all, or each stale file with its reason):

  • everything when there is no index, or globals changed;
  • a test file that is new or whose content changed;
  • a test file that hit a source file whose content changed since it was probed (when the run knows its functions: in a function it ran sites of, or in any function of that file that had no mutants, added and removed ones included);
  • a test file probed before a source file was mutated (its sites were not there);
  • a test file probed for fewer of a source file's mutants than the run has (the run's keys of that file are not all among the keys it was probed for).

Limit (the same as any site-level coverage): an edit can send a test file to sites it never hit through code the index cannot see it run. A test file is kept when the edited code is in a file it never hit (a file without mutants, or one it reached only through functions without sites), or in a function with mutants whose sites it never ran (it entered through a clause with no sites). Its coverage of the sites it reaches now is then missing, so a mutant only it would kill can read as survived or no_coverage until it is probed again. A core survivor's reconfirmation (gate and release lanes) runs every test file that hit its source file, so it recovers the second case but not the first.

Summary

Types

keys: each mutated source file's mutant keys, sorted (a missing file has none). functions: each mutated source file's function hashes now; key_functions: each mutant key's function. Without the last two, staleness is judged by file.

A probed test file. hit_functions and site_functions hold, by source file, the functions (Juggernaut.Cache.Footprint.key/3) it ran sites of and the functions that had mutants when it was probed; functions the hashes of those and of every other function the file wrote then.

t()

Functions

The current hashes of the project at root: its tests (test file โ†’ hash), the mutated sources (each of source_files), and globals (mix.lock, config/ and every other file under test_paths). test_files are the test files, relative to root, as the workers load them.

The fingerprint of a sorted list of mutant keys.

The function of a mutant as the index names it (Juggernaut.Cache.Footprint.key/3).

Each mutant key's function (function_key/1), the run's key_functions.

Each source file of mutants with its mutants' keys, sorted (current's keys).

Every label of each mutant key, over the index's test files.

The index at path, or nil when it is missing, unreadable or of another format.

The record of one probed test file (Juggernaut.Coverage.Probe), its site hits put under the keys of the site's mutants: site_mutants maps each site to its mutants (%{key, file}). keys holds the fingerprint of each source file's current keys.

What to probe again (see the module doc), given the current hashes.

The index with probed (%{test_file => record}) put in, records of test files no longer in current dropped, current's globals, and the keys behind every fingerprint a record refers to (key_sets).

Writes index to path atomically.

Types

current()

@type current() :: %{
  :globals => String.t(),
  :sources => %{required(String.t()) => String.t()},
  :tests => %{required(String.t()) => String.t()},
  optional(:keys) => %{required(String.t()) => [String.t()]},
  optional(:functions) => %{
    required(String.t()) => Juggernaut.Cache.Footprint.hashes()
  },
  optional(:key_functions) => %{required(String.t()) => String.t()}
}

keys: each mutated source file's mutant keys, sorted (a missing file has none). functions: each mutated source file's function hashes now; key_functions: each mutant key's function. Without the last two, staleness is judged by file.

file_record()

@type file_record() :: %{
  :hash => String.t(),
  :sources => %{required(String.t()) => String.t()},
  :keys => %{required(String.t()) => String.t()},
  :hit_sources => [String.t()],
  :hits => %{required(String.t()) => [Juggernaut.Coverage.Selection.label()]},
  :us => non_neg_integer(),
  optional(:hit_functions) => %{required(String.t()) => [String.t()]},
  optional(:site_functions) => %{required(String.t()) => [String.t()]},
  optional(:functions) => %{
    required(String.t()) => %{required(String.t()) => String.t()}
  }
}

A probed test file. hit_functions and site_functions hold, by source file, the functions (Juggernaut.Cache.Footprint.key/3) it ran sites of and the functions that had mutants when it was probed; functions the hashes of those and of every other function the file wrote then.

t()

@type t() :: %Juggernaut.Coverage.Index{
  files: %{required(String.t()) => file_record()},
  globals: String.t() | nil,
  key_sets: %{required(String.t()) => [String.t()]}
}

Functions

current(root, test_paths, test_files, source_files)

@spec current(Path.t(), [String.t()], [String.t()], [String.t()]) :: current()

The current hashes of the project at root: its tests (test file โ†’ hash), the mutated sources (each of source_files), and globals (mix.lock, config/ and every other file under test_paths). test_files are the test files, relative to root, as the workers load them.

fingerprint(keys)

@spec fingerprint([String.t()]) :: String.t()

The fingerprint of a sorted list of mutant keys.

function_key(map)

@spec function_key(%{module: module(), function: {atom(), arity()}}) :: String.t()

The function of a mutant as the index names it (Juggernaut.Cache.Footprint.key/3).

key_functions(mutants)

@spec key_functions([map()]) :: %{required(String.t()) => String.t()}

Each mutant key's function (function_key/1), the run's key_functions.

keys_by_source(mutants)

@spec keys_by_source([%{key: String.t(), file: String.t()}]) :: %{
  required(String.t()) => [String.t()]
}

Each source file of mutants with its mutants' keys, sorted (current's keys).

labels(index)

@spec labels(t()) :: %{
  required(String.t()) => [Juggernaut.Coverage.Selection.label()]
}

Every label of each mutant key, over the index's test files.

read(path)

@spec read(Path.t()) :: t() | nil

The index at path, or nil when it is missing, unreadable or of another format.

record(file_result, file, current, site_mutants)

@spec record(map(), String.t(), current(), %{required(pos_integer()) => [map()]}) ::
  file_record()

The record of one probed test file (Juggernaut.Coverage.Probe), its site hits put under the keys of the site's mutants: site_mutants maps each site to its mutants (%{key, file}). keys holds the fingerprint of each source file's current keys.

stale(index, current)

@spec stale(t() | nil, current()) ::
  {:all, String.t()} | {:files, %{required(String.t()) => String.t()}}

What to probe again (see the module doc), given the current hashes.

update(index, current, probed)

@spec update(t() | nil, current(), %{required(String.t()) => file_record()}) :: t()

The index with probed (%{test_file => record}) put in, records of test files no longer in current dropped, current's globals, and the keys behind every fingerprint a record refers to (key_sets).

write(path, index)

@spec write(Path.t(), t()) :: :ok | {:error, Juggernaut.Error.t()}

Writes index to path atomically.