Skip to content

Commit f1108b5

Browse files
committed
add unit test checking for dirty files
1 parent 0cc2459 commit f1108b5

33 files changed

+171
-0
lines changed

src/test/java/pl/project13/maven/git/AvailableGitTestRepo.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public enum AvailableGitTestRepo {
2525
WITH_ONE_COMMIT("src/test/resources/_git_one_commit"),
2626
WITH_ONE_COMMIT_DIRTY("src/test/resources/_git_one_commit_dirty"),
2727
GIT_COMMIT_ID("src/test/resources/_git_of_git_commit_id"),
28+
GIT_WITH_NO_CHANGES("src/test/resources/_git_with_no_changes/_git_dir"),
29+
GIT_WITH_CHANGES("src/test/resources/_git_with_changes/_git_dir"),
2830
ON_A_TAG("src/test/resources/_git_on_a_tag"),
2931
/**
3032
* <pre>
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* This file is part of git-commit-id-plugin by Konrad Malawski <konrad.malawski@java.pl>
3+
*
4+
* git-commit-id-plugin is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* git-commit-id-plugin is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with git-commit-id-plugin. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package pl.project13.maven.git;
19+
20+
import com.google.common.collect.ImmutableList;
21+
import com.google.common.collect.Maps;
22+
import org.apache.maven.project.MavenProject;
23+
import org.eclipse.jgit.lib.Repository;
24+
import org.junit.Before;
25+
import org.junit.Test;
26+
27+
import java.io.File;
28+
import java.io.IOException;
29+
import java.util.Map;
30+
import java.util.Properties;
31+
32+
import static org.fest.assertions.Assertions.assertThat;
33+
import static org.mockito.Mockito.*;
34+
35+
/**
36+
* @author Adam Batkin
37+
*/
38+
public class GitCommitIdMojoDirtyFilesTest {
39+
40+
@Test
41+
public void testDetectCleanWorkingDirectory() throws Exception {
42+
File dotGitDirectory = AvailableGitTestRepo.GIT_WITH_NO_CHANGES.getDir();
43+
GitDescribeConfig gitDescribeConfig = new GitDescribeConfig();
44+
gitDescribeConfig.setSkip(false);
45+
46+
String prefix = "git";
47+
int abbrevLength = 7;
48+
String dateFormat = "dd.MM.yyyy '@' HH:mm:ss z";
49+
boolean verbose = true;
50+
51+
GitCommitIdMojo mojo = new GitCommitIdMojo();
52+
mojo.setDotGitDirectory(dotGitDirectory);
53+
mojo.setPrefix(prefix);
54+
mojo.setAbbrevLength(abbrevLength);
55+
mojo.setDateFormat(dateFormat);
56+
mojo.setVerbose(verbose);
57+
mojo.useNativeGit(false);
58+
mojo.setGitDescribe(gitDescribeConfig);
59+
60+
61+
mojo.runningTests = true;
62+
mojo.project = mock(MavenProject.class, RETURNS_MOCKS);
63+
when(mojo.project.getPackaging()).thenReturn("jar");
64+
65+
mojo.execute();
66+
67+
Properties properties = mojo.getProperties();
68+
69+
assertThat(properties.get("git.commit.files.dirty")).isEqualTo("false");
70+
}
71+
72+
@Test
73+
public void testDetectDirtyWorkingDirectory() throws Exception {
74+
File dotGitDirectory = AvailableGitTestRepo.GIT_WITH_CHANGES.getDir();
75+
GitDescribeConfig gitDescribeConfig = new GitDescribeConfig();
76+
gitDescribeConfig.setSkip(false);
77+
78+
String prefix = "git";
79+
int abbrevLength = 7;
80+
String dateFormat = "dd.MM.yyyy '@' HH:mm:ss z";
81+
boolean verbose = true;
82+
83+
GitCommitIdMojo mojo = new GitCommitIdMojo();
84+
mojo.setDotGitDirectory(dotGitDirectory);
85+
mojo.setPrefix(prefix);
86+
mojo.setAbbrevLength(abbrevLength);
87+
mojo.setDateFormat(dateFormat);
88+
mojo.setVerbose(verbose);
89+
mojo.useNativeGit(false);
90+
mojo.setGitDescribe(gitDescribeConfig);
91+
92+
93+
mojo.runningTests = true;
94+
mojo.project = mock(MavenProject.class, RETURNS_MOCKS);
95+
when(mojo.project.getPackaging()).thenReturn("jar");
96+
97+
mojo.execute();
98+
99+
Properties properties = mojo.getProperties();
100+
101+
assertThat(properties.get("git.commit.files.dirty")).isEqualTo("true");
102+
}
103+
}

src/test/java/pl/project13/maven/git/GitCommitIdMojoTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public void shouldIncludeExpectedProperties() throws Exception {
8181
assertThat(properties).satisfies(new ContainsKeyCondition("git.branch"));
8282
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.id"));
8383
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.id.abbrev"));
84+
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.files.dirty"));
8485
assertThat(properties).satisfies(new ContainsKeyCondition("git.build.user.name"));
8586
assertThat(properties).satisfies(new ContainsKeyCondition("git.build.user.email"));
8687
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.user.name"));
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add file.txt
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ref: refs/heads/master
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
79c5c38ca494525c1d2d3127af13476f9759957d
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[core]
2+
repositoryformatversion = 0
3+
filemode = true
4+
bare = false
5+
logallrefupdates = true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Unnamed repository; edit this file 'description' to name the repository.
104 Bytes
Binary file not shown.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# git ls-files --others --exclude-from=.git/info/exclude
2+
# Lines that start with '#' are comments.
3+
# For a project mostly in C, the following would be a good set of
4+
# exclude patterns (uncomment them if you want to use them):
5+
# *.[oa]
6+
# *~
7+
/_git_dir/

0 commit comments

Comments
 (0)