15
15
import static org .junit .jupiter .api .Assertions .assertTrue ;
16
16
17
17
import java .io .FileReader ;
18
- import java .io .IOException ;
19
18
20
- import org .eclipse .cdt .managedbuilder .core .ManagedBuilderCorePlugin ;
21
19
import org .eclipse .cdt .managedbuilder .core .jsoncdb .CompilationDatabaseInformation ;
20
+ import org .eclipse .cdt .managedbuilder .internal .core .CommonBuilder ;
22
21
import org .eclipse .cdt .managedbuilder .testplugin .AbstractBuilderTest ;
23
22
import org .eclipse .cdt .managedbuilder .testplugin .ManagedBuildTestHelper ;
24
23
import org .eclipse .core .resources .IFile ;
25
24
import org .eclipse .core .resources .IProject ;
26
25
import org .eclipse .core .resources .IncrementalProjectBuilder ;
27
- import org .eclipse .core .runtime .CoreException ;
28
26
import org .eclipse .core .runtime .preferences .InstanceScope ;
29
27
import org .eclipse .jface .preference .IPreferenceStore ;
30
28
import org .eclipse .ui .preferences .ScopedPreferenceStore ;
31
- import org .junit .Ignore ;
32
- import org .junit .Test ;
29
+ import org .junit .jupiter . api . AfterEach ;
30
+ import org .junit .jupiter . api . Test ;
33
31
34
32
import com .google .gson .Gson ;
35
33
import com .google .gson .JsonArray ;
36
34
import com .google .gson .JsonElement ;
37
- import com .google .gson .JsonIOException ;
38
35
39
36
public class CompilationDatabaseGenerationTest extends AbstractBuilderTest {
40
37
41
38
/**
42
39
* Tests generation of compile_commands.json in "build" folder
43
- * @throws CoreException
44
40
*/
45
41
@ Test
46
- public void testCompilationDatabaseGeneration () throws CoreException {
42
+ public void testCompilationDatabaseGeneration () throws Exception {
47
43
setWorkspace ("regressions" );
48
44
final IProject app = loadProject ("helloworldC" );
49
- isGenerateFileOptionEnabled (true );
45
+ setGenerateFileOptionEnabled (true );
50
46
app .build (IncrementalProjectBuilder .FULL_BUILD , null );
51
47
IFile compilationDatabase = app .getFile ("build/compile_commands.json" );
52
48
assertTrue (compilationDatabase .exists ());
53
49
}
54
50
55
51
/**
56
52
* Tests format for compile_commands.json. JSON array is expected, containing an element for the c file
57
- * @throws JsonIOException
58
- * @throws CoreException
59
53
*/
60
54
@ Test
61
- public void testJsonFormat () throws JsonIOException , CoreException {
55
+ public void testJsonFormat () throws Exception {
62
56
setWorkspace ("regressions" );
63
57
final IProject app = loadProject ("helloworldC" );
64
- isGenerateFileOptionEnabled (true );
58
+ setGenerateFileOptionEnabled (true );
65
59
app .build (IncrementalProjectBuilder .FULL_BUILD , null );
66
60
IFile commandsFile = app .getFile ("build/compile_commands.json" );
67
61
if (commandsFile .exists ()) {
68
62
69
63
try (FileReader reader = new FileReader (commandsFile .getLocation ().toFile ())) {
70
64
Gson gson = new Gson ();
71
65
JsonArray jsonArray = gson .fromJson (reader , JsonArray .class );
72
- System .out .println (jsonArray );
73
66
for (JsonElement element : jsonArray ) {
74
67
CompilationDatabaseInformation compileCommand = gson .fromJson (element ,
75
68
CompilationDatabaseInformation .class );
@@ -80,23 +73,20 @@ public void testJsonFormat() throws JsonIOException, CoreException {
80
73
assertTrue (compileCommand .file ().endsWith ("src/helloworldC.c" ));
81
74
}
82
75
83
- } catch (IOException e ) {
84
- assertTrue (false );
85
76
}
86
77
87
78
}
88
79
}
89
80
90
81
/**
91
82
* Test that compile_commands.json is correctly generated when more than one .c file is present as a source file
92
- * @throws CoreException
93
83
*/
94
84
@ Test
95
- public void testMultipleFiles () throws CoreException {
85
+ public void testMultipleFiles () throws Exception {
96
86
setWorkspace ("regressions" );
97
87
final IProject app = loadProject ("helloworldC" );
98
88
IFile aFile = ManagedBuildTestHelper .createFile (app , "src/newFile.c" );
99
- isGenerateFileOptionEnabled (true );
89
+ setGenerateFileOptionEnabled (true );
100
90
app .build (IncrementalProjectBuilder .FULL_BUILD , null );
101
91
IFile commandsFile = app .getFile ("build/compile_commands.json" );
102
92
int numberOfElementsFound = 0 ;
@@ -120,31 +110,24 @@ public void testMultipleFiles() throws CoreException {
120
110
assertEquals (2 , numberOfElementsFound );
121
111
assertTrue (helloworldCIsPresent );
122
112
assertTrue (newFileIsPresent );
123
- } catch (IOException e ) {
124
- assertTrue (false );
125
113
}
126
-
127
114
}
128
115
129
116
/**
130
117
* Tests that cpp files are handled by compile_commands.json file generator
131
- * @throws CoreException
132
118
*/
133
119
@ Test
134
- @ Ignore ("This will be temporary skipped due to builder error" )
135
- public void isCPPFileAllowed () throws CoreException {
120
+ public void isCPPFileAllowed () throws Exception {
136
121
setWorkspace ("regressions" );
137
122
final IProject app = loadProject ("helloworldCPP" );
138
- isGenerateFileOptionEnabled (true );
123
+ setGenerateFileOptionEnabled (true );
139
124
app .build (IncrementalProjectBuilder .FULL_BUILD , null );
140
- System .out .println (app .getLocation ());
141
125
IFile commandsFile = app .getFile ("build/compile_commands.json" );
142
126
if (commandsFile .exists ()) {
143
127
144
128
try (FileReader reader = new FileReader (commandsFile .getLocation ().toFile ())) {
145
129
Gson gson = new Gson ();
146
130
JsonArray jsonArray = gson .fromJson (reader , JsonArray .class );
147
- System .out .println (jsonArray );
148
131
for (JsonElement element : jsonArray ) {
149
132
CompilationDatabaseInformation compileCommand = gson .fromJson (element ,
150
133
CompilationDatabaseInformation .class );
@@ -155,35 +138,33 @@ public void isCPPFileAllowed() throws CoreException {
155
138
assertTrue (compileCommand .file ().endsWith ("src/helloworldCPP.cpp" ));
156
139
}
157
140
158
- } catch (IOException e ) {
159
- assertTrue (false );
160
141
}
161
142
}
162
143
}
163
144
164
145
/**
165
146
* Tests that compilation database is not generated when feature is disabled
166
- * @throws CoreException
167
147
*/
168
148
@ Test
169
- public void testCompilationDatabaseGenerationNotEnabled () throws CoreException {
149
+ public void testCompilationDatabaseGenerationNotEnabled () throws Exception {
170
150
setWorkspace ("regressions" );
171
151
final IProject app = loadProject ("helloworldC" );
172
- isGenerateFileOptionEnabled (false );
152
+ setGenerateFileOptionEnabled (false );
173
153
app .build (IncrementalProjectBuilder .FULL_BUILD , null );
174
154
IFile compilationDatabase = app .getFile ("build/compile_commands.json" );
175
155
assertFalse (compilationDatabase .exists ());
176
156
}
177
157
178
- public static boolean isGenerateFileOptionEnabled (boolean value ) {
179
- try {
180
- IPreferenceStore preferenceStore = new ScopedPreferenceStore (InstanceScope .INSTANCE ,
181
- "org.eclipse.cdt.managedbuilder.ui" ); //$NON-NLS-1$
182
- preferenceStore .setDefault ("generateFile" , value );
183
- return preferenceStore .getBoolean ("generateFile" );
184
- } catch (Exception e ) {
185
- ManagedBuilderCorePlugin .log (e );
186
- }
187
- return false ;
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 );
188
169
}
189
170
}
0 commit comments