-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created JSON Compilation Database Generator preference page and set file
generation on false by default. Added JUnit test to test the generation of the file
- Loading branch information
1 parent
e6fb475
commit f7eb2c8
Showing
14 changed files
with
545 additions
and
17 deletions.
There are no files selected for viewing
Binary file added
BIN
+2.84 KB
...clipse.cdt.managedbuilder.core.tests/resources/builderTests/regressions/helloworldCPP.zip
Binary file not shown.
187 changes: 187 additions & 0 deletions
187
...ts/tests/org/eclipse/cdt/managedbuilder/core/tests/CompilationDatabaseGenerationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2019, 2020 Marc-Andre Laperle. | ||
* | ||
* This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License 2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*******************************************************************************/ | ||
package org.eclipse.cdt.managedbuilder.core.tests; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.io.FileReader; | ||
import java.io.IOException; | ||
|
||
import org.eclipse.cdt.managedbuilder.core.ManagedBuilderCorePlugin; | ||
import org.eclipse.cdt.managedbuilder.core.jsoncdb.CompilationDatabaseInformation; | ||
import org.eclipse.cdt.managedbuilder.testplugin.AbstractBuilderTest; | ||
import org.eclipse.cdt.managedbuilder.testplugin.ManagedBuildTestHelper; | ||
import org.eclipse.core.resources.IFile; | ||
import org.eclipse.core.resources.IProject; | ||
import org.eclipse.core.resources.IncrementalProjectBuilder; | ||
import org.eclipse.core.runtime.CoreException; | ||
import org.eclipse.core.runtime.preferences.InstanceScope; | ||
import org.eclipse.jface.preference.IPreferenceStore; | ||
import org.eclipse.ui.preferences.ScopedPreferenceStore; | ||
import org.junit.Test; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonIOException; | ||
|
||
public class CompilationDatabaseGenerationTest extends AbstractBuilderTest { | ||
|
||
/** | ||
* Tests generation of compile_commands.json in "build" folder | ||
* @throws CoreException | ||
*/ | ||
@Test | ||
public void testCompilationDatabaseGeneration() throws CoreException { | ||
setWorkspace("regressions"); | ||
final IProject app = loadProject("helloworldC"); | ||
isGenerateFileOptionEnabled(true); | ||
app.build(IncrementalProjectBuilder.FULL_BUILD, null); | ||
IFile compilationDatabase = app.getFile("build/compile_commands.json"); | ||
assertTrue(compilationDatabase.exists()); | ||
} | ||
|
||
/** | ||
* Tests format for compile_commands.json. JSON array is expected, containing an element for the c file | ||
* @throws JsonIOException | ||
* @throws CoreException | ||
*/ | ||
@Test | ||
public void testJsonFormat() throws JsonIOException, CoreException { | ||
setWorkspace("regressions"); | ||
final IProject app = loadProject("helloworldC"); | ||
isGenerateFileOptionEnabled(true); | ||
app.build(IncrementalProjectBuilder.FULL_BUILD, null); | ||
IFile commandsFile = app.getFile("build/compile_commands.json"); | ||
if (commandsFile.exists()) { | ||
|
||
try (FileReader reader = new FileReader(commandsFile.getLocation().toFile())) { | ||
Gson gson = new Gson(); | ||
JsonArray jsonArray = gson.fromJson(reader, JsonArray.class); | ||
System.out.println(jsonArray); | ||
for (JsonElement element : jsonArray) { | ||
CompilationDatabaseInformation compileCommand = gson.fromJson(element, | ||
CompilationDatabaseInformation.class); | ||
|
||
assertTrue(compileCommand.directory() != null && !compileCommand.directory().isEmpty()); | ||
assertTrue(compileCommand.command() != null && !compileCommand.command().isEmpty()); | ||
assertTrue(compileCommand.file() != null && !compileCommand.file().isEmpty()); | ||
assertTrue(compileCommand.file().endsWith("src/helloworldC.c")); | ||
} | ||
|
||
} catch (IOException e) { | ||
assertTrue(false); | ||
} | ||
|
||
} | ||
} | ||
|
||
/** | ||
* Test that compile_commands.json is correctly generated when more than one .c file is present as a source file | ||
* @throws CoreException | ||
*/ | ||
@Test | ||
public void testMultipleFiles() throws CoreException { | ||
setWorkspace("regressions"); | ||
final IProject app = loadProject("helloworldC"); | ||
IFile aFile = ManagedBuildTestHelper.createFile(app, "src/newFile.c"); | ||
isGenerateFileOptionEnabled(true); | ||
app.build(IncrementalProjectBuilder.FULL_BUILD, null); | ||
IFile commandsFile = app.getFile("build/compile_commands.json"); | ||
int numberOfElementsFound = 0; | ||
boolean helloworldCIsPresent = false; | ||
boolean newFileIsPresent = false; | ||
try (FileReader reader = new FileReader(commandsFile.getLocation().toFile())) { | ||
Gson gson = new Gson(); | ||
JsonArray jsonArray = gson.fromJson(reader, JsonArray.class); | ||
System.out.println(jsonArray); | ||
for (JsonElement element : jsonArray) { | ||
CompilationDatabaseInformation compileCommand = gson.fromJson(element, | ||
CompilationDatabaseInformation.class); | ||
numberOfElementsFound++; | ||
if (compileCommand.file().endsWith("helloworldC.c")) { | ||
helloworldCIsPresent = true; | ||
} | ||
if (compileCommand.file().endsWith("newFile.c")) { | ||
newFileIsPresent = true; | ||
} | ||
} | ||
assertEquals(2, numberOfElementsFound); | ||
assertTrue(helloworldCIsPresent); | ||
assertTrue(newFileIsPresent); | ||
} catch (IOException e) { | ||
assertTrue(false); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Tests that cpp files are handled by compile_commands.json file generator | ||
* @throws CoreException | ||
*/ | ||
@Test | ||
public void isCPPFileAllowed() throws CoreException { | ||
setWorkspace("regressions"); | ||
final IProject app = loadProject("helloworldCPP"); | ||
isGenerateFileOptionEnabled(true); | ||
app.build(IncrementalProjectBuilder.FULL_BUILD, null); | ||
System.out.println(app.getLocation()); | ||
IFile commandsFile = app.getFile("build/compile_commands.json"); | ||
if (commandsFile.exists()) { | ||
|
||
try (FileReader reader = new FileReader(commandsFile.getLocation().toFile())) { | ||
Gson gson = new Gson(); | ||
JsonArray jsonArray = gson.fromJson(reader, JsonArray.class); | ||
System.out.println(jsonArray); | ||
for (JsonElement element : jsonArray) { | ||
CompilationDatabaseInformation compileCommand = gson.fromJson(element, | ||
CompilationDatabaseInformation.class); | ||
|
||
assertTrue(compileCommand.directory() != null && !compileCommand.directory().isEmpty()); | ||
assertTrue(compileCommand.command() != null && !compileCommand.command().isEmpty()); | ||
assertTrue(compileCommand.file() != null && !compileCommand.file().isEmpty()); | ||
assertTrue(compileCommand.file().endsWith("src/helloworldCPP.cpp")); | ||
} | ||
|
||
} catch (IOException e) { | ||
assertTrue(false); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Tests that compilation database is not generated when feature is disabled | ||
* @throws CoreException | ||
*/ | ||
@Test | ||
public void testCompilationDatabaseGenerationNotEnabled() throws CoreException { | ||
setWorkspace("regressions"); | ||
final IProject app = loadProject("helloworldC"); | ||
isGenerateFileOptionEnabled(false); | ||
app.build(IncrementalProjectBuilder.FULL_BUILD, null); | ||
IFile compilationDatabase = app.getFile("build/compile_commands.json"); | ||
assertFalse(compilationDatabase.exists()); | ||
} | ||
|
||
public static boolean isGenerateFileOptionEnabled(boolean value) { | ||
try { | ||
IPreferenceStore preferenceStore = new ScopedPreferenceStore(InstanceScope.INSTANCE, | ||
"org.eclipse.cdt.managedbuilder.ui"); //$NON-NLS-1$ | ||
preferenceStore.setDefault("generateFile", value); | ||
return preferenceStore.getBoolean("generateFile"); | ||
} catch (Exception e) { | ||
ManagedBuilderCorePlugin.log(e); | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.