|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2019, 2020 Marc-Andre Laperle. |
| 3 | + * |
| 4 | + * This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + *******************************************************************************/ |
| 11 | +package org.eclipse.cdt.managedbuilder.core.tests; |
| 12 | + |
| 13 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 14 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 15 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 16 | + |
| 17 | +import java.io.FileReader; |
| 18 | + |
| 19 | +import org.eclipse.cdt.managedbuilder.core.jsoncdb.CompilationDatabaseInformation; |
| 20 | +import org.eclipse.cdt.managedbuilder.internal.core.CommonBuilder; |
| 21 | +import org.eclipse.cdt.managedbuilder.testplugin.AbstractBuilderTest; |
| 22 | +import org.eclipse.cdt.managedbuilder.testplugin.ManagedBuildTestHelper; |
| 23 | +import org.eclipse.core.resources.IFile; |
| 24 | +import org.eclipse.core.resources.IProject; |
| 25 | +import org.eclipse.core.resources.IncrementalProjectBuilder; |
| 26 | +import org.eclipse.core.runtime.preferences.InstanceScope; |
| 27 | +import org.eclipse.jface.preference.IPreferenceStore; |
| 28 | +import org.eclipse.ui.preferences.ScopedPreferenceStore; |
| 29 | +import org.junit.jupiter.api.AfterEach; |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | + |
| 32 | +import com.google.gson.Gson; |
| 33 | +import com.google.gson.JsonArray; |
| 34 | +import com.google.gson.JsonElement; |
| 35 | + |
| 36 | +public class CompilationDatabaseGenerationTest extends AbstractBuilderTest { |
| 37 | + |
| 38 | + /** |
| 39 | + * Tests generation of compile_commands.json in "build" folder |
| 40 | + */ |
| 41 | + @Test |
| 42 | + public void testCompilationDatabaseGeneration() throws Exception { |
| 43 | + setWorkspace("regressions"); |
| 44 | + final IProject app = loadProject("helloworldC"); |
| 45 | + setGenerateFileOptionEnabled(true); |
| 46 | + app.build(IncrementalProjectBuilder.FULL_BUILD, null); |
| 47 | + IFile compilationDatabase = app.getFile("Debug/compile_commands.json"); |
| 48 | + assertTrue(compilationDatabase.exists()); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Tests format for compile_commands.json. JSON array is expected, containing an element for the c file |
| 53 | + */ |
| 54 | + @Test |
| 55 | + public void testJsonFormat() throws Exception { |
| 56 | + setWorkspace("regressions"); |
| 57 | + final IProject app = loadProject("helloworldC"); |
| 58 | + setGenerateFileOptionEnabled(true); |
| 59 | + app.build(IncrementalProjectBuilder.FULL_BUILD, null); |
| 60 | + IFile commandsFile = app.getFile("Debug/compile_commands.json"); |
| 61 | + if (commandsFile.exists()) { |
| 62 | + |
| 63 | + try (FileReader reader = new FileReader(commandsFile.getLocation().toFile())) { |
| 64 | + Gson gson = new Gson(); |
| 65 | + JsonArray jsonArray = gson.fromJson(reader, JsonArray.class); |
| 66 | + for (JsonElement element : jsonArray) { |
| 67 | + CompilationDatabaseInformation compileCommand = gson.fromJson(element, |
| 68 | + CompilationDatabaseInformation.class); |
| 69 | + |
| 70 | + assertTrue(compileCommand.directory() != null && !compileCommand.directory().isEmpty()); |
| 71 | + assertTrue(compileCommand.command() != null && !compileCommand.command().isEmpty()); |
| 72 | + assertTrue(compileCommand.file() != null && !compileCommand.file().isEmpty()); |
| 73 | + assertTrue(compileCommand.file().endsWith("src/helloworldC.c")); |
| 74 | + } |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Test that compile_commands.json is correctly generated when more than one .c file is present as a source file |
| 83 | + */ |
| 84 | + @Test |
| 85 | + public void testMultipleFiles() throws Exception { |
| 86 | + setWorkspace("regressions"); |
| 87 | + final IProject app = loadProject("helloworldC"); |
| 88 | + IFile aFile = ManagedBuildTestHelper.createFile(app, "src/newFile.c"); |
| 89 | + setGenerateFileOptionEnabled(true); |
| 90 | + app.build(IncrementalProjectBuilder.FULL_BUILD, null); |
| 91 | + IFile commandsFile = app.getFile("Debug/compile_commands.json"); |
| 92 | + int numberOfElementsFound = 0; |
| 93 | + boolean helloworldCIsPresent = false; |
| 94 | + boolean newFileIsPresent = false; |
| 95 | + try (FileReader reader = new FileReader(commandsFile.getLocation().toFile())) { |
| 96 | + Gson gson = new Gson(); |
| 97 | + JsonArray jsonArray = gson.fromJson(reader, JsonArray.class); |
| 98 | + System.out.println(jsonArray); |
| 99 | + for (JsonElement element : jsonArray) { |
| 100 | + CompilationDatabaseInformation compileCommand = gson.fromJson(element, |
| 101 | + CompilationDatabaseInformation.class); |
| 102 | + numberOfElementsFound++; |
| 103 | + if (compileCommand.file().endsWith("helloworldC.c")) { |
| 104 | + helloworldCIsPresent = true; |
| 105 | + } |
| 106 | + if (compileCommand.file().endsWith("newFile.c")) { |
| 107 | + newFileIsPresent = true; |
| 108 | + } |
| 109 | + } |
| 110 | + assertEquals(2, numberOfElementsFound); |
| 111 | + assertTrue(helloworldCIsPresent); |
| 112 | + assertTrue(newFileIsPresent); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Tests that cpp files are handled by compile_commands.json file generator |
| 118 | + */ |
| 119 | + @Test |
| 120 | + public void isCPPFileAllowed() throws Exception { |
| 121 | + setWorkspace("regressions"); |
| 122 | + final IProject app = loadProject("helloworldCPP"); |
| 123 | + setGenerateFileOptionEnabled(true); |
| 124 | + app.build(IncrementalProjectBuilder.FULL_BUILD, null); |
| 125 | + IFile commandsFile = app.getFile("Debug/compile_commands.json"); |
| 126 | + if (commandsFile.exists()) { |
| 127 | + |
| 128 | + try (FileReader reader = new FileReader(commandsFile.getLocation().toFile())) { |
| 129 | + Gson gson = new Gson(); |
| 130 | + JsonArray jsonArray = gson.fromJson(reader, JsonArray.class); |
| 131 | + for (JsonElement element : jsonArray) { |
| 132 | + CompilationDatabaseInformation compileCommand = gson.fromJson(element, |
| 133 | + CompilationDatabaseInformation.class); |
| 134 | + |
| 135 | + assertTrue(compileCommand.directory() != null && !compileCommand.directory().isEmpty()); |
| 136 | + assertTrue(compileCommand.command() != null && !compileCommand.command().isEmpty()); |
| 137 | + assertTrue(compileCommand.file() != null && !compileCommand.file().isEmpty()); |
| 138 | + assertTrue(compileCommand.file().endsWith("src/helloworldCPP.cpp")); |
| 139 | + } |
| 140 | + |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Tests that compilation database is not generated when feature is disabled |
| 147 | + */ |
| 148 | + @Test |
| 149 | + public void testCompilationDatabaseGenerationNotEnabled() throws Exception { |
| 150 | + setWorkspace("regressions"); |
| 151 | + final IProject app = loadProject("helloworldC"); |
| 152 | + setGenerateFileOptionEnabled(false); |
| 153 | + app.build(IncrementalProjectBuilder.FULL_BUILD, null); |
| 154 | + IFile compilationDatabase = app.getFile("Debug/compile_commands.json"); |
| 155 | + assertFalse(compilationDatabase.exists()); |
| 156 | + } |
| 157 | + |
| 158 | + private static void setGenerateFileOptionEnabled(boolean value) { |
| 159 | + IPreferenceStore preferenceStore = new ScopedPreferenceStore(InstanceScope.INSTANCE, |
| 160 | + "org.eclipse.cdt.managedbuilder.ui"); |
| 161 | + preferenceStore.setValue(CommonBuilder.COMPILATION_DATABASE_ENABLEMENT, value); |
| 162 | + } |
| 163 | + |
| 164 | + @AfterEach |
| 165 | + public void restoreDefaultForGenerateFile() { |
| 166 | + IPreferenceStore preferenceStore = new ScopedPreferenceStore(InstanceScope.INSTANCE, |
| 167 | + "org.eclipse.cdt.managedbuilder.ui"); |
| 168 | + preferenceStore.setToDefault(CommonBuilder.COMPILATION_DATABASE_ENABLEMENT); |
| 169 | + } |
| 170 | +} |
0 commit comments