From: Joel Rosdahl Date: Sun, 31 Dec 2023 14:20:16 +0000 (+0100) Subject: test: Add test of core::rewrite_stderr_to_absolute_paths X-Git-Tag: v4.10~141 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=376dfecb414196cb496d72bdb2bc17127582d2ac;p=thirdparty%2Fccache.git test: Add test of core::rewrite_stderr_to_absolute_paths --- diff --git a/src/core/common.cpp b/src/core/common.cpp index 9efe5462a..63a214346 100644 --- a/src/core/common.cpp +++ b/src/core/common.cpp @@ -66,6 +66,19 @@ find_first_ansi_csi_seq(std::string_view string) } } +} // namespace + +namespace core { + +void +ensure_dir_exists(const fs::path& dir) +{ + if (auto result = fs::create_directories(dir); !result) { + throw core::Fatal( + FMT("Failed to create directory {}: {}", dir, result.error().message())); + } +} + std::string rewrite_stderr_to_absolute_paths(std::string_view text) { @@ -77,12 +90,6 @@ rewrite_stderr_to_absolute_paths(std::string_view text) "\n", Tokenizer::Mode::include_empty, Tokenizer::IncludeDelimiter::yes)) { - // Rewrite to in the following two cases, where X may - // be optional ANSI CSI sequences: - // - // In file included from XX:1: - // XX:1:2: ... - if (util::starts_with(line, in_file_included_from)) { result += in_file_included_from; line = line.substr(in_file_included_from.length()); @@ -109,19 +116,6 @@ rewrite_stderr_to_absolute_paths(std::string_view text) return result; } -} // namespace - -namespace core { - -void -ensure_dir_exists(const fs::path& dir) -{ - if (auto result = fs::create_directories(dir); !result) { - throw core::Fatal( - FMT("Failed to create directory {}: {}", dir, result.error().message())); - } -} - void send_to_console(const Context& ctx, std::string_view text, int fd) { diff --git a/src/core/common.hpp b/src/core/common.hpp index fc8dcfd80..f8d0b339c 100644 --- a/src/core/common.hpp +++ b/src/core/common.hpp @@ -29,6 +29,13 @@ namespace core { // Like std::filesystem::create_directories but throws core::Fatal on error. void ensure_dir_exists(const std::filesystem::path& dir); +// Rewrite path to absolute path in `text` in the following two cases, where X +// may be optional ANSI CSI sequences: +// +// X[:1:2]X: ... +// In file included from X[:1:2]X: +std::string rewrite_stderr_to_absolute_paths(std::string_view text); + // Send `text` to file descriptor `fd` (typically stdout or stderr, which // potentially is connected to a console), optionally stripping ANSI color // sequences if `ctx.args_info.strip_diagnostics_colors` is true and rewriting diff --git a/unittest/test_core_common.cpp b/unittest/test_core_common.cpp index 7a972d24b..26da83e3c 100644 --- a/unittest/test_core_common.cpp +++ b/unittest/test_core_common.cpp @@ -21,9 +21,13 @@ #include #include #include +#include +#include #include +namespace fs = util::filesystem; + using TestUtil::TestContext; using util::DirEntry; @@ -44,6 +48,31 @@ TEST_CASE("core::ensure_dir_exists") doctest::Contains("Failed to create directory create/dir/file:")); } +TEST_CASE("core::rewrite_stderr_to_absolute_paths") +{ + TestContext test_context; + util::write_file("existing", ""); + + std::string input = + "a:1:2\n" + "existing:3:4\n" + "c:5:6\n" + "\x1b[01m\x1b[Kexisting:\x1b[m\x1b[K: foo\n" + "\x1b[01m\x1b[Kexisting:47:11:\x1b[m\x1b[K: foo\n" + "In file included from \x1b[01m\x1b[Kexisting:\x1b[m\x1b[K: foo\n" + "In file included from \x1b[01m\x1b[Kexisting:47:11:\x1b[m\x1b[K: foo\n"; + std::string expected = FMT( + "a:1:2\n" + "{0}:3:4\n" + "c:5:6\n" + "\x1b[01m\x1b[K{0}:\x1b[m\x1b[K: foo\n" + "\x1b[01m\x1b[K{0}:47:11:\x1b[m\x1b[K: foo\n" + "In file included from \x1b[01m\x1b[K{0}:\x1b[m\x1b[K: foo\n" + "In file included from \x1b[01m\x1b[K{0}:47:11:\x1b[m\x1b[K: foo\n", + *fs::canonical("existing")); + CHECK(core::rewrite_stderr_to_absolute_paths(input) == expected); +} + TEST_CASE("core::strip_ansi_csi_seqs") { const char input[] =