-
Notifications
You must be signed in to change notification settings - Fork 198
/
CompilationDatabaseGenerationTest.java
170 lines (151 loc) · 6.52 KB
/
CompilationDatabaseGenerationTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*******************************************************************************
* 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 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.preferences.InstanceScope;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.ui.preferences.ScopedPreferenceStore;
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;
public class CompilationDatabaseGenerationTest extends AbstractBuilderTest {
/**
* Tests generation of compile_commands.json in "build" folder
*/
@Test
public void testCompilationDatabaseGeneration() throws Exception {
setWorkspace("regressions");
final IProject app = loadProject("helloworldC");
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
*/
@Test
public void testJsonFormat() throws Exception {
setWorkspace("regressions");
final IProject app = loadProject("helloworldC");
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);
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"));
}
}
}
}
/**
* Test that compile_commands.json is correctly generated when more than one .c file is present as a source file
*/
@Test
public void testMultipleFiles() throws Exception {
setWorkspace("regressions");
final IProject app = loadProject("helloworldC");
IFile aFile = ManagedBuildTestHelper.createFile(app, "src/newFile.c");
setGenerateFileOptionEnabled(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);
}
}
/**
* Tests that cpp files are handled by compile_commands.json file generator
*/
@Test
public void isCPPFileAllowed() throws Exception {
setWorkspace("regressions");
final IProject app = loadProject("helloworldCPP");
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);
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"));
}
}
}
}
/**
* Tests that compilation database is not generated when feature is disabled
*/
@Test
public void testCompilationDatabaseGenerationNotEnabled() throws Exception {
setWorkspace("regressions");
final IProject app = loadProject("helloworldC");
setGenerateFileOptionEnabled(false);
app.build(IncrementalProjectBuilder.FULL_BUILD, null);
IFile compilationDatabase = app.getFile("build/compile_commands.json");
assertFalse(compilationDatabase.exists());
}
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
private void restoreDefaultForGenerateFile() {
IPreferenceStore preferenceStore = new ScopedPreferenceStore(InstanceScope.INSTANCE,
"org.eclipse.cdt.managedbuilder.ui");
preferenceStore.setToDefault(CommonBuilder.COMPILATION_DATABASE_ENABLEMENT);
}
}