Files
clice/tests/unit/test/cdb_helper.h
ykiko bc04845293 refactor(tests): CMake-based CDB, workspace fixture, test cleanup (#378)
## Summary

- **CMake-based CDB generation for module tests**: Replace hand-written
compile_commands.json with CMakeLists.txt (CMake 3.28 `FILE_SET
CXX_MODULES`) in all 26 `tests/data/modules/*/` directories. CDB is
generated on-the-fly via `cmake -G Ninja` during test setup.
- **`@pytest.mark.workspace()` decorator**: Introduce a marker + fixture
pattern so tests declare their workspace via decorator and receive a
resolved `workspace` path. The fixture auto-generates CDB when a
CMakeLists.txt is present.
- **`CliceClient` helper methods**: Add `initialize()`, `open()`,
`wait_diagnostics()`, and `open_and_wait()` to reduce boilerplate across
all test files.
- **Use `asyncio_mode = "auto"`**: Switch from `@pytest_asyncio.fixture`
+ `@pytest.mark.asyncio` to `@pytest.fixture` + auto mode for proper
Pylance type inference on fixtures.
- **Test cleanup**: Remove redundant section separators and docstrings,
delete `tests/pyproject.toml` (config moved to `pytest.ini`).
- **Format task**: Add `.cppm` to `format-cpp` glob pattern.
- **CI fix**: Disable `CMAKE_CXX_SCAN_FOR_MODULES` and prefer pixi
clang++ to fix macOS CI where CMake rejects module scanning.

## Test plan

- [x] All 26 module test directories have CMakeLists.txt with FILE_SET
CXX_MODULES
- [x] generate_cdb() produces valid compile_commands.json with module
flags
- [x] Integration tests pass locally
- [ ] CI passes on all platforms (Linux, macOS, Windows)

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **Tests**
* Unified fixtures and client workflow: new init/open/wait helpers,
workspace marker support, bounded diagnostics waiting, CMake-based
compilation-database generation, and directory-backed temp-file
workflows; enabled asyncio test mode.
* **Chores**
* Added many C++20 module test projects and test data; removed prior
test pyproject in favor of pytest config; updated formatter to include
.cppm files.
* **Style**
* Reformatted many module/source implementations to consistent
multi-line function bodies.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-31 16:57:48 +08:00

68 lines
1.8 KiB
C++

#pragma once
#include <cstddef>
#include <string>
#include <vector>
#include "test/temp_dir.h"
#include "command/command.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringRef.h"
namespace clice::testing {
struct CDBEntry {
llvm::StringRef dir;
std::string file;
std::vector<std::string> extra_args;
};
/// Escape backslashes and quotes for JSON string values.
inline std::string json_escape(llvm::StringRef s) {
std::string result;
result.reserve(s.size());
for(char c: s) {
if(c == '\\' || c == '"') {
result += '\\';
}
result += c;
}
return result;
}
/// Build a compile_commands.json array from entries.
/// Uses "arguments" array form to avoid platform-specific tokenization issues.
inline std::string build_cdb_json(llvm::ArrayRef<CDBEntry> entries) {
std::string json = "[\n";
for(std::size_t i = 0; i < entries.size(); ++i) {
auto& e = entries[i];
if(i > 0) {
json += ",\n";
}
json += R"( {"directory": ")";
json += json_escape(e.dir);
json += R"(", "file": ")";
json += json_escape(e.file);
json += R"(", "arguments": ["clang++", "-std=c++20")";
for(auto& arg: e.extra_args) {
json += R"(, ")";
json += json_escape(arg);
json += R"(")";
}
json += R"(, ")";
json += json_escape(e.file);
json += R"("]})";
}
json += "\n]";
return json;
}
/// Write a compile_commands.json into the temp dir and load it into the given CDB.
inline void write_cdb(TempDir& tmp, CompilationDatabase& cdb, llvm::StringRef json_content) {
tmp.touch("compile_commands.json", json_content);
cdb.load(tmp.path("compile_commands.json"));
}
} // namespace clice::testing