catch3
Progmasoft Catch3: an independently maintained C++17 test framework with a parallel runner, property checks, snapshots and structured results.
Clonegit clone https://git.progmasoft.com/catch3
Define what an incremental run treats as inputs
- Commit
ced5a9e4d67240409beee683e321c74512eb2578- Author
- Leitwolf11 ·
- Parents
16cf55a45b4f
Changes
docs/command-line.md | 2 + docs/progmasoft-roadmap.md | 4 + docs/progmasoft-runner.md | 44 ++++++++- extras/catch_amalgamated.cpp | 163 ++++++++++++++++++++++++++++++- src/Progmasoft/Runner/Files.cpp | 76 ++++++++++++++ src/Progmasoft/Runner/Files.hpp | 12 +++ src/Progmasoft/Runner/Options.cpp | 22 +++++ src/Progmasoft/Runner/Options.hpp | 5 + src/Progmasoft/Runner/Planner.cpp | 10 +- src/Progmasoft/Runner/Planner.hpp | 3 + src/Progmasoft/Runner/ProjectConfig.cpp | 23 +++++ src/Progmasoft/Runner/Runner.cpp | 12 +++ tests/Progmasoft/RunnerConfig.tests.cpp | 21 ++++ tests/Progmasoft/RunnerPlanner.tests.cpp | 116 ++++++++++++++++++++++ 14 files changed, 509 insertions(+), 4 deletions(-) diff --git a/docs/command-line.md b/docs/command-line.md index ccca35ff..9b8080c3 100644 --- a/docs/command-line.md +++ b/docs/command-line.md @@ -59,6 +59,8 @@ Without a spec, every test runs except hidden ones, whose tags start with | `--select=WHICH` | `all` (default), `failed`, `changed`, `new` or `smart`; see [choosing tests](progmasoft-runner.md#choosing-and-ordering-tests) | | `--changes-since=REVISION` | the Git revision `changed` and `smart` compare with (default `HEAD`) | | `--incremental` | skip tests that passed last time and whose inputs did not change | +| `--incremental-input=PATH` | a file or directory the tests read; `--incremental` reruns every test when its contents change (repeatable) | +| `--incremental-env=NAME` | a variable the tests read; `--incremental` reruns every test when its value changes (repeatable) | | `--shard=INDEX/COUNT` | run one of COUNT shards, counting from 0 | | `--shard-durations=FILE` | balance shards by the durations in a JSON report or a history | | `-#`, `--filenames-as-tags` | tag every test with `#` and the name of its file, as `[#parser.tests]` | diff --git a/docs/progmasoft-roadmap.md b/docs/progmasoft-roadmap.md index 1f61be79..6bef72bc 100644 --- a/docs/progmasoft-roadmap.md +++ b/docs/progmasoft-roadmap.md @@ -41,6 +41,10 @@ Catch3 runner replaced Catch2's in every test binary: found by bisection; - a history of verdicts and durations drives quarantine, adaptive time limits, predictive ordering, incremental runs, replay and analytics; + an incremental run fingerprints the whole binary with the files and + variables declared as inputs; next is a fingerprint per test, from the + code and the files that test reaches, so that a change reruns only the + tests it touches; - tests are selected by change, by past failure or by prediction, and sharded by a stable hash or by duration; - tags declare dependencies, shared resources, serial tests, time limits and diff --git a/docs/progmasoft-runner.md b/docs/progmasoft-runner.md index d61ebadd..40e7a7ae 100644 --- a/docs/progmasoft-runner.md +++ b/docs/progmasoft-runner.md @@ -126,7 +126,8 @@ The history drives several features: * **Replay.** `--replay` runs the last failures again, each with the seed it failed with and after the same tests, in a fresh process. * **Incremental runs.** `--incremental` skips tests that passed last time - with the same fingerprint; they are reported as up to date. + with the same fingerprint; they are reported as up to date. See + [What an incremental run treats as inputs](#what-an-incremental-run-treats-as-inputs). * **Analytics.** `--analytics` prints what the history says: flaky tests, tests in quarantine, the tests that fail most often, the slowest tests and the tests getting slower. `--analytics=json` prints it as JSON. @@ -134,6 +135,44 @@ The history drives several features: Under `bazel test` there is no history unless `CATCH3_HISTORY` names a directory that outlives the sandbox. +### What an incremental run treats as inputs + +An incremental run keeps the result of a test only when all of these hold: + +* the test passed in its last run, was not flaky, and is not in quarantine + (unless `--quarantine=off`); +* `--replay` is not given; +* its fingerprint equals the one of that run. + +The fingerprint of a test is a SHA-256 over: + +* the test binary, byte for byte. The binary holds the test, Catch3 itself and + everything linked statically, so a change of code, of a compiler flag or of + the Catch3 version changes every fingerprint of the binary; +* the name of the test; +* the contents of every file named with `--incremental-input=PATH`, a file or + a directory whose regular files are read recursively; a missing path and an + unreadable file count as values of their own; +* the values of the variables named with `--incremental-env=NAME` and of those + handed over with `--pass-env=NAME`; an unset variable counts as a value of + its own. + +Everything else is outside the fingerprint, and a test that depends on it +must declare it or run without `--incremental`: files a test reads that are +not declared, shared libraries loaded at run time, other variables of the +environment, the working directory, the time, the network and other +processes. A fingerprint covers the whole binary rather than the code a test +reaches, so any change to the binary reruns all of its tests; finer, +per-test dependencies are on the [roadmap](progmasoft-roadmap.md#top). + +```sh +./tests --incremental --incremental-input=tests/data --incremental-env=TZ +``` + +In the project file the same inputs are `"incremental-input"`, paths relative +to the file, and `"incremental-env"`. A history written without declared +inputs keeps its fingerprints, so naming no input reruns nothing. + ## Importing the results of other frameworks @@ -452,7 +491,8 @@ as on the command line, with the key named in the message: | a whole number or a string | `retries` (`"auto"`, `"off"`), `seed` (`"random"`) | | a number | `timeout` (seconds), `benchmark-confidence-interval` | | a path, relative to the file | `junit`, `json`, `trace`, `shard-durations`; `history`, or `false` for none | -| an array of strings | `tests` (test specs), `warn`, `pass-env` | +| an array of paths, each relative to the file | `incremental-input` | +| an array of strings | `tests` (test specs), `warn`, `pass-env`, `incremental-env` | | `"quiet"`, `"normal"` or `"verbose"` | `verbosity` | `"version": 1` is required; `"$schema"` may name a schema for editors. diff --git a/extras/catch_amalgamated.cpp b/extras/catch_amalgamated.cpp index e09c8a56..d749a275 100644 --- a/extras/catch_amalgamated.cpp +++ b/extras/catch_amalgamated.cpp @@ -1076,6 +1076,11 @@ namespace Progmasoft::Catch3::Runner Selection selection = Selection::kAll; std::string changesSince; bool incremental = false; + /// Files and directories the tests read: an incremental run keeps + /// a result only while their contents are unchanged. + std::vector<std::string> incrementalInputs; + /// Variables whose values an incremental run treats as inputs. + std::vector<std::string> incrementalEnvironment; Shard shard; std::string shardDurations; bool filenamesAsTags = false; @@ -1234,6 +1239,9 @@ namespace Progmasoft::Catch3::Runner const Changes *changes = nullptr; /// A digest of the test binary. std::string binaryFingerprint; + /// A digest of the declared inputs, DeclaredInputsDigest; empty when + /// none are declared. + std::string inputsFingerprint; }; [[nodiscard]] Expected<Plan> @@ -1485,6 +1493,7 @@ namespace Progmasoft::Catch3::Runner #include <filesystem> #include <string> #include <string_view> +#include <vector> namespace Progmasoft::Catch3::Runner { @@ -1532,6 +1541,17 @@ namespace Progmasoft::Catch3::Runner /// A name for a temporary file that no other process or thread uses. [[nodiscard]] std::string UniqueName(); + + /// A digest of the inputs of the tests besides their binary: the + /// contents of every regular file under `paths`, a file or a directory + /// each, and the values of the variables `names`. A missing path, an + /// unreadable file and an unset variable each count as a value of their + /// own, so that a change between any two states changes the digest. + /// Empty when both lists are, so that fingerprints without declared + /// inputs stay as they were. + [[nodiscard]] std::string + DeclaredInputsDigest(std::vector<std::string> paths, + std::vector<std::string> names); } // namespace Progmasoft::Catch3::Runner // Copyright (c) 2026 Progmasoft. // SPDX-License-Identifier: LGPL-3.0-or-later @@ -12228,6 +12248,7 @@ namespace Progmasoft::Catch3::Runner # include <unistd.h> #endif +#include <algorithm> #include <atomic> #include <cstdio> #include <fstream> @@ -12414,6 +12435,81 @@ namespace Progmasoft::Catch3::Runner << '-' << std::this_thread::get_id() << '-' << ++counter; return ToHex(Fnv1a64(text.str())); } + + std::string + DeclaredInputsDigest(std::vector<std::string> paths, + std::vector<std::string> names) + { + if (paths.empty() && names.empty()) + { + return {}; + } + std::sort(paths.begin(), paths.end()); + paths.erase(std::unique(paths.begin(), paths.end()), paths.end()); + std::sort(names.begin(), names.end()); + names.erase(std::unique(names.begin(), names.end()), names.end()); + + Sha256 digest; + const auto file = [&digest](const std::string &label, + const std::filesystem::path &path) { + const auto contents = Sha256File(path); + digest.Update("file\n" + label + "\n" + + (contents ? *contents : std::string{ "unreadable" }) + + "\n"); + }; + for (const std::string &path : paths) + { + std::error_code error; + const std::filesystem::path root{ path }; + if (std::filesystem::is_regular_file(root, error)) + { + file(path, root); + continue; + } + if (!std::filesystem::is_directory(root, error)) + { + digest.Update("missing\n" + path + "\n"); + continue; + } + // Every regular file below the directory, in a fixed order, by + // its path relative to the directory. Links to directories are + // not followed, so a loop cannot make the walk endless. + std::vector<std::filesystem::path> found; + std::filesystem::recursive_directory_iterator walk{ + root, + std::filesystem::directory_options::skip_permission_denied, + error + }; + for (const std::filesystem::recursive_directory_iterator end; + !error && walk != end; + walk.increment(error)) + { + if (walk->is_regular_file(error)) + { + found.push_back(walk->path()); + } + } + std::sort(found.begin(), found.end()); + digest.Update("directory\n" + path + "\n"); + for (const auto &entry : found) + { + file(entry.lexically_relative(root).generic_string(), entry); + } + if (error) + { + digest.Update("incomplete\n"); + } + } + for (const std::string &name : names) + { + digest.Update(HasEnvironment(name.c_str()) + ? "variable\n" + name + "\n" + + Sha256Hex(ReadEnvironment(name.c_str())) + + "\n" + : "unset\n" + name + "\n"); + } + return digest.HexDigest(); + } } // namespace Progmasoft::Catch3::Runner // Copyright (c) 2026 Progmasoft. // SPDX-License-Identifier: LGPL-3.0-or-later @@ -14088,6 +14184,28 @@ namespace Progmasoft::Catch3::Runner options.incremental = true; return Status::Ok(); }}, + {"incremental-input", + '\0', + Takes::kValue, + "PATH", + "Selecting tests", + "a file or directory the tests read; --incremental reruns " + "every test when its contents change (repeatable)", + [](Options &options, const std::optional<std::string> &value) { + options.incrementalInputs.push_back(*value); + return Status::Ok(); + }}, + {"incremental-env", + '\0', + Takes::kValue, + "NAME", + "Selecting tests", + "a variable the tests read; --incremental reruns every test " + "when its value changes (repeatable)", + [](Options &options, const std::optional<std::string> &value) { + options.incrementalEnvironment.push_back(*value); + return Status::Ok(); + }}, {"shard", '\0', Takes::kValue, @@ -15471,8 +15589,16 @@ namespace Progmasoft::Catch3::Runner continue; } const TestEntry &entry = tests[index]; + // The binary holds the test, Catch3 and everything linked + // statically; declared files and variables come next. Without + // declared inputs the fingerprint is the one of earlier releases, + // so their history keeps its results. const std::string fingerprint = - Sha256Hex(inputs.binaryFingerprint + "\n" + entry.key); + inputs.inputsFingerprint.empty() + ? Sha256Hex(inputs.binaryFingerprint + "\n" + entry.key) + : Sha256Hex(inputs.binaryFingerprint + "\n" + + inputs.inputsFingerprint + "\n" + + entry.key); plan.fingerprints[entry.key] = fingerprint; if (!options.incremental || options.replayFailures) { @@ -17117,6 +17243,9 @@ namespace Progmasoft::Catch3::Runner kNumber, /// An array of strings, each given to the option. kWords, + /// An array of files or directories, each relative to the + /// file's directory and given to the option. + kPaths, /// A directory, or false for none. kPathOrFalse, /// "quiet", "normal" or "verbose". @@ -17142,6 +17271,8 @@ namespace Progmasoft::Catch3::Runner { "select", ValueKind::kWord }, { "changes-since", ValueKind::kWord }, { "incremental", ValueKind::kFlag }, + { "incremental-input", ValueKind::kPaths }, + { "incremental-env", ValueKind::kWords }, { "shard-durations", ValueKind::kPath }, { "filenames-as-tags", ValueKind::kFlag }, // Running. @@ -17395,6 +17526,24 @@ namespace Progmasoft::Catch3::Runner } return arguments; } + case ValueKind::kPaths: + { + if (!value.IsArray()) + { + return wrong("an array of paths"); + } + std::vector<std::string> arguments; + for (const auto &item : value.Items()) + { + if (!item.IsString() || item.AsString().empty()) + { + return wrong("an array of paths"); + } + arguments.push_back( + option + "=" + PathText(item.AsString(), directory)); + } + return arguments; + } case ValueKind::kPathOrFalse: if (value.GetKind() == JsonValue::Kind::kBoolean && !value.AsBoolean()) @@ -20411,6 +20560,18 @@ namespace Progmasoft::Catch3 inputs.history = options.historyEnabled ? &history : nullptr; inputs.changes = changes ? &*changes : nullptr; inputs.binaryFingerprint = binaryFingerprint; + if (options.historyEnabled || options.incremental) + { + // The variables handed to the tests on purpose are inputs + // whether or not they were declared. + std::vector<std::string> variables = + options.incrementalEnvironment; + variables.insert(variables.end(), + options.passedEnvironment.begin(), + options.passedEnvironment.end()); + inputs.inputsFingerprint = Runner::DeclaredInputsDigest( + options.incrementalInputs, std::move(variables)); + } auto plan = Runner::MakePlan(options, inputs); if (!plan) { diff --git a/src/Progmasoft/Runner/Files.cpp b/src/Progmasoft/Runner/Files.cpp index 94b720c9..096b959a 100644 --- a/src/Progmasoft/Runner/Files.cpp +++ b/src/Progmasoft/Runner/Files.cpp @@ -16,6 +16,7 @@ # include <unistd.h> #endif +#include <algorithm> #include <atomic> #include <cstdio> #include <fstream> @@ -202,4 +203,79 @@ namespace Progmasoft::Catch3::Runner << '-' << std::this_thread::get_id() << '-' << ++counter; return ToHex(Fnv1a64(text.str())); } + + std::string + DeclaredInputsDigest(std::vector<std::string> paths, + std::vector<std::string> names) + { + if (paths.empty() && names.empty()) + { + return {}; + } + std::sort(paths.begin(), paths.end()); + paths.erase(std::unique(paths.begin(), paths.end()), paths.end()); + std::sort(names.begin(), names.end()); + names.erase(std::unique(names.begin(), names.end()), names.end()); + + Sha256 digest; + const auto file = [&digest](const std::string &label, + const std::filesystem::path &path) { + const auto contents = Sha256File(path); + digest.Update("file\n" + label + "\n" + + (contents ? *contents : std::string{ "unreadable" }) + + "\n"); + }; + for (const std::string &path : paths) + { + std::error_code error; + const std::filesystem::path root{ path }; + if (std::filesystem::is_regular_file(root, error)) + { + file(path, root); + continue; + } + if (!std::filesystem::is_directory(root, error)) + { + digest.Update("missing\n" + path + "\n"); + continue; + } + // Every regular file below the directory, in a fixed order, by + // its path relative to the directory. Links to directories are + // not followed, so a loop cannot make the walk endless. + std::vector<std::filesystem::path> found; + std::filesystem::recursive_directory_iterator walk{ + root, + std::filesystem::directory_options::skip_permission_denied, + error + }; + for (const std::filesystem::recursive_directory_iterator end; + !error && walk != end; + walk.increment(error)) + { + if (walk->is_regular_file(error)) + { + found.push_back(walk->path()); + } + } + std::sort(found.begin(), found.end()); + digest.Update("directory\n" + path + "\n"); + for (const auto &entry : found) + { + file(entry.lexically_relative(root).generic_string(), entry); + } + if (error) + { + digest.Update("incomplete\n"); + } + } + for (const std::string &name : names) + { + digest.Update(HasEnvironment(name.c_str()) + ? "variable\n" + name + "\n" + + Sha256Hex(ReadEnvironment(name.c_str())) + + "\n" + : "unset\n" + name + "\n"); + } + return digest.HexDigest(); + } } // namespace Progmasoft::Catch3::Runner diff --git a/src/Progmasoft/Runner/Files.hpp b/src/Progmasoft/Runner/Files.hpp index 0725e3a1..e9a88987 100644 --- a/src/Progmasoft/Runner/Files.hpp +++ b/src/Progmasoft/Runner/Files.hpp @@ -8,6 +8,7 @@ #include <filesystem> #include <string> #include <string_view> +#include <vector> namespace Progmasoft::Catch3::Runner { @@ -55,4 +56,15 @@ namespace Progmasoft::Catch3::Runner /// A name for a temporary file that no other process or thread uses. [[nodiscard]] std::string UniqueName(); + + /// A digest of the inputs of the tests besides their binary: the + /// contents of every regular file under `paths`, a file or a directory + /// each, and the values of the variables `names`. A missing path, an + /// unreadable file and an unset variable each count as a value of their + /// own, so that a change between any two states changes the digest. + /// Empty when both lists are, so that fingerprints without declared + /// inputs stay as they were. + [[nodiscard]] std::string + DeclaredInputsDigest(std::vector<std::string> paths, + std::vector<std::string> names); } // namespace Progmasoft::Catch3::Runner diff --git a/src/Progmasoft/Runner/Options.cpp b/src/Progmasoft/Runner/Options.cpp index a260a725..2cfc4af2 100644 --- a/src/Progmasoft/Runner/Options.cpp +++ b/src/Progmasoft/Runner/Options.cpp @@ -281,6 +281,28 @@ namespace Progmasoft::Catch3::Runner options.incremental = true; return Status::Ok(); }}, + {"incremental-input", + '\0', + Takes::kValue, + "PATH", + "Selecting tests", + "a file or directory the tests read; --incremental reruns " + "every test when its contents change (repeatable)", + [](Options &options, const std::optional<std::string> &value) { + options.incrementalInputs.push_back(*value); + return Status::Ok(); + }}, + {"incremental-env", + '\0', + Takes::kValue, + "NAME", + "Selecting tests", + "a variable the tests read; --incremental reruns every test " + "when its value changes (repeatable)", + [](Options &options, const std::optional<std::string> &value) { + options.incrementalEnvironment.push_back(*value); + return Status::Ok(); + }}, {"shard", '\0', Takes::kValue, diff --git a/src/Progmasoft/Runner/Options.hpp b/src/Progmasoft/Runner/Options.hpp index f9abe9e1..56a3a1e3 100644 --- a/src/Progmasoft/Runner/Options.hpp +++ b/src/Progmasoft/Runner/Options.hpp @@ -130,6 +130,11 @@ namespace Progmasoft::Catch3::Runner Selection selection = Selection::kAll; std::string changesSince; bool incremental = false; + /// Files and directories the tests read: an incremental run keeps + /// a result only while their contents are unchanged. + std::vector<std::string> incrementalInputs; + /// Variables whose values an incremental run treats as inputs. + std::vector<std::string> incrementalEnvironment; Shard shard; std::string shardDurations; bool filenamesAsTags = false; diff --git a/src/Progmasoft/Runner/Planner.cpp b/src/Progmasoft/Runner/Planner.cpp index d66e39af..0b5c280c 100644 --- a/src/Progmasoft/Runner/Planner.cpp +++ b/src/Progmasoft/Runner/Planner.cpp @@ -419,8 +419,16 @@ namespace Progmasoft::Catch3::Runner continue; } const TestEntry &entry = tests[index]; + // The binary holds the test, Catch3 and everything linked + // statically; declared files and variables come next. Without + // declared inputs the fingerprint is the one of earlier releases, + // so their history keeps its results. const std::string fingerprint = - Sha256Hex(inputs.binaryFingerprint + "\n" + entry.key); + inputs.inputsFingerprint.empty() + ? Sha256Hex(inputs.binaryFingerprint + "\n" + entry.key) + : Sha256Hex(inputs.binaryFingerprint + "\n" + + inputs.inputsFingerprint + "\n" + + entry.key); plan.fingerprints[entry.key] = fingerprint; if (!options.incremental || options.replayFailures) { diff --git a/src/Progmasoft/Runner/Planner.hpp b/src/Progmasoft/Runner/Planner.hpp index b7d81309..623ddc34 100644 --- a/src/Progmasoft/Runner/Planner.hpp +++ b/src/Progmasoft/Runner/Planner.hpp @@ -70,6 +70,9 @@ namespace Progmasoft::Catch3::Runner const Changes *changes = nullptr; /// A digest of the test binary. std::string binaryFingerprint; + /// A digest of the declared inputs, DeclaredInputsDigest; empty when + /// none are declared. + std::string inputsFingerprint; }; [[nodiscard]] Expected<Plan> diff --git a/src/Progmasoft/Runner/ProjectConfig.cpp b/src/Progmasoft/Runner/ProjectConfig.cpp index c4c4df6c..2df0c09b 100644 --- a/src/Progmasoft/Runner/ProjectConfig.cpp +++ b/src/Progmasoft/Runner/ProjectConfig.cpp @@ -37,6 +37,9 @@ namespace Progmasoft::Catch3::Runner kNumber, /// An array of strings, each given to the option. kWords, + /// An array of files or directories, each relative to the + /// file's directory and given to the option. + kPaths, /// A directory, or false for none. kPathOrFalse, /// "quiet", "normal" or "verbose". @@ -62,6 +65,8 @@ namespace Progmasoft::Catch3::Runner { "select", ValueKind::kWord }, { "changes-since", ValueKind::kWord }, { "incremental", ValueKind::kFlag }, + { "incremental-input", ValueKind::kPaths }, + { "incremental-env", ValueKind::kWords }, { "shard-durations", ValueKind::kPath }, { "filenames-as-tags", ValueKind::kFlag }, // Running. @@ -315,6 +320,24 @@ namespace Progmasoft::Catch3::Runner } return arguments; } + case ValueKind::kPaths: + { + if (!value.IsArray()) + { + return wrong("an array of paths"); + } + std::vector<std::string> arguments; + for (const auto &item : value.Items()) + { + if (!item.IsString() || item.AsString().empty()) + { + return wrong("an array of paths"); + } + arguments.push_back( + option + "=" + PathText(item.AsString(), directory)); + } + return arguments; + } case ValueKind::kPathOrFalse: if (value.GetKind() == JsonValue::Kind::kBoolean && !value.AsBoolean()) diff --git a/src/Progmasoft/Runner/Runner.cpp b/src/Progmasoft/Runner/Runner.cpp index 6a92a2ef..1bb8e69b 100644 --- a/src/Progmasoft/Runner/Runner.cpp +++ b/src/Progmasoft/Runner/Runner.cpp @@ -912,6 +912,18 @@ namespace Progmasoft::Catch3 inputs.history = options.historyEnabled ? &history : nullptr; inputs.changes = changes ? &*changes : nullptr; inputs.binaryFingerprint = binaryFingerprint; + if (options.historyEnabled || options.incremental) + { + // The variables handed to the tests on purpose are inputs + // whether or not they were declared. + std::vector<std::string> variables = + options.incrementalEnvironment; + variables.insert(variables.end(), + options.passedEnvironment.begin(), + options.passedEnvironment.end()); + inputs.inputsFingerprint = Runner::DeclaredInputsDigest( + options.incrementalInputs, std::move(variables)); + } auto plan = Runner::MakePlan(options, inputs); if (!plan) { diff --git a/tests/Progmasoft/RunnerConfig.tests.cpp b/tests/Progmasoft/RunnerConfig.tests.cpp index 401b6e9a..4d201148 100644 --- a/tests/Progmasoft/RunnerConfig.tests.cpp +++ b/tests/Progmasoft/RunnerConfig.tests.cpp @@ -423,3 +423,24 @@ TEST_CASE("--config names the file, and some modes read none", Runner::ParseCommandLine({ "--no-config", "--config=broken.json" }, scratch.Root())); } + +TEST_CASE("Declared inputs of incremental runs come from the file", + "[progmasoft][runner][config]") +{ + const fs::path directory = fs::path{ "project" } / "tests"; + const auto options = Apply(R"({ + "version": 1, + "incremental": true, + "incremental-input": ["data", "../fixtures/table.csv"], + "incremental-env": ["LANG", "TZ"] + })", + directory); + CHECK(options.incremental); + REQUIRE(options.incrementalInputs.size() == 2); + CHECK(fs::path{ options.incrementalInputs[0] } + == (directory / "data").lexically_normal()); + CHECK(fs::path{ options.incrementalInputs[1] } + == fs::path{ "project/fixtures/table.csv" }.lexically_normal()); + CHECK(options.incrementalEnvironment + == std::vector<std::string>{ "LANG", "TZ" }); +} diff --git a/tests/Progmasoft/RunnerPlanner.tests.cpp b/tests/Progmasoft/RunnerPlanner.tests.cpp index 1ce707ed..c4d4592b 100644 --- a/tests/Progmasoft/RunnerPlanner.tests.cpp +++ b/tests/Progmasoft/RunnerPlanner.tests.cpp @@ -8,6 +8,7 @@ #include <Progmasoft/Runner/Catalog.hpp> #include <Progmasoft/Runner/Files.hpp> +#include <Progmasoft/Runner/Hash.hpp> #include <Progmasoft/Runner/History.hpp> #include <Progmasoft/Runner/Planner.hpp> @@ -16,6 +17,7 @@ #include <algorithm> #include <chrono> +#include <cstdlib> #include <filesystem> #include <fstream> #include <map> @@ -597,3 +599,117 @@ TEST_CASE("Replay runs the last failures the way they happened", CHECK_THAT(plan.notes.front(), ContainsSubstring("no failure")); } } + +namespace +{ + void + WriteText(const std::filesystem::path &file, const std::string &text) + { + std::ofstream stream(file, std::ios::binary | std::ios::trunc); + stream << text; + } + + void + SetVariable(const char *name, const char *value) + { +#if defined(_WIN32) + REQUIRE(_putenv_s(name, value) == 0); +#else + REQUIRE(setenv(name, value, 1) == 0); +#endif + } + + void + UnsetVariable(const char *name) + { +#if defined(_WIN32) + CHECK(_putenv_s(name, "") == 0); +#else + CHECK(unsetenv(name) == 0); +#endif + } +} // namespace + +TEST_CASE("The digest of declared inputs follows their contents", + "[progmasoft][runner][planner]") +{ + const Scratch scratch; + const auto data = scratch.Path() / "data.txt"; + const auto directory = scratch.Path() / "fixtures"; + std::filesystem::create_directories(directory / "nested"); + WriteText(data, "one"); + WriteText(directory / "nested" / "a.txt", "alpha"); + const std::string dataPath = data.string(); + const std::string directoryPath = directory.string(); + + CHECK(Runner::DeclaredInputsDigest({}, {}).empty()); + + const auto first = + Runner::DeclaredInputsDigest({dataPath, directoryPath}, {}); + CHECK(first.size() == 64); + // The order in which inputs are named does not matter. + CHECK(Runner::DeclaredInputsDigest({directoryPath, dataPath}, {}) == + first); + + WriteText(data, "two"); + const auto changedFile = + Runner::DeclaredInputsDigest({dataPath, directoryPath}, {}); + CHECK(changedFile != first); + + WriteText(directory / "nested" / "b.txt", "beta"); + const auto addedFile = + Runner::DeclaredInputsDigest({dataPath, directoryPath}, {}); + CHECK(addedFile != changedFile); + + std::filesystem::remove(data); + CHECK(Runner::DeclaredInputsDigest({dataPath, directoryPath}, {}) != + addedFile); +} + +TEST_CASE("The digest of declared inputs follows variables", + "[progmasoft][runner][planner]") +{ + const char *name = "CATCH3_TEST_INCREMENTAL_INPUT"; + UnsetVariable(name); + const auto unset = Runner::DeclaredInputsDigest({}, {name}); + CHECK_FALSE(unset.empty()); + SetVariable(name, "first"); + const auto first = Runner::DeclaredInputsDigest({}, {name}); + CHECK(first != unset); + SetVariable(name, "second"); + CHECK(Runner::DeclaredInputsDigest({}, {name}) != first); + // A variable named twice counts once. + CHECK(Runner::DeclaredInputsDigest({}, {name, name}) == + Runner::DeclaredInputsDigest({}, {name})); + UnsetVariable(name); +} + +TEST_CASE("Declared inputs join the fingerprint of every test", + "[progmasoft][runner][planner]") +{ + const auto catalog = + Runner::Catalog::FromEntries({Entry("reads"), Entry("computes")}); + Runner::PlanInputs inputs; + inputs.catalog = &catalog; + inputs.matched = All(catalog); + inputs.binaryFingerprint = "build 1"; + Runner::Options options; + options.order = Runner::Order::kDeclared; + + // Without declared inputs the fingerprint is the binary and the test. + const auto plain = PlanOf(options, inputs); + for (const auto &[key, fingerprint] : plain.fingerprints) + { + CHECK(fingerprint == Runner::Sha256Hex("build 1\n" + key)); + } + + inputs.inputsFingerprint = "inputs 1"; + const auto declared = PlanOf(options, inputs); + inputs.inputsFingerprint = "inputs 2"; + const auto changed = PlanOf(options, inputs); + for (const auto &[key, fingerprint] : plain.fingerprints) + { + CHECK(declared.fingerprints.at(key) != fingerprint); + CHECK(changed.fingerprints.at(key) != declared.fingerprints.at(key)); + } +}