#include #include #include #include #include #include #include #include #include #include #include #include #include namespace { struct TempDirGuard { std::filesystem::path dir; explicit TempDirGuard(std::string_view suffix) : dir(std::filesystem::temp_directory_path() / suffix) { std::filesystem::remove_all(dir); std::filesystem::create_directories(dir); } ~TempDirGuard() { std::error_code ec; std::filesystem::remove_all(dir, ec); } }; } // namespace Void::Testing::Test testing_failure_capture_and_bounded_assertions() { using namespace Void::Testing; // FailureCapture catches expect and require aborts locally without failing the outer test { FailureCapture cap; const bool ok = cap.run([] { expect_eq(1, 2); require(false); }); expect(!ok); expect(cap.failed()); expect(cap.aborted()); expect_eq(cap.assertions().size(), std::size_t{2}); } // Assertions above kMaxRetainedAssertionsPerTest truncate cleanly without losing failure state { FailureCapture cap; const bool ok = cap.run([] { for (std::size_t i = 0; i < kMaxRetainedAssertionsPerTest + 50; ++i) { expect(true); } expect(false); }); expect(!ok); expect(cap.failed()); expect(cap.truncated()); expect_eq(cap.assertions().size(), kMaxRetainedAssertionsPerTest); expect_eq(cap.dropped_assertions(), std::size_t{51}); } } Void::Testing::Test testing_runner_counts_failure_after_retention_cap() { using namespace Void::Testing; // A failure recorded after kMaxRetainedAssertionsPerTest is dropped from the // bounded assertion report, but it must still fail the executed test. TestCase tc{ .run = [] { for (std::size_t i = 0; i < kMaxRetainedAssertionsPerTest; ++i) { expect(true); } expect(false); }, .name = "failure_after_retention_cap", .suite = "HardeningRunner", }; const TestResult res = Runner::instance().execute_case(tc); expect(res.state == TestState::Failed); expect(res.assertions_truncated); expect_eq(res.assertions.size(), kMaxRetainedAssertionsPerTest); expect_eq(res.dropped_assertions, std::size_t{1}); } Void::Testing::Test testing_fixture_single_teardown_and_cleanup_error() { using namespace Void::Testing; struct State { int setup_count{0}; int run_count{0}; int teardown_count{0}; int destroy_count{0}; }; // Case 1: body succeeds, teardown throws -> teardown runs once, destroy runs once, Failed + cleanup_error { State st; TestCase tc{ .run = [&] { ++st.run_count; }, .name = "teardown_throws", .suite = "HardeningFixture", .fixture_create = [&]() -> void* { return &st; }, .fixture_destroy = [](void* p) { ++static_cast(p)->destroy_count; }, .fixture_setup = [](void* p) { ++static_cast(p)->setup_count; }, .fixture_teardown = [](void* p) { ++static_cast(p)->teardown_count; throw std::runtime_error{"teardown boom"}; }, }; const TestResult res = Runner::instance().execute_case(tc); expect(res.state == TestState::Failed); expect_eq(st.setup_count, 1); expect_eq(st.run_count, 1); expect_eq(st.teardown_count, 1); expect_eq(st.destroy_count, 1); expect(res.cleanup_error.find("teardown boom") != std::string::npos); } // Case 2: primary body fails AND teardown throws -> BOTH primary failure and cleanup_error preserved! { State st; TestCase tc{ .run = [&] { ++st.run_count; throw std::runtime_error{"body boom"}; }, .name = "both_throw", .suite = "HardeningFixture", .fixture_create = [&]() -> void* { return &st; }, .fixture_destroy = [](void* p) { ++static_cast(p)->destroy_count; }, .fixture_setup = [](void* p) { ++static_cast(p)->setup_count; }, .fixture_teardown = [](void* p) { ++static_cast(p)->teardown_count; throw std::runtime_error{"cleanup boom"}; }, }; const TestResult res = Runner::instance().execute_case(tc); expect(res.state == TestState::Failed); expect_eq(st.teardown_count, 1); expect_eq(st.destroy_count, 1); expect(res.crash_info.find("body boom") != std::string::npos); expect(res.cleanup_error.find("cleanup boom") != std::string::npos); } } Void::Testing::Test testing_junit_distinct_files_and_config_validation() { using namespace Void::Testing; TempDirGuard tmp{"void_testing_junit_check"}; const auto file1 = tmp.dir / "run1.xml"; const auto file2 = tmp.dir / "run2.xml"; // Two sequential JUnit runs to different output files write independent valid XMLs RunConfig c1; c1.filter_pattern = "^broken_feature$"; c1.reporter = "junit"; c1.output_file = file1.string(); const RunResult r1 = Runner::instance().run(c1); expect(r1.config_error.empty()); expect_eq(r1.skipped, 1); RunConfig c2; c2.filter_pattern = "^unquoted_skip$"; c2.reporter = "junit"; c2.output_file = file2.string(); const RunResult r2 = Runner::instance().run(c2); expect(r2.config_error.empty()); expect_eq(r2.skipped, 1); auto read_all = [](const std::filesystem::path& p) { std::ifstream f{p}; return std::string{std::istreambuf_iterator{f}, std::istreambuf_iterator{}}; }; const std::string xml1 = read_all(file1); const std::string xml2 = read_all(file2); expect(xml1.find("broken_feature") != std::string::npos); expect(xml1.find("unquoted_skip") == std::string::npos); expect(xml2.find("unquoted_skip") != std::string::npos); expect(xml2.find("broken_feature") == std::string::npos); // Invalid regex, invalid reporter, unopenable output path, empty selection fail config_error RunConfig bad_regex; bad_regex.filter_pattern = "[unclosed"; expect(!Runner::instance().run(bad_regex).config_error.empty()); RunConfig bad_rep; bad_rep.reporter = "nonexistent"; expect(!Runner::instance().run(bad_rep).config_error.empty()); RunConfig bad_out; bad_out.filter_pattern = "^broken_feature$"; bad_out.reporter = "junit"; bad_out.output_file = (tmp.dir / "missing_dir" / "out.xml").string(); expect(!Runner::instance().run(bad_out).config_error.empty()); RunConfig empty_sel; empty_sel.filter_pattern = "^definitely_no_such_test_name_12345$"; expect(!Runner::instance().run(empty_sel).config_error.empty()); empty_sel.allow_empty = true; empty_sel.reporter = "junit"; empty_sel.output_file = (tmp.dir / "empty.xml").string(); const RunResult allowed = Runner::instance().run(empty_sel); expect(allowed.config_error.empty()); expect_eq(allowed.total, 0); expect(read_all(tmp.dir / "empty.xml").find(" args{"--filter", "^broken_feature$", "--no-color"}; const auto res = run_isolated_subprocess(exe, args); expect(res.outcome == ProcessOutcome::passed); expect_eq(res.exit_code, 0); } // 2. Empty selection fails cleanly with non-zero exit code 2 { const std::vector args{ "--filter", "^definitely_no_such_test_xyz$", "--no-color"}; const auto res = run_isolated_subprocess(exe, args); expect(res.outcome == ProcessOutcome::failed); expect_eq(res.exit_code, 2); } // 3. --allow-empty turns explicit empty selection into clean exit 0 { const std::vector args{ "--filter", "^definitely_no_such_test_xyz$", "--allow-empty", "--no-color"}; const auto res = run_isolated_subprocess(exe, args); expect(res.outcome == ProcessOutcome::passed); expect_eq(res.exit_code, 0); } // 4. Child abort isolated cleanly as crashed (SIGABRT) { const std::vector args{"--selftest-child", "abort"}; const auto res = run_isolated_subprocess(exe, args); expect(res.outcome == ProcessOutcome::crashed); expect_eq(res.term_signal, SIGABRT); } // 5. Child hang terminated on deadline cleanly { const std::vector args{"--selftest-child", "hang"}; const auto res = run_isolated_subprocess( exe, args, ProcessTestOptions{.timeout = std::chrono::milliseconds{150}}); expect(res.outcome == ProcessOutcome::timed_out); } // 6. Concurrent stdout+stderr flood drains completely with bounded truncation & zero deadlock { const std::vector args{"--selftest-child", "flood"}; const auto res = run_isolated_subprocess( exe, args, ProcessTestOptions{ .timeout = std::chrono::milliseconds{5000}, .max_output_bytes = 4096, }); expect(res.outcome == ProcessOutcome::passed); expect(res.stdout_truncated); expect(res.stderr_truncated); expect_eq(res.stdout_captured.size(), std::size_t{4096}); expect_eq(res.stderr_captured.size(), std::size_t{4096}); } }