@@ -169,48 +169,6 @@ TEST_F(OverlayCDBTest, Adjustments) {
169
169
" -DFallback" , " -DAdjust_baz.cc" ));
170
170
}
171
171
172
- // Allows placement of files for tests and cleans them up after.
173
- // FIXME: GlobalCompilationDatabase is mostly VFS-clean now, switch to MockFS?
174
- class ScratchFS {
175
- llvm::SmallString<128 > Root;
176
-
177
- public:
178
- ScratchFS () {
179
- EXPECT_FALSE (llvm::sys::fs::createUniqueDirectory (" clangd-cdb-test" , Root))
180
- << " Failed to create unique directory" ;
181
- }
182
-
183
- ~ScratchFS () {
184
- EXPECT_FALSE (llvm::sys::fs::remove_directories (Root))
185
- << " Failed to cleanup " << Root;
186
- }
187
-
188
- llvm::StringRef root () const { return Root; }
189
-
190
- void write (PathRef RelativePath, llvm::StringRef Contents) {
191
- std::string AbsPath = path (RelativePath);
192
- EXPECT_FALSE (llvm::sys::fs::create_directories (
193
- llvm::sys::path::parent_path (AbsPath)))
194
- << " Failed to create directories for: " << AbsPath;
195
-
196
- std::error_code EC;
197
- llvm::raw_fd_ostream OS (AbsPath, EC);
198
- EXPECT_FALSE (EC) << " Failed to open " << AbsPath << " for writing" ;
199
- OS << llvm::formatv (Contents.data (),
200
- llvm::sys::path::convert_to_slash (Root));
201
- OS.close ();
202
-
203
- EXPECT_FALSE (OS.has_error ());
204
- }
205
-
206
- std::string path (PathRef RelativePath) const {
207
- llvm::SmallString<128 > AbsPath (Root);
208
- llvm::sys::path::append (AbsPath, RelativePath);
209
- llvm::sys::path::native (AbsPath);
210
- return AbsPath.str ().str ();
211
- }
212
- };
213
-
214
172
TEST (GlobalCompilationDatabaseTest, DiscoveryWithNestedCDBs) {
215
173
const char *const CDBOuter =
216
174
R"cdb(
@@ -242,59 +200,59 @@ TEST(GlobalCompilationDatabaseTest, DiscoveryWithNestedCDBs) {
242
200
}
243
201
]
244
202
)cdb" ;
245
- ScratchFS FS;
246
- RealThreadsafeFS TFS;
247
- FS.write (" compile_commands.json" , CDBOuter);
248
- FS.write (" build/compile_commands.json" , CDBInner);
203
+ MockFS FS;
204
+ FS.Files [testPath (" compile_commands.json" )] =
205
+ llvm::formatv (CDBOuter, llvm::sys::path::convert_to_slash (testRoot ()));
206
+ FS.Files [testPath (" build/compile_commands.json" )] =
207
+ llvm::formatv (CDBInner, llvm::sys::path::convert_to_slash (testRoot ()));
249
208
250
209
// Note that gen2.cc goes missing with our following model, not sure this
251
210
// happens in practice though.
252
211
{
253
- DirectoryBasedGlobalCompilationDatabase DB (TFS );
212
+ DirectoryBasedGlobalCompilationDatabase DB (FS );
254
213
std::vector<std::string> DiscoveredFiles;
255
214
auto Sub =
256
215
DB.watch ([&DiscoveredFiles](const std::vector<std::string> Changes) {
257
216
DiscoveredFiles = Changes;
258
217
});
259
218
260
- DB.getCompileCommand (FS. path (" build/../a.cc" ));
219
+ DB.getCompileCommand (testPath (" build/../a.cc" ));
261
220
EXPECT_THAT (DiscoveredFiles, UnorderedElementsAre (AllOf (
262
221
EndsWith (" a.cc" ), Not (HasSubstr (" .." )))));
263
222
DiscoveredFiles.clear ();
264
223
265
- DB.getCompileCommand (FS. path (" build/gen.cc" ));
224
+ DB.getCompileCommand (testPath (" build/gen.cc" ));
266
225
EXPECT_THAT (DiscoveredFiles, UnorderedElementsAre (EndsWith (" gen.cc" )));
267
226
}
268
227
269
228
// With a custom compile commands dir.
270
229
{
271
- DirectoryBasedGlobalCompilationDatabase::Options Opts (TFS );
272
- Opts.CompileCommandsDir = FS. root (). str ();
230
+ DirectoryBasedGlobalCompilationDatabase::Options Opts (FS );
231
+ Opts.CompileCommandsDir = testRoot ();
273
232
DirectoryBasedGlobalCompilationDatabase DB (Opts);
274
233
std::vector<std::string> DiscoveredFiles;
275
234
auto Sub =
276
235
DB.watch ([&DiscoveredFiles](const std::vector<std::string> Changes) {
277
236
DiscoveredFiles = Changes;
278
237
});
279
238
280
- DB.getCompileCommand (FS. path (" a.cc" ));
239
+ DB.getCompileCommand (testPath (" a.cc" ));
281
240
EXPECT_THAT (DiscoveredFiles,
282
241
UnorderedElementsAre (EndsWith (" a.cc" ), EndsWith (" gen.cc" ),
283
242
EndsWith (" gen2.cc" )));
284
243
DiscoveredFiles.clear ();
285
244
286
- DB.getCompileCommand (FS. path (" build/gen.cc" ));
245
+ DB.getCompileCommand (testPath (" build/gen.cc" ));
287
246
EXPECT_THAT (DiscoveredFiles, IsEmpty ());
288
247
}
289
248
}
290
249
291
250
TEST (GlobalCompilationDatabaseTest, BuildDir) {
292
- ScratchFS FS;
293
- RealThreadsafeFS TFS;
251
+ MockFS FS;
294
252
auto Command = [&](llvm::StringRef Relative) {
295
- DirectoryBasedGlobalCompilationDatabase::Options Opts (TFS );
253
+ DirectoryBasedGlobalCompilationDatabase::Options Opts (FS );
296
254
return DirectoryBasedGlobalCompilationDatabase (Opts)
297
- .getCompileCommand (FS. path (Relative))
255
+ .getCompileCommand (testPath (Relative))
298
256
.getValueOr (tooling::CompileCommand ())
299
257
.CommandLine ;
300
258
};
@@ -314,7 +272,8 @@ TEST(GlobalCompilationDatabaseTest, BuildDir) {
314
272
}
315
273
]
316
274
)cdb" ;
317
- FS.write (" x/build/compile_commands.json" , CDB);
275
+ FS.Files [testPath (" x/build/compile_commands.json" )] =
276
+ llvm::formatv (CDB, llvm::sys::path::convert_to_slash (testRoot ()));
318
277
EXPECT_THAT (Command (" x/foo.cc" ), Contains (" -DXYZZY" ));
319
278
EXPECT_THAT (Command (" bar.cc" ), IsEmpty ())
320
279
<< " x/build/compile_flags.json only applicable to x/" ;
0 commit comments