18
18
*/
19
19
20
20
/*
21
- * Copyright (c) 2009, 2021 , Oracle and/or its affiliates. All rights reserved.
21
+ * Copyright (c) 2009, 2023 , Oracle and/or its affiliates. All rights reserved.
22
22
* Portions Copyright (c) 2017, Chris Fraire <[email protected] >.
23
23
*/
24
24
package org .opengrok .indexer .history ;
25
25
26
26
import org .junit .jupiter .api .AfterEach ;
27
27
import org .junit .jupiter .api .BeforeEach ;
28
28
import org .junit .jupiter .api .Test ;
29
+ import org .junit .jupiter .params .ParameterizedTest ;
30
+ import org .junit .jupiter .params .provider .ValueSource ;
29
31
import org .opengrok .indexer .condition .EnabledForRepository ;
30
32
import org .opengrok .indexer .configuration .RuntimeEnvironment ;
31
33
import org .opengrok .indexer .util .Executor ;
@@ -81,13 +83,16 @@ public class MercurialRepositoryTest {
81
83
82
84
private TestRepository repository ;
83
85
86
+ private File repositoryRoot ;
87
+
84
88
/**
85
89
* Set up a test repository. Should be called by the tests that need it. The
86
90
* test repository will be destroyed automatically when the test finishes.
87
91
*/
88
92
private void setUpTestRepository () throws IOException , URISyntaxException {
89
93
repository = new TestRepository ();
90
94
repository .create (getClass ().getResource ("/repositories" ));
95
+ repositoryRoot = new File (repository .getSourceRoot (), "mercurial" );
91
96
}
92
97
93
98
@ BeforeEach
@@ -122,29 +127,25 @@ public void testGetHistory() throws Exception {
122
127
123
128
@ Test
124
129
public void testGetHistorySubdir () throws Exception {
125
- File root = new File (repository .getSourceRoot (), "mercurial" );
126
-
127
130
// Add a subdirectory with some history.
128
- runHgCommand (root , "import" ,
131
+ runHgCommand (repositoryRoot , "import" ,
129
132
Paths .get (getClass ().getResource ("/history/hg-export-subdir.txt" ).toURI ()).toString ());
130
133
131
- MercurialRepository mr = (MercurialRepository ) RepositoryFactory .getRepository (root );
132
- History hist = mr .getHistory (new File (root , "subdir" ));
134
+ MercurialRepository mr = (MercurialRepository ) RepositoryFactory .getRepository (repositoryRoot );
135
+ History hist = mr .getHistory (new File (repositoryRoot , "subdir" ));
133
136
List <HistoryEntry > entries = hist .getHistoryEntries ();
134
137
assertEquals (1 , entries .size ());
135
138
}
136
139
137
140
/**
138
141
* Test that subset of changesets can be extracted based on penultimate
139
142
* revision number. This works for directories only.
140
- * @throws Exception
141
143
*/
142
144
@ Test
143
145
public void testGetHistoryPartial () throws Exception {
144
- File root = new File (repository .getSourceRoot (), "mercurial" );
145
- MercurialRepository mr = (MercurialRepository ) RepositoryFactory .getRepository (root );
146
+ MercurialRepository mr = (MercurialRepository ) RepositoryFactory .getRepository (repositoryRoot );
146
147
// Get all but the oldest revision.
147
- History hist = mr .getHistory (root , REVISIONS [REVISIONS .length - 1 ]);
148
+ History hist = mr .getHistory (repositoryRoot , REVISIONS [REVISIONS .length - 1 ]);
148
149
List <HistoryEntry > entries = hist .getHistoryEntries ();
149
150
assertEquals (REVISIONS .length - 1 , entries .size ());
150
151
for (int i = 0 ; i < entries .size (); i ++) {
@@ -171,7 +172,7 @@ public static void runHgCommand(File reposRoot, String... args) {
171
172
172
173
Executor exec = new Executor (cmdargs , reposRoot );
173
174
int exitCode = exec .exec ();
174
- assertEquals (0 , exitCode , "hg command '" + cmdargs . toString () + "' failed."
175
+ assertEquals (0 , exitCode , "hg command '" + cmdargs + "' failed."
175
176
+ "\n exit code: " + exitCode
176
177
+ "\n stdout:\n " + exec .getOutputString ()
177
178
+ "\n stderr:\n " + exec .getErrorString ());
@@ -180,25 +181,21 @@ public static void runHgCommand(File reposRoot, String... args) {
180
181
/**
181
182
* Test that history of branched repository contains changesets of the
182
183
* default branch as well.
183
- * @throws Exception
184
184
*/
185
185
@ Test
186
186
public void testGetHistoryBranch () throws Exception {
187
- File root = new File (repository .getSourceRoot (), "mercurial" );
188
-
189
187
// Branch the repo and add one changeset.
190
- runHgCommand (root , "unbundle" ,
188
+ runHgCommand (repositoryRoot , "unbundle" ,
191
189
Paths .get (getClass ().getResource ("/history/hg-branch.bundle" ).toURI ()).toString ());
192
190
// Switch to the branch.
193
- runHgCommand (root , "update" , "mybranch" );
191
+ runHgCommand (repositoryRoot , "update" , "mybranch" );
194
192
195
193
// Since the above hg commands change the active branch the repository
196
194
// needs to be initialized here so that its branch matches.
197
- MercurialRepository mr
198
- = (MercurialRepository ) RepositoryFactory .getRepository (root );
195
+ MercurialRepository mr = (MercurialRepository ) RepositoryFactory .getRepository (repositoryRoot );
199
196
200
197
// Get all revisions.
201
- History hist = mr .getHistory (root );
198
+ History hist = mr .getHistory (repositoryRoot );
202
199
List <HistoryEntry > entries = hist .getHistoryEntries ();
203
200
List <String > both = new ArrayList <>(REVISIONS .length
204
201
+ REVISIONS_extra_branch .length );
@@ -218,7 +215,7 @@ public void testGetHistoryBranch() throws Exception {
218
215
}
219
216
220
217
// Get revisions starting with given changeset before the repo was branched.
221
- hist = mr .getHistory (root , "8:6a8c423f5624" );
218
+ hist = mr .getHistory (repositoryRoot , "8:6a8c423f5624" );
222
219
entries = hist .getHistoryEntries ();
223
220
assertEquals (2 , entries .size ());
224
221
assertEquals (REVISIONS_extra_branch [0 ], entries .get (0 ).getRevision ());
@@ -227,12 +224,10 @@ public void testGetHistoryBranch() throws Exception {
227
224
228
225
/**
229
226
* Test that contents of last revision of a text file match expected content.
230
- * @throws java.lang.Exception
231
227
*/
232
228
@ Test
233
229
public void testGetHistoryGet () throws Exception {
234
- File root = new File (repository .getSourceRoot (), "mercurial" );
235
- MercurialRepository mr = (MercurialRepository ) RepositoryFactory .getRepository (root );
230
+ MercurialRepository mr = (MercurialRepository ) RepositoryFactory .getRepository (repositoryRoot );
236
231
String exp_str = "This will be a first novel of mine.\n "
237
232
+ "\n "
238
233
+ "Chapter 1.\n "
@@ -242,7 +237,7 @@ public void testGetHistoryGet() throws Exception {
242
237
+ "...\n " ;
243
238
byte [] buffer = new byte [1024 ];
244
239
245
- InputStream input = mr .getHistoryGet (root .getCanonicalPath (),
240
+ InputStream input = mr .getHistoryGet (repositoryRoot .getCanonicalPath (),
246
241
"novel.txt" , REVISIONS [0 ]);
247
242
assertNotNull (input );
248
243
@@ -257,37 +252,32 @@ public void testGetHistoryGet() throws Exception {
257
252
258
253
/**
259
254
* Test that it is possible to get contents of multiple revisions of a file.
260
- * @throws java.lang.Exception
261
255
*/
262
256
@ Test
263
257
public void testgetHistoryGetForAll () throws Exception {
264
- File root = new File (repository .getSourceRoot (), "mercurial" );
265
- MercurialRepository mr = (MercurialRepository ) RepositoryFactory .getRepository (root );
258
+ MercurialRepository mr = (MercurialRepository ) RepositoryFactory .getRepository (repositoryRoot );
266
259
267
260
for (String rev : REVISIONS_novel ) {
268
- InputStream input = mr .getHistoryGet (root .getCanonicalPath (),
261
+ InputStream input = mr .getHistoryGet (repositoryRoot .getCanonicalPath (),
269
262
"novel.txt" , rev );
270
263
assertNotNull (input );
271
264
}
272
265
}
273
266
274
267
/**
275
- * Test that {@code getHistoryGet()} returns historical contents of renamed
276
- * file.
277
- * @throws java.lang.Exception
268
+ * Test that {@code getHistoryGet()} returns historical contents of renamed file.
278
269
*/
279
270
@ Test
280
271
public void testGetHistoryGetRenamed () throws Exception {
281
- File root = new File (repository .getSourceRoot (), "mercurial" );
282
- MercurialRepository mr = (MercurialRepository ) RepositoryFactory .getRepository (root );
272
+ MercurialRepository mr = (MercurialRepository ) RepositoryFactory .getRepository (repositoryRoot );
283
273
String exp_str = "This is totally plaintext file.\n " ;
284
274
byte [] buffer = new byte [1024 ];
285
275
286
276
/*
287
277
* In our test repository the file was renamed twice since
288
278
* revision 3.
289
279
*/
290
- InputStream input = mr .getHistoryGet (root .getCanonicalPath (),
280
+ InputStream input = mr .getHistoryGet (repositoryRoot .getCanonicalPath (),
291
281
"novel.txt" , "3" );
292
282
assert (input != null );
293
283
int len = input .read (buffer );
@@ -299,12 +289,10 @@ public void testGetHistoryGetRenamed() throws Exception {
299
289
/**
300
290
* Test that {@code getHistory()} throws an exception if the revision
301
291
* argument doesn't match any of the revisions in the history.
302
- * @throws java.lang.Exception
303
292
*/
304
293
@ Test
305
294
public void testGetHistoryWithNoSuchRevision () throws Exception {
306
- File root = new File (repository .getSourceRoot (), "mercurial" );
307
- MercurialRepository mr = (MercurialRepository ) RepositoryFactory .getRepository (root );
295
+ MercurialRepository mr = (MercurialRepository ) RepositoryFactory .getRepository (repositoryRoot );
308
296
309
297
// Get the sequence number and the hash from one of the revisions.
310
298
String [] revisionParts = REVISIONS [1 ].split (":" );
@@ -314,14 +302,13 @@ public void testGetHistoryWithNoSuchRevision() throws Exception {
314
302
315
303
// Construct a revision identifier that doesn't exist.
316
304
String constructedRevision = (number + 1 ) + ":" + hash ;
317
- assertThrows (HistoryException .class , () -> mr .getHistory (root , constructedRevision ));
305
+ assertThrows (HistoryException .class , () -> mr .getHistory (repositoryRoot , constructedRevision ));
318
306
}
319
307
320
308
@ Test
321
309
void testGetHistorySinceTillNullNull () throws Exception {
322
- File root = new File (repository .getSourceRoot (), "mercurial" );
323
- MercurialRepository hgRepo = (MercurialRepository ) RepositoryFactory .getRepository (root );
324
- History history = hgRepo .getHistory (root , null , null );
310
+ MercurialRepository hgRepo = (MercurialRepository ) RepositoryFactory .getRepository (repositoryRoot );
311
+ History history = hgRepo .getHistory (repositoryRoot , null , null );
325
312
assertNotNull (history );
326
313
assertNotNull (history .getHistoryEntries ());
327
314
assertEquals (10 , history .getHistoryEntries ().size ());
@@ -332,9 +319,8 @@ void testGetHistorySinceTillNullNull() throws Exception {
332
319
333
320
@ Test
334
321
void testGetHistorySinceTillNullRev () throws Exception {
335
- File root = new File (repository .getSourceRoot (), "mercurial" );
336
- MercurialRepository hgRepo = (MercurialRepository ) RepositoryFactory .getRepository (root );
337
- History history = hgRepo .getHistory (root , null , REVISIONS [4 ]);
322
+ MercurialRepository hgRepo = (MercurialRepository ) RepositoryFactory .getRepository (repositoryRoot );
323
+ History history = hgRepo .getHistory (repositoryRoot , null , REVISIONS [4 ]);
338
324
assertNotNull (history );
339
325
assertNotNull (history .getHistoryEntries ());
340
326
assertEquals (6 , history .getHistoryEntries ().size ());
@@ -345,9 +331,8 @@ void testGetHistorySinceTillNullRev() throws Exception {
345
331
346
332
@ Test
347
333
void testGetHistorySinceTillRevNull () throws Exception {
348
- File root = new File (repository .getSourceRoot (), "mercurial" );
349
- MercurialRepository hgRepo = (MercurialRepository ) RepositoryFactory .getRepository (root );
350
- History history = hgRepo .getHistory (root , REVISIONS [3 ], null );
334
+ MercurialRepository hgRepo = (MercurialRepository ) RepositoryFactory .getRepository (repositoryRoot );
335
+ History history = hgRepo .getHistory (repositoryRoot , REVISIONS [3 ], null );
351
336
assertNotNull (history );
352
337
assertNotNull (history .getHistoryEntries ());
353
338
assertEquals (3 , history .getHistoryEntries ().size ());
@@ -358,9 +343,8 @@ void testGetHistorySinceTillRevNull() throws Exception {
358
343
359
344
@ Test
360
345
void testGetHistorySinceTillRevRev () throws Exception {
361
- File root = new File (repository .getSourceRoot (), "mercurial" );
362
- MercurialRepository hgRepo = (MercurialRepository ) RepositoryFactory .getRepository (root );
363
- History history = hgRepo .getHistory (root , REVISIONS [7 ], REVISIONS [2 ]);
346
+ MercurialRepository hgRepo = (MercurialRepository ) RepositoryFactory .getRepository (repositoryRoot );
347
+ History history = hgRepo .getHistory (repositoryRoot , REVISIONS [7 ], REVISIONS [2 ]);
364
348
assertNotNull (history );
365
349
assertNotNull (history .getHistoryEntries ());
366
350
assertEquals (5 , history .getHistoryEntries ().size ());
@@ -372,10 +356,9 @@ void testGetHistorySinceTillRevRev() throws Exception {
372
356
@ Test
373
357
void testGetHistoryRenamedFileTillRev () throws Exception {
374
358
RuntimeEnvironment .getInstance ().setHandleHistoryOfRenamedFiles (true );
375
- File root = new File (repository .getSourceRoot (), "mercurial" );
376
- File file = new File (root , "novel.txt" );
359
+ File file = new File (repositoryRoot , "novel.txt" );
377
360
assertTrue (file .exists () && file .isFile ());
378
- MercurialRepository hgRepo = (MercurialRepository ) RepositoryFactory .getRepository (root );
361
+ MercurialRepository hgRepo = (MercurialRepository ) RepositoryFactory .getRepository (repositoryRoot );
379
362
History history = hgRepo .getHistory (file , null , "7:db1394c05268" );
380
363
assertNotNull (history );
381
364
assertNotNull (history .getHistoryEntries ());
@@ -387,12 +370,39 @@ void testGetHistoryRenamedFileTillRev() throws Exception {
387
370
388
371
@ Test
389
372
void testGetLastHistoryEntry () throws Exception {
390
- File root = new File (repository .getSourceRoot (), "mercurial" );
391
- File file = new File (root , "novel.txt" );
373
+ File file = new File (repositoryRoot , "novel.txt" );
392
374
assertTrue (file .exists () && file .isFile ());
393
- MercurialRepository hgRepo = (MercurialRepository ) RepositoryFactory .getRepository (root );
375
+ MercurialRepository hgRepo = (MercurialRepository ) RepositoryFactory .getRepository (repositoryRoot );
394
376
HistoryEntry historyEntry = hgRepo .getLastHistoryEntry (file , true );
395
377
assertNotNull (historyEntry );
396
378
assertEquals ("8:6a8c423f5624" , historyEntry .getRevision ());
397
379
}
380
+
381
+ @ ParameterizedTest
382
+ @ ValueSource (booleans = {true , false })
383
+ void testMergeCommits (boolean isMergeCommitsEnabled ) throws Exception {
384
+ // The bundle will add a branch and merge commit in the default branch.
385
+ runHgCommand (repositoryRoot , "unbundle" ,
386
+ Paths .get (getClass ().getResource ("/history/hg-merge.bundle" ).toURI ()).toString ());
387
+ runHgCommand (repositoryRoot , "update" );
388
+
389
+ RuntimeEnvironment env = RuntimeEnvironment .getInstance ();
390
+ boolean isMergeCommitsEnabledOrig = env .isMergeCommitsEnabled ();
391
+ env .setMergeCommitsEnabled (isMergeCommitsEnabled );
392
+
393
+ MercurialRepository hgRepo = (MercurialRepository ) RepositoryFactory .getRepository (repositoryRoot );
394
+ History history = hgRepo .getHistory (repositoryRoot , null );
395
+ assertNotNull (history );
396
+ assertNotNull (history .getHistoryEntries ());
397
+ if (isMergeCommitsEnabled ) {
398
+ assertEquals (12 , history .getHistoryEntries ().size ());
399
+ assertNotNull (history .getLastHistoryEntry ());
400
+ assertEquals ("merge" , history .getLastHistoryEntry ().getMessage ());
401
+ } else {
402
+ assertEquals (11 , history .getHistoryEntries ().size ());
403
+ }
404
+
405
+ // Cleanup.
406
+ env .setMergeCommitsEnabled (isMergeCommitsEnabledOrig );
407
+ }
398
408
}
0 commit comments