Search code examples
clang-format

How do I run the clang-format (libFormat) unit tests?


I'm trying to fix some bugs in clang-format, which means changing code in libFormat.

There is a whole set of google-test unit tests in unittests/Format/FormatTest.cpp.

However, I can't figure out how to build and run just the target that runs those tests.

Specifically, if I change a test in unittests/Format/FormatTest.cpp so that it fails, I want the test that I can run to see it fail.

I can see the failure if I build and run all tests with ninja check-all but that takes a very long time. I can't seem to figure out how to just run the lib-format unit tests so I don't have to wait all day for the others.

I've looked at the documentation, and I'm sure there is something to do with llvm-lit, and I've tried reading the cmake stuff to figure it out, but I now feel like I just gave myself a frontal lobotomy.

Surely, this is a common thing, but I can't seem to find anything in the documentation, or with google that provides much help.


Solution

  • So, FormatTest is actually written using GTest instead of using the usual llvm-lit. I'm sure there's a better way, but I've gotten by just fine with the following method, which is something I came up with with also really limited CMake knowledge.

    Once I've made a change to FormatTest or TokenAnnotatorTest, etc, I run ninja -C build FormatTests which creates a GTest test binary at build/tools/clang/unittests/Format/FormatTests. Running this executable will run the tests, but it will only use one CPU core so it will be painfully slow.

    To remedy this, I use the following project: https://github.com/google/gtest-parallel. It's just a python script I've stashed somewhere, and I run it like: python ~/src/gtest-parallel/gtest_parallel.py build/tools/clang/unittests/Format/FormatTests.

    I agree that this should definitely be documented better, especially if there's a better way I also don't know of.