Skip to content

Commit

Permalink
add tests for MercurialRepository.annotate() (#4627)
Browse files Browse the repository at this point in the history
  • Loading branch information
vladak authored Aug 2, 2024
1 parent 0725498 commit 9fd1f26
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/*
* Copyright (c) 2006, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2006, 2024, Oracle and/or its affiliates. All rights reserved.
* Portions Copyright (c) 2017, 2019, Chris Fraire <[email protected]>.
*/
package org.opengrok.indexer.history;
Expand All @@ -32,6 +32,7 @@
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.TreeSet;
import java.util.function.Consumer;
import java.util.function.Supplier;
Expand Down Expand Up @@ -457,6 +458,7 @@ boolean getHistoryGet(OutputStream out, String parent, String basename, String r
* @throws java.io.IOException if I/O exception occurred
*/
@Override
@Nullable
public Annotation annotate(File file, String revision) throws IOException {
ArrayList<String> argv = new ArrayList<>();
ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK);
Expand All @@ -483,6 +485,11 @@ public Annotation annotate(File file, String revision) throws IOException {
// needed later to get user string for particular revision.
try {
History hist = HistoryGuru.getInstance().getHistory(file, false);
if (Objects.isNull(hist)) {
LOGGER.log(Level.SEVERE,
"Error: cannot get history for file ''{0}''", file);
return null;
}
for (HistoryEntry e : hist.getHistoryEntries()) {
// Chop out the colon and all hexadecimal what follows.
// This is because the whole changeset identification is
Expand All @@ -492,7 +499,7 @@ public Annotation annotate(File file, String revision) throws IOException {
}
} catch (HistoryException he) {
LOGGER.log(Level.SEVERE,
"Error: cannot get history for file {0}", file);
"Error: cannot get history for file ''{0}''", file);
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@
*/

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

import org.apache.commons.lang3.tuple.Pair;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.opengrok.indexer.condition.EnabledForRepository;
import org.opengrok.indexer.configuration.RuntimeEnvironment;
Expand All @@ -43,11 +45,14 @@
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.opengrok.indexer.condition.RepositoryInstalled.Type.MERCURIAL;
Expand Down Expand Up @@ -255,7 +260,7 @@ void testGetHistoryGet() throws Exception {
* Test that it is possible to get contents of multiple revisions of a file.
*/
@Test
void testgetHistoryGetForAll() throws Exception {
void testGetHistoryGetForAll() throws Exception {
MercurialRepository mr = (MercurialRepository) RepositoryFactory.getRepository(repositoryRoot);

for (String rev : REVISIONS_novel) {
Expand Down Expand Up @@ -406,4 +411,34 @@ void testMergeCommits(boolean isMergeCommitsEnabled) throws Exception {
// Cleanup.
env.setMergeCommitsEnabled(isMergeCommitsEnabledOrig);
}

@Test
void testAnnotationNegative() throws Exception {
MercurialRepository hgRepo = (MercurialRepository) RepositoryFactory.getRepository(repositoryRoot);
File file = new File(repositoryRoot, "nonexistent");
assertFalse(file.exists());
Annotation annotation = hgRepo.annotate(file, null);
assertNull(annotation);
}

private static Stream<Pair<String, List<String>>> provideParametersForPositiveAnnotationTest() {
return Stream.of(Pair.of("8:6a8c423f5624", List.of("7", "8")),
Pair.of("7:db1394c05268", List.of("7")));
}

@ParameterizedTest
@MethodSource("provideParametersForPositiveAnnotationTest")
void testAnnotationPositive(Pair<String, List<String>> pair) throws Exception {
MercurialRepository hgRepo = (MercurialRepository) RepositoryFactory.getRepository(repositoryRoot);
assertNotNull(hgRepo);
File file = new File(repositoryRoot, "novel.txt");
assertTrue(file.exists());
// The annotate() method calls uses HistoryGuru's getHistory() method which requires the RepositoryLookup
// to be initialized. Do so via setRepositories().
RuntimeEnvironment.getInstance().setRepositories(repository.getSourceRoot());
Annotation annotation = hgRepo.annotate(file, pair.getKey());
assertNotNull(annotation);
List<String> revisions = new ArrayList<>(annotation.getRevisions());
assertEquals(pair.getValue(), revisions);
}
}

0 comments on commit 9fd1f26

Please sign in to comment.