Skip to content

Commit

Permalink
Issue checkstyle#18: Refine DiffParserTest
Browse files Browse the repository at this point in the history
  • Loading branch information
Luolc authored and rnveach committed Jul 6, 2017
1 parent befb36a commit f924b17
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 18 deletions.
141 changes: 123 additions & 18 deletions src/test/java/com/github/checkstyle/regression/git/DiffParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,25 @@
import static org.junit.Assert.assertEquals;

import java.io.File;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.PosixFilePermission;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.SystemUtils;
import org.eclipse.jgit.lib.Repository;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.github.checkstyle.regression.data.GitChange;
import com.github.checkstyle.regression.data.ImmutableGitChange;
import com.github.checkstyle.regression.internal.GitUtils;

public class DiffParserTest {
private Repository repository;

@Before
public void setUp() throws Exception {
repository = GitUtils.createNewRepository();
final File helloWorld = GitUtils.addAnEmptyFileAndCommit(repository, "HelloWorld");
GitUtils.createNewBranchAndCheckout(repository, "foo");
Files.write(helloWorld.toPath(), "hello world!".getBytes(), StandardOpenOption.APPEND);
GitUtils.addAllAndCommit(repository, "append text to HelloWorld");
}

@After
public void tearDown() throws Exception {
repository.close();
GitUtils.clearTempRepositories();
}

Expand All @@ -60,11 +51,37 @@ public void testIsProperUtilsClass() throws Exception {
}

@Test
public void testParse() throws Exception {
final List<GitChange> changes = DiffParser.parse(
repository.getDirectory().getParent(), "foo");
assertEquals(1, changes.size());
assertEquals("HelloWorld", changes.iterator().next().path());
public void testParseAddChange() throws Exception {
try (Repository repository = GitUtils.createNewRepository()) {
GitUtils.addAnEmptyFileAndCommit(repository, "HelloWorld");
GitUtils.createNewBranchAndCheckout(repository, "foo");
GitUtils.addAnEmptyFileAndCommit(repository, "AddedFile");
final List<GitChange> changes = DiffParser.parse(
repository.getDirectory().getParent(), "foo");
assertEquals("There should be 1 change detected", 1, changes.size());
final GitChange expected = ImmutableGitChange.builder()
.path("AddedFile")
.build();
assertEquals("The change is not as expected", expected, changes.get(0));
}
}

@Test
public void testParseModifyChange() throws Exception {
try (Repository repository = GitUtils.createNewRepository()) {
final File helloWorld = GitUtils.addAnEmptyFileAndCommit(repository, "HelloWorld");
GitUtils.createNewBranchAndCheckout(repository, "foo");
Files.write(helloWorld.toPath(), "hello world!".getBytes(Charset.forName("UTF-8")),
StandardOpenOption.APPEND);
GitUtils.addAllAndCommit(repository, "append text to HelloWorld");
final List<GitChange> changes = DiffParser.parse(
repository.getDirectory().getParent(), "foo");
assertEquals("There should be 1 change detected", 1, changes.size());
final GitChange expected = ImmutableGitChange.builder()
.path("HelloWorld")
.build();
assertEquals("The change is not as expected", expected, changes.get(0));
}
}

@Test
Expand All @@ -80,6 +97,50 @@ public void testParseDeleteChange() throws Exception {
}
}

@Test
public void testParseRenameChange() throws Exception {
try (Repository repository = GitUtils.createNewRepository()) {
GitUtils.addAnEmptyFileAndCommit(repository, "HelloWorld");
GitUtils.createNewBranchAndCheckout(repository, "foo");
GitUtils.addAnEmptyFileAndCommit(repository, "HelloWorldFoo");
GitUtils.removeFileAndCommit(repository, "HelloWorld");
final List<GitChange> changes = DiffParser.parse(
repository.getDirectory().getParent(), "foo");
assertEquals("There should be 1 change detected", 1, changes.size());
final GitChange expected = ImmutableGitChange.builder()
.path("HelloWorldFoo")
.build();
assertEquals("The change is not as expected", expected, changes.get(0));
}
}

@Test
public void testParseCopyChange() throws Exception {
try (Repository repository = GitUtils.createNewRepository()) {
GitUtils.addAnEmptyFileAndCommit(repository, "a.txt");
GitUtils.createNewBranchAndCheckout(repository, "foo");
GitUtils.addAnEmptyFileAndCommit(repository, "b.txt");
GitUtils.addAnEmptyFileAndCommit(repository, "src/com/foo/c.java");
GitUtils.addAnEmptyFileAndCommit(repository, "src/com/foo/d.java");
GitUtils.removeFileAndCommit(repository, "a.txt");
final List<GitChange> changes = DiffParser.parse(
repository.getDirectory().getParent(), "foo");
assertEquals("There should be 3 change detected", 3, changes.size());
final GitChange expected0 = ImmutableGitChange.builder()
.path("b.txt")
.build();
assertEquals("The change is not as expected", expected0, changes.get(0));
final GitChange expected1 = ImmutableGitChange.builder()
.path("src/com/foo/c.java")
.build();
assertEquals("The change is not as expected", expected1, changes.get(1));
final GitChange expected2 = ImmutableGitChange.builder()
.path("src/com/foo/d.java")
.build();
assertEquals("The change is not as expected", expected2, changes.get(2));
}
}

@Test
public void testParsePrBranchBehindMaster() throws Exception {
try (Repository repository = GitUtils.createNewRepository()) {
Expand All @@ -98,4 +159,48 @@ public void testParsePrBranchBehindMaster() throws Exception {
expected, changes.get(0));
}
}

@Test
public void testParsePrBranchWithMultipleCommits() throws Exception {
try (Repository repository = GitUtils.createNewRepository()) {
GitUtils.addAnEmptyFileAndCommit(repository, "HelloWorld");
GitUtils.createNewBranchAndCheckout(repository, "foo");
GitUtils.addAnEmptyFileAndCommit(repository, "AddInCommit1");
GitUtils.addAnEmptyFileAndCommit(repository, "AddInCommit2");
final List<GitChange> changes = DiffParser.parse(
repository.getDirectory().getParent(), "foo");
assertEquals("There should be 2 change detected", 2, changes.size());
final GitChange expected0 = ImmutableGitChange.builder()
.path("AddInCommit1")
.build();
assertEquals("The change is not as expected", expected0, changes.get(0));
final GitChange expected1 = ImmutableGitChange.builder()
.path("AddInCommit2")
.build();
assertEquals("The change is not as expected", expected1, changes.get(1));
}
}

@Test
public void testParseFilePermissionChange() throws Exception {
// Skip this UT on Windows
if (!SystemUtils.IS_OS_WINDOWS) {
try (Repository repository = GitUtils.createNewRepository()) {
final File file = GitUtils.addAnEmptyFileAndCommit(repository, "HelloWorld");
GitUtils.createNewBranchAndCheckout(repository, "foo");
final Set<PosixFilePermission> permissions =
Files.getPosixFilePermissions(file.toPath());
permissions.add(PosixFilePermission.OWNER_EXECUTE);
Files.setPosixFilePermissions(file.toPath(), permissions);
GitUtils.addAllAndCommit(repository, "Change file mode");
final List<GitChange> changes = DiffParser.parse(
repository.getDirectory().getParent(), "foo");
assertEquals("There should be 1 change detected", 1, changes.size());
final GitChange expected = ImmutableGitChange.builder()
.path("HelloWorld")
.build();
assertEquals("The change is not as expected", expected, changes.get(0));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@

import org.apache.commons.io.FileUtils;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.RmCommand;
import org.eclipse.jgit.api.Status;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;

import com.google.common.collect.Iterables;

/**
* Contains utility methods for git component test.
* @author LuoLiangchen
Expand Down Expand Up @@ -76,6 +80,11 @@ public static File addAnEmptyFileAndCommit(Repository repository, String fileNam
throws IOException, GitAPIException {
try (Git git = new Git(repository)) {
final File file = new File(repository.getDirectory().getParent(), fileName);
if (!file.getParentFile().exists()) {
if (!file.getParentFile().mkdirs()) {
throw new IOException("Could not create directory " + file.getParentFile());
}
}
if (!file.createNewFile()) {
throw new IOException("Could not create file " + file);
}
Expand All @@ -88,6 +97,16 @@ public static File addAnEmptyFileAndCommit(Repository repository, String fileNam
public static void addAllAndCommit(Repository repository, String message)
throws GitAPIException {
try (Git git = new Git(repository)) {
// In JGit, the "add ." could not add the deleted files into staging area.
// To obtain the same behavior as git CLI command "git add .", we have to
// use RmCommand to handle deleted files.
final Status status = git.status().call();
if (!status.getMissing().isEmpty() || !status.getRemoved().isEmpty()) {
final RmCommand rm = git.rm().setCached(true);
Iterables.concat(status.getMissing(), status.getRemoved())
.forEach(rm::addFilepattern);
rm.call();
}
git.add().addFilepattern(".").call();
git.commit().setMessage(message).call();
}
Expand Down

0 comments on commit f924b17

Please sign in to comment.