|
33 | 33 | import org.eclipse.cdt.utils.CommandLineUtil; |
34 | 34 | import org.eclipse.core.resources.IFile; |
35 | 35 | import org.eclipse.core.resources.IProject; |
| 36 | +import org.eclipse.core.resources.IResource; |
36 | 37 | import org.eclipse.core.resources.IncrementalProjectBuilder; |
| 38 | +import org.eclipse.core.resources.ResourcesPlugin; |
37 | 39 | import org.eclipse.core.runtime.IPath; |
38 | 40 | import org.eclipse.core.runtime.Path; |
39 | 41 | import org.eclipse.core.runtime.preferences.InstanceScope; |
@@ -246,4 +248,47 @@ private static String escapeArgsForCompileCommand(List<String> args) { |
246 | 248 | }).collect(Collectors.joining(" ")); |
247 | 249 | } |
248 | 250 |
|
| 251 | + @Test |
| 252 | + public void testCompileCommandsDirectory() throws Exception { |
| 253 | + setWorkspace("regressions"); |
| 254 | + final IProject app = loadProject("helloworldC"); |
| 255 | + IManagedBuildInfo info = ManagedBuildManager.getBuildInfo(app); |
| 256 | + IManagedProject mProj = info.getManagedProject(); |
| 257 | + IConfiguration cfg = mProj.getConfigurations()[0]; |
| 258 | + IToolChain toolChain = cfg.getToolChain(); |
| 259 | + IBuilder builder = toolChain.getBuilder(); |
| 260 | + setGenerateFileOptionEnabled(true); |
| 261 | + app.build(IncrementalProjectBuilder.FULL_BUILD, null); |
| 262 | + |
| 263 | + IFile commandsFile = app.getFile("Debug/compile_commands.json"); |
| 264 | + |
| 265 | + if (commandsFile.exists()) { |
| 266 | + try (FileReader reader = new FileReader(commandsFile.getLocation().toFile())) { |
| 267 | + Gson gson = new Gson(); |
| 268 | + JsonArray jsonArray = gson.fromJson(reader, JsonArray.class); |
| 269 | + IPath buildDir = ManagedBuildManager.getBuildFullPath(cfg, builder); |
| 270 | + |
| 271 | + IResource rc = ResourcesPlugin.getWorkspace().getRoot().findMember(buildDir); |
| 272 | + String workspacePath = rc != null ? rc.getLocation().toOSString() : buildDir.toOSString(); |
| 273 | + |
| 274 | + java.nio.file.Path workspaceNormalized = java.nio.file.Paths.get(workspacePath).normalize(); |
| 275 | + |
| 276 | + for (JsonElement element : jsonArray) { |
| 277 | + CompilationDatabaseInformation compileCommand = gson.fromJson(element, |
| 278 | + CompilationDatabaseInformation.class); |
| 279 | + |
| 280 | + String directory = compileCommand.directory(); |
| 281 | + assertNotNull("Directory field should not be null", directory); |
| 282 | + assertFalse(directory.isEmpty(), "Directory field should not be empty"); |
| 283 | + |
| 284 | + java.nio.file.Path directoryNormalized = java.nio.file.Paths.get(directory).normalize(); |
| 285 | + |
| 286 | + assertTrue(directoryNormalized.startsWith(workspaceNormalized), |
| 287 | + "Directory should start with workspace path.\nExpected prefix: " + workspaceNormalized |
| 288 | + + "\nBut got: " + directoryNormalized); |
| 289 | + } |
| 290 | + } |
| 291 | + } |
| 292 | + } |
| 293 | + |
249 | 294 | } |
0 commit comments