Skip to content

Commit

Permalink
Fixed selection issue on property page
Browse files Browse the repository at this point in the history
  • Loading branch information
alicetrifu committed Mar 15, 2024
1 parent 852b0a8 commit d530365
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 58 deletions.
Expand Up @@ -3,5 +3,9 @@ helloworldC.zip:
Basic standard Linux GCC C HelloWorld ManagedBuild Project template.
Contains Debug + Release configurations.

helloworldCPP.zip:
Minimal standard Linux GCC C++ HelloWorld ManagedBuild Project template
Contains Debug + Release configurations.

bug_335476.zip:
Test for noticing environment variable changes.
Binary file not shown.
Expand Up @@ -15,61 +15,54 @@
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.internal.core.CommonBuilder;
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.Ignore;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.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 {
public void testCompilationDatabaseGeneration() throws Exception {
setWorkspace("regressions");
final IProject app = loadProject("helloworldC");
isGenerateFileOptionEnabled(true);
setGenerateFileOptionEnabled(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 {
public void testJsonFormat() throws Exception {
setWorkspace("regressions");
final IProject app = loadProject("helloworldC");
isGenerateFileOptionEnabled(true);
setGenerateFileOptionEnabled(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);
Expand All @@ -80,23 +73,20 @@ public void testJsonFormat() throws JsonIOException, CoreException {
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 {
public void testMultipleFiles() throws Exception {
setWorkspace("regressions");
final IProject app = loadProject("helloworldC");
IFile aFile = ManagedBuildTestHelper.createFile(app, "src/newFile.c");
isGenerateFileOptionEnabled(true);
setGenerateFileOptionEnabled(true);
app.build(IncrementalProjectBuilder.FULL_BUILD, null);
IFile commandsFile = app.getFile("build/compile_commands.json");
int numberOfElementsFound = 0;
Expand All @@ -120,31 +110,24 @@ public void testMultipleFiles() throws CoreException {
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
@Ignore("This will be temporary skipped due to builder error")
public void isCPPFileAllowed() throws CoreException {
public void isCPPFileAllowed() throws Exception {
setWorkspace("regressions");
final IProject app = loadProject("helloworldCPP");
isGenerateFileOptionEnabled(true);
setGenerateFileOptionEnabled(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);
Expand All @@ -155,35 +138,33 @@ public void isCPPFileAllowed() throws CoreException {
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 {
public void testCompilationDatabaseGenerationNotEnabled() throws Exception {
setWorkspace("regressions");
final IProject app = loadProject("helloworldC");
isGenerateFileOptionEnabled(false);
setGenerateFileOptionEnabled(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;
private static void setGenerateFileOptionEnabled(boolean value) {
IPreferenceStore preferenceStore = new ScopedPreferenceStore(InstanceScope.INSTANCE,
"org.eclipse.cdt.managedbuilder.ui");
preferenceStore.setValue(CommonBuilder.COMPILATION_DATABASE_ENABLEMENT, value);
}

@AfterEach
public void restoreDefaultForGenerateFile() {
IPreferenceStore preferenceStore = new ScopedPreferenceStore(InstanceScope.INSTANCE,
"org.eclipse.cdt.managedbuilder.ui");
preferenceStore.setToDefault(CommonBuilder.COMPILATION_DATABASE_ENABLEMENT);
}
}
5 changes: 5 additions & 0 deletions build/org.eclipse.cdt.managedbuilder.core/.project
Expand Up @@ -25,6 +25,11 @@
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ds.core.builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
Expand Down
Expand Up @@ -16,6 +16,7 @@ Export-Package: org.eclipse.cdt.build.core.scannerconfig,
org.eclipse.cdt.managedbuilder.envvar,
org.eclipse.cdt.managedbuilder.internal.buildmodel;x-friends:="org.eclipse.cdt.managedbuilder.ui",
org.eclipse.cdt.managedbuilder.internal.core;x-friends:="org.eclipse.cdt.managedbuilder.ui,org.eclipse.cdt.managedbuilder.headlessbuilderapp",
org.eclipse.cdt.managedbuilder.internal.core.jsoncdb.generator;x-internal:=true,
org.eclipse.cdt.managedbuilder.internal.dataprovider;x-internal:=true,
org.eclipse.cdt.managedbuilder.internal.envvar;x-internal:=true,
org.eclipse.cdt.managedbuilder.internal.language.settings.providers;x-friends:="org.eclipse.cdt.managedbuilder.ui",
Expand Down
Expand Up @@ -97,7 +97,7 @@ public class CommonBuilder extends ACBuilder implements IIncrementalProjectBuild
private static final String NEWLINE = System.getProperty("line.separator"); //$NON-NLS-1$
private static final String TRACE_FOOTER = "]: "; //$NON-NLS-1$
private static final String TRACE_HEADER = "GeneratedmakefileBuilder trace ["; //$NON-NLS-1$
private static final String COMPILATION_DATABASE_ENABLEMENT = "generateFile"; //$NON-NLS-1$
public static final String COMPILATION_DATABASE_ENABLEMENT = "generateCBDFile"; //$NON-NLS-1$
public static boolean VERBOSE = false;

private static final int PROGRESS_MONITOR_SCALE = 100;
Expand Down
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %pluginName
Bundle-SymbolicName: org.eclipse.cdt.managedbuilder.ui; singleton:=true
Bundle-Version: 9.4.100.qualifier
Bundle-Version: 9.4.200.qualifier
Bundle-Activator: org.eclipse.cdt.managedbuilder.ui.properties.ManagedBuilderUIPlugin
Bundle-Vendor: %providerName
Bundle-Localization: plugin
Expand Down
Expand Up @@ -9,6 +9,7 @@
********************************************************************************/
package org.eclipse.cdt.managedbuilder.internal.ui.compilationdatabase;

import org.eclipse.cdt.managedbuilder.internal.core.CommonBuilder;
import org.eclipse.cdt.managedbuilder.ui.properties.ManagedBuilderUIPlugin;
import org.eclipse.cdt.ui.dialogs.AbstractCOptionPage;
import org.eclipse.cdt.ui.dialogs.ICOptionContainer;
Expand All @@ -33,15 +34,14 @@
*/
public class CompilationDatabaseGeneratorBlock extends AbstractCOptionPage {

private static final String PREF_PAGE_ID = "org.eclipse.cdt.managedbuilder.internal.ui.compilationdatabase.JsonCdbGeneratorPreferencePage"; //$NON-NLS-1$
private final String ENABLE_FILE_GENERATION = "generateFile"; //$NON-NLS-1$
private static final String PREF_PAGE_ID = "org.eclipse.cdt.managedbuilder.ui.compilationdatabase.JsonCdbPreferencePage"; //$NON-NLS-1$
private final String ENABLE_FILE_GENERATION = CommonBuilder.COMPILATION_DATABASE_ENABLEMENT;
private Button generateFileCheckbox;
private IPreferenceStore preferenceStore;
private PreferenceScopeBlock fPrefScopeBlock;

protected CompilationDatabaseGeneratorBlock() {
public CompilationDatabaseGeneratorBlock() {
preferenceStore = ManagedBuilderUIPlugin.getDefault().getPreferenceStore();
performDefaults();
}

@Override
Expand All @@ -53,7 +53,7 @@ public void createControl(Composite parent) {
fPrefScopeBlock = new PreferenceScopeBlock(PREF_PAGE_ID) {
@Override
protected void onPreferenceScopeChange() {
generateFileCheckbox.setEnabled(preferenceStore.getBoolean(ENABLE_FILE_GENERATION));
generateFileCheckbox.setSelection(preferenceStore.getBoolean(ENABLE_FILE_GENERATION));
}
};
fPrefScopeBlock.createControl(composite);
Expand All @@ -66,21 +66,16 @@ protected void onPreferenceScopeChange() {
generateFileCheckbox = new Button(cdbGeneratorOptions, SWT.CHECK);
generateFileCheckbox.setSelection(preferenceStore.getBoolean(ENABLE_FILE_GENERATION));
generateFileCheckbox.setText(Messages.JsonCdbGeneratorPreferencePage_generateCompilationdatabase);
generateFileCheckbox.addListener(SWT.Selection, e -> {
boolean newValue = generateFileCheckbox.getSelection();
preferenceStore.setValue(ENABLE_FILE_GENERATION, newValue);
});

}

@Override
public void performDefaults() {
preferenceStore.setDefault(ENABLE_FILE_GENERATION, false);
preferenceStore.setToDefault(ENABLE_FILE_GENERATION);
}

@Override
public void performApply(IProgressMonitor monitor) throws CoreException {
this.performApply(monitor);
preferenceStore.setValue(ENABLE_FILE_GENERATION, generateFileCheckbox.getSelection());
}

private IProject getProject() {
Expand Down
Expand Up @@ -9,10 +9,12 @@
********************************************************************************/
package org.eclipse.cdt.managedbuilder.internal.ui.compilationdatabase;

import org.eclipse.cdt.managedbuilder.ui.properties.ManagedBuilderUIPlugin;
import org.eclipse.cdt.ui.dialogs.ICOptionContainer;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
Expand Down Expand Up @@ -72,6 +74,7 @@ public boolean performOk() {
try {
fOptionBlock.performApply(new NullProgressMonitor());
} catch (CoreException e) {
ManagedBuilderUIPlugin.log(Status.error("Failed to save JSON Compilation Database Generator settings", e)); //$NON-NLS-1$
}
return true;
}
Expand Down
Expand Up @@ -17,6 +17,7 @@
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Preferences;
import org.eclipse.core.runtime.Status;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
Expand Down Expand Up @@ -54,7 +55,7 @@ public boolean performOk() {
try {
optionPage.performApply(new NullProgressMonitor());
} catch (CoreException e) {
ManagedBuilderUIPlugin.log(e);
ManagedBuilderUIPlugin.log(Status.error("Failed to save JSON Compilation Database Generator settings", e)); //$NON-NLS-1$
}
return true;
}
Expand Down

0 comments on commit d530365

Please sign in to comment.