]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
test: Add test of core::rewrite_stderr_to_absolute_paths
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 31 Dec 2023 14:20:16 +0000 (15:20 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 7 Jan 2024 08:57:37 +0000 (09:57 +0100)
src/core/common.cpp
src/core/common.hpp
unittest/test_core_common.cpp

index 9efe5462aa879cb01ab91839351307c3a7b0d398..63a2143465955a5da60dd551429e494c0fe215e9 100644 (file)
@@ -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 <path> to <absolute path> in the following two cases, where X may
-    // be optional ANSI CSI sequences:
-    //
-    // In file included from X<path>X:1:
-    // X<path>X: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)
 {
index fc8dcfd806ff028984b645dea5793d4ac119e5b7..f8d0b339c40d61f36ba72d6865ad73ef0e3a72cb 100644 (file)
@@ -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<path>[:1:2]X: ...
+//     In file included from X<path>[: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
index 7a972d24b79f031bf044314c25d0657be2489c58..26da83e3c6af92051d61d9e4eeb0c846d84b3055 100644 (file)
 #include <core/common.hpp>
 #include <util/DirEntry.hpp>
 #include <util/file.hpp>
+#include <util/filesystem.hpp>
+#include <util/fmtmacros.hpp>
 
 #include <third_party/doctest.h>
 
+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[] =