Search code examples
cmakegoogletestninja

GTest's output has no colors when built with cmake+ninja and executed automatically


I'm trying to configure CMake and ninja as a build system for my project. Except the app itself I have an extra executable for unit tests powered by gtest. I thought it would be nice to have them executed automatically whenever they are built. Here's how I made it:

├── build
└── source
    ├── CMakeLists.txt
    ├── main.cc
    └── ut
        ├── CMakeLists.txt
        ├── gtest
        │   ├── ...
        └── ut.cc

source/CMakeLists.txt...

cmake_minimum_required (VERSION 2.6)
project (trial)
add_subdirectory(ut)
add_executable(trial main.cc)

...and source/ut/CMakeLists.txt:

add_subdirectory(gtest)
include_directories ("gtest/include")

add_executable(ut ut.cc)
target_link_libraries(ut LINK_PUBLIC gtest_main)

add_custom_target(run_uts
                  COMMAND ut
                  DEPENDS ut
                  WORKING_DIRECTORY ${CMAKE_PROJECT_DIR}
                 )

Now when I build it, i.e.:

cd build
cmake -GNinja ../source
ninja run_uts

It works fine except that the output is colorless. When I run the ut binary by hand, i.e. build/ut/ut I get nice green and red colors. The colors are also there when I use Unix Makefiles as a genrator for CMake.

Since I'm only learning CMake, is there something I missed or is it an issue with Ninja?


Solution

  • I assume your automated code runs a gtest executable and directs the output to a file. By default, gtest adds color sequences only when sending output to a terminal. In order to force it to add color sequences to output sent to a file or a pipe, run your test executable with the --gtest_color=yes option.