Skip to content

Commit 9fd1f26

Browse files
authored
add tests for MercurialRepository.annotate() (#4627)
1 parent 0725498 commit 9fd1f26

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/history/MercurialRepository.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2006, 2023, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2006, 2024, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2017, 2019, Chris Fraire <[email protected]>.
2323
*/
2424
package org.opengrok.indexer.history;
@@ -32,6 +32,7 @@
3232
import java.util.Arrays;
3333
import java.util.HashMap;
3434
import java.util.List;
35+
import java.util.Objects;
3536
import java.util.TreeSet;
3637
import java.util.function.Consumer;
3738
import java.util.function.Supplier;
@@ -457,6 +458,7 @@ boolean getHistoryGet(OutputStream out, String parent, String basename, String r
457458
* @throws java.io.IOException if I/O exception occurred
458459
*/
459460
@Override
461+
@Nullable
460462
public Annotation annotate(File file, String revision) throws IOException {
461463
ArrayList<String> argv = new ArrayList<>();
462464
ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK);
@@ -483,6 +485,11 @@ public Annotation annotate(File file, String revision) throws IOException {
483485
// needed later to get user string for particular revision.
484486
try {
485487
History hist = HistoryGuru.getInstance().getHistory(file, false);
488+
if (Objects.isNull(hist)) {
489+
LOGGER.log(Level.SEVERE,
490+
"Error: cannot get history for file ''{0}''", file);
491+
return null;
492+
}
486493
for (HistoryEntry e : hist.getHistoryEntries()) {
487494
// Chop out the colon and all hexadecimal what follows.
488495
// This is because the whole changeset identification is
@@ -492,7 +499,7 @@ public Annotation annotate(File file, String revision) throws IOException {
492499
}
493500
} catch (HistoryException he) {
494501
LOGGER.log(Level.SEVERE,
495-
"Error: cannot get history for file {0}", file);
502+
"Error: cannot get history for file ''{0}''", file);
496503
return null;
497504
}
498505

opengrok-indexer/src/test/java/org/opengrok/indexer/history/MercurialRepositoryTest.java

+37-2
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,17 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2009, 2023, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2009, 2024, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2017, Chris Fraire <[email protected]>.
2323
*/
2424
package org.opengrok.indexer.history;
2525

26+
import org.apache.commons.lang3.tuple.Pair;
2627
import org.junit.jupiter.api.AfterEach;
2728
import org.junit.jupiter.api.BeforeEach;
2829
import org.junit.jupiter.api.Test;
2930
import org.junit.jupiter.params.ParameterizedTest;
31+
import org.junit.jupiter.params.provider.MethodSource;
3032
import org.junit.jupiter.params.provider.ValueSource;
3133
import org.opengrok.indexer.condition.EnabledForRepository;
3234
import org.opengrok.indexer.configuration.RuntimeEnvironment;
@@ -43,11 +45,14 @@
4345
import java.util.Collections;
4446
import java.util.List;
4547
import java.util.stream.Collectors;
48+
import java.util.stream.Stream;
4649

4750
import static org.junit.jupiter.api.Assertions.assertEquals;
51+
import static org.junit.jupiter.api.Assertions.assertFalse;
4852
import static org.junit.jupiter.api.Assertions.assertNotEquals;
4953
import static org.junit.jupiter.api.Assertions.assertNotNull;
5054
import static org.junit.jupiter.api.Assertions.assertNotSame;
55+
import static org.junit.jupiter.api.Assertions.assertNull;
5156
import static org.junit.jupiter.api.Assertions.assertThrows;
5257
import static org.junit.jupiter.api.Assertions.assertTrue;
5358
import static org.opengrok.indexer.condition.RepositoryInstalled.Type.MERCURIAL;
@@ -255,7 +260,7 @@ void testGetHistoryGet() throws Exception {
255260
* Test that it is possible to get contents of multiple revisions of a file.
256261
*/
257262
@Test
258-
void testgetHistoryGetForAll() throws Exception {
263+
void testGetHistoryGetForAll() throws Exception {
259264
MercurialRepository mr = (MercurialRepository) RepositoryFactory.getRepository(repositoryRoot);
260265

261266
for (String rev : REVISIONS_novel) {
@@ -406,4 +411,34 @@ void testMergeCommits(boolean isMergeCommitsEnabled) throws Exception {
406411
// Cleanup.
407412
env.setMergeCommitsEnabled(isMergeCommitsEnabledOrig);
408413
}
414+
415+
@Test
416+
void testAnnotationNegative() throws Exception {
417+
MercurialRepository hgRepo = (MercurialRepository) RepositoryFactory.getRepository(repositoryRoot);
418+
File file = new File(repositoryRoot, "nonexistent");
419+
assertFalse(file.exists());
420+
Annotation annotation = hgRepo.annotate(file, null);
421+
assertNull(annotation);
422+
}
423+
424+
private static Stream<Pair<String, List<String>>> provideParametersForPositiveAnnotationTest() {
425+
return Stream.of(Pair.of("8:6a8c423f5624", List.of("7", "8")),
426+
Pair.of("7:db1394c05268", List.of("7")));
427+
}
428+
429+
@ParameterizedTest
430+
@MethodSource("provideParametersForPositiveAnnotationTest")
431+
void testAnnotationPositive(Pair<String, List<String>> pair) throws Exception {
432+
MercurialRepository hgRepo = (MercurialRepository) RepositoryFactory.getRepository(repositoryRoot);
433+
assertNotNull(hgRepo);
434+
File file = new File(repositoryRoot, "novel.txt");
435+
assertTrue(file.exists());
436+
// The annotate() method calls uses HistoryGuru's getHistory() method which requires the RepositoryLookup
437+
// to be initialized. Do so via setRepositories().
438+
RuntimeEnvironment.getInstance().setRepositories(repository.getSourceRoot());
439+
Annotation annotation = hgRepo.annotate(file, pair.getKey());
440+
assertNotNull(annotation);
441+
List<String> revisions = new ArrayList<>(annotation.getRevisions());
442+
assertEquals(pair.getValue(), revisions);
443+
}
409444
}

0 commit comments

Comments
 (0)