c++cmakectest

CTest output and log file with words cut off


I run the following command:

$> ctest -V -R path_unittest

and get the following failure message:

2: /path_to_spec/path_unittest.cpp:80: ERROR: CHECK( lines[0] == "test,test1,test2" ) is NOT correct!
 == test,test1,test2 )st,test1,test2
2: 
2: /path_to_spec/path_unittest.cpp:81: ERROR: CHECK( lines[1] == "1,2,3" ) is NOT correct!
 == 1,2,3 ): CHECK( 1,2,3
2:

It also tells me:

Output from these tests are in: /path_to_log/LastTest.log

but when I cat that file, the failure message shows up as this:

/path_to_spec/path_unittest.cpp:80: ERROR: CHECK( lines[0] == "test,test1,test2" ) is NOT correct!
 == test,test1,test2 )test1,test2

/path_to_spec/path_unittest.cpp:81: ERROR: CHECK( lines[1] == "1,2,3" ) is NOT correct!
 == 1,2,3 )HECK( 1,2,3

What's happening here? Notice that in the test output, it says == test,test1,test2 )st,test1,test2 and == 1,2,3 ): CHECK( 1,2,3 and then cuts off. Similarly, in the log output, it says == 1,2,3 )HECK( 1,2,3. Why is the word CHECK being cut off in the log file? Why does it seem like the results are being ended prematurely in the ctest output?

The results are really weird because it seems like the expected and actual data are the same so I'm hoping there's some kind of flag or something I can pass to ctest that will give me a better idea of why the test is failing.

Editing to add a minimal reproducible example:

#include "../common.hpp"

#include <base/exception.hpp>
#include <base/paths.hpp>
#include <cstdlib>

TEST_CASE("test path functions") {
  SUBCASE("test get file contents") {
    SUBCASE("test valid file") {
      String path_str =
          base::path::unittest_resource_path() + "base/path/test.csv";
      Strings lines;
      base::path::get_lines_from_file(path_str, lines);
      CHECK(lines.size() == 2);
      CHECK(lines[0] == "test,test1,test2");
      CHECK(lines[1] == "1,2,3");
    }
  }
}

Here's the function being tested, I assume you don't need the includes to make sense of it:

void get_lines_from_file(const String &fname, Strings &lines) {
  if (!std::filesystem::exists(fname)) {
    String msg = "The file " + fname + " does not exist!";
    base::log_and_throw<base::InputException>(msg);
  }
  String line;
  std::ifstream input;
  input.open(fname);
  std::stringstream buffer;
  buffer << input.rdbuf();
  lines = base::string::split(buffer.str(), "\n");
  input.close();
}

Solution

  • I'm guessing that your input file has line endings of "\r\n". You split at "\n" so you end up with strings ending in "\r". When these strings are printed they cause the strange output you see, because "\r" causes the output to continue from the beginning of the current line, thus overwriting previous output.