Skip to content

Commit 1512e5d

Browse files
author
TheSnoozer
committed
1 parent e6414c4 commit 1512e5d

6 files changed

Lines changed: 373 additions & 0 deletions

File tree

pom.xml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535

3636
<jgit.version>5.12.0.202106070339-r</jgit.version>
3737
<junit.version>4.13.2</junit.version>
38+
<mockito.version>3.11.0</mockito.version>
39+
<assertj.version>3.19.0</assertj.version>
3840
</properties>
3941

4042
<dependencyManagement>
@@ -217,6 +219,42 @@
217219
<version>${junit.version}</version>
218220
<scope>test</scope>
219221
</dependency>
222+
<dependency>
223+
<groupId>org.codehaus.plexus</groupId>
224+
<artifactId>plexus-utils</artifactId>
225+
<version>3.3.0</version>
226+
<scope>test</scope>
227+
</dependency>
228+
<dependency>
229+
<groupId>org.codehaus.plexus</groupId>
230+
<artifactId>plexus-container-default</artifactId>
231+
<version>2.1.0</version>
232+
<scope>test</scope>
233+
</dependency>
234+
<dependency>
235+
<groupId>org.assertj</groupId>
236+
<artifactId>assertj-core</artifactId>
237+
<version>${assertj.version}</version>
238+
<scope>test</scope>
239+
</dependency>
240+
<dependency>
241+
<groupId>org.mockito</groupId>
242+
<artifactId>mockito-core</artifactId>
243+
<version>${mockito.version}</version>
244+
<scope>test</scope>
245+
</dependency>
246+
<dependency>
247+
<groupId>pl.pragmatists</groupId>
248+
<artifactId>JUnitParams</artifactId>
249+
<version>1.1.1</version>
250+
<scope>test</scope>
251+
</dependency>
252+
<dependency>
253+
<groupId>org.slf4j</groupId>
254+
<artifactId>slf4j-simple</artifactId>
255+
<version>1.7.25</version>
256+
<scope>test</scope>
257+
</dependency>
220258
</dependencies>
221259

222260
<distributionManagement>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* This file is part of git-commit-id-maven-plugin by Konrad 'ktoso' Malawski <konrad.malawski@java.pl>
3+
*
4+
* git-commit-id-maven-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-maven-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-maven-plugin. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package pl.project13.core;
19+
20+
import org.junit.Test;
21+
import pl.project13.core.log.LoggerBridge;
22+
23+
import java.util.Properties;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
import static org.mockito.Mockito.mock;
27+
import static org.mockito.Mockito.spy;
28+
29+
public class GitDataProviderTest {
30+
@Test
31+
public void loadShortDescribe() throws GitCommitIdExecutionException {
32+
assertShortDescribe("1.0.2-12-g19471", "1.0.2-12");
33+
assertShortDescribe("v1.0.0-0-gde4db35917", "v1.0.0-0");
34+
assertShortDescribe("1.0.2-12-g19471-DEV", "1.0.2-12-DEV");
35+
assertShortDescribe("V-1.0.2-12-g19471-DEV", "V-1.0.2-12-DEV");
36+
37+
assertShortDescribe(null, null);
38+
assertShortDescribe("12.4.0-1432", "12.4.0-1432");
39+
assertShortDescribe("12.6.0", "12.6.0");
40+
assertShortDescribe("", "");
41+
}
42+
43+
private void assertShortDescribe(String commitDescribe, String expectedShortDescribe) throws GitCommitIdExecutionException {
44+
Properties prop = new Properties();
45+
if (commitDescribe != null) {
46+
prop.put(GitCommitPropertyConstant.COMMIT_DESCRIBE, commitDescribe);
47+
}
48+
49+
TestGitDataProvider gitDataProvider = spy(TestGitDataProvider.class);
50+
gitDataProvider.loadShortDescribe(prop);
51+
assertThat(prop.getProperty(GitCommitPropertyConstant.COMMIT_SHORT_DESCRIBE)).isEqualTo(expectedShortDescribe);
52+
}
53+
54+
private abstract static class TestGitDataProvider extends GitDataProvider {
55+
TestGitDataProvider() {
56+
super(mock(LoggerBridge.class));
57+
setPrefixDot("");
58+
}
59+
}
60+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* This file is part of git-commit-id-maven-plugin by Konrad 'ktoso' Malawski <konrad.malawski@java.pl>
3+
*
4+
* git-commit-id-maven-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-maven-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-maven-plugin. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package pl.project13.core;
19+
20+
import org.junit.Before;
21+
import org.junit.Rule;
22+
import org.junit.Test;
23+
import org.junit.rules.TemporaryFolder;
24+
import org.sonatype.plexus.build.incremental.BuildContext;
25+
import org.sonatype.plexus.build.incremental.DefaultBuildContext;
26+
import pl.project13.core.log.LoggerBridge;
27+
import pl.project13.core.log.StdOutLoggerBridge;
28+
29+
import java.io.IOException;
30+
import java.nio.file.Files;
31+
import java.nio.file.Path;
32+
import java.util.Properties;
33+
34+
import static java.nio.charset.StandardCharsets.UTF_8;
35+
import static org.junit.Assert.assertEquals;
36+
37+
public class PropertiesFileGeneratorTest {
38+
@Rule
39+
public final TemporaryFolder temporaryFolder = new TemporaryFolder();
40+
41+
private final LoggerBridge loggerBridge = new StdOutLoggerBridge(false);
42+
private final BuildContext buildContext = new DefaultBuildContext();
43+
44+
private PropertiesFileGenerator propertiesFileGenerator;
45+
46+
@Before
47+
public void setUp() {
48+
propertiesFileGenerator = new PropertiesFileGenerator(loggerBridge, buildContext, "properties", "", "test");
49+
}
50+
51+
@Test
52+
public void generatedPropertiesFileDoesNotContainDateComment() throws GitCommitIdExecutionException, IOException {
53+
Properties properties = new Properties();
54+
properties.put(GitCommitPropertyConstant.COMMIT_ID_FULL, "b5993378ffadd1f84dc8da220b9204d157ec0f29");
55+
properties.put(GitCommitPropertyConstant.BRANCH, "develop");
56+
57+
Path propertiesPath = temporaryFolder.getRoot().toPath().resolve("git.properties");
58+
propertiesFileGenerator.maybeGeneratePropertiesFile(properties, temporaryFolder.getRoot(), propertiesPath.getFileName().toString(), UTF_8);
59+
60+
byte[] bytes = Files.readAllBytes(propertiesPath);
61+
String actualContent = new String(bytes, UTF_8);
62+
String expectedContent = "#Generated by Git-Commit-Id-Plugin\n"
63+
+ "branch=develop\n"
64+
+ "commit.id.full=b5993378ffadd1f84dc8da220b9204d157ec0f29\n";
65+
assertEquals(expectedContent, actualContent);
66+
}
67+
68+
@Test
69+
public void rereadGeneratedPropertiesFile() throws GitCommitIdExecutionException, IOException {
70+
Properties properties = new Properties();
71+
properties.put(GitCommitPropertyConstant.COMMIT_ID_FULL, "b5993378ffadd1f84dc8da220b9204d157ec0f29");
72+
properties.put(GitCommitPropertyConstant.BRANCH, "develop");
73+
74+
Path propertiesPath = temporaryFolder.getRoot().toPath().resolve("git.properties");
75+
propertiesFileGenerator.maybeGeneratePropertiesFile(properties, temporaryFolder.getRoot(), propertiesPath.getFileName().toString(), UTF_8);
76+
77+
// Re-read the generated properties file.
78+
propertiesFileGenerator.maybeGeneratePropertiesFile(properties, temporaryFolder.getRoot(), propertiesPath.getFileName().toString(), UTF_8);
79+
80+
byte[] bytes = Files.readAllBytes(propertiesPath);
81+
String actualContent = new String(bytes, UTF_8);
82+
String expectedContent = "#Generated by Git-Commit-Id-Plugin\n"
83+
+ "branch=develop\n"
84+
+ "commit.id.full=b5993378ffadd1f84dc8da220b9204d157ec0f29\n";
85+
assertEquals(expectedContent, actualContent);
86+
}
87+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* This file is part of git-commit-id-maven-plugin by Konrad 'ktoso' Malawski <konrad.malawski@java.pl>
3+
*
4+
* git-commit-id-maven-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-maven-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-maven-plugin. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package pl.project13.core;
19+
20+
import org.junit.Test;
21+
import org.junit.runner.RunWith;
22+
import org.mockito.ArgumentMatchers;
23+
24+
import static org.junit.Assert.assertEquals;
25+
import static org.mockito.Mockito.mock;
26+
import static org.mockito.Mockito.when;
27+
28+
import junitparams.JUnitParamsRunner;
29+
import junitparams.Parameters;
30+
31+
import java.util.Arrays;
32+
import java.util.Collection;
33+
34+
@RunWith(JUnitParamsRunner.class)
35+
public class UriUserInfoRemoverTest {
36+
public static Collection<Object[]> parameters() {
37+
Object[][] data = new Object[][] {
38+
{ "https://example.com", "https://example.com" },
39+
{ "https://example.com:8888", "https://example.com:8888" },
40+
{ "https://user@example.com", "https://user@example.com" },
41+
{ "https://user@example.com:8888", "https://user@example.com:8888" },
42+
{ "https://user:password@example.com", "https://user@example.com" },
43+
{ "https://user:password@example.com:8888", "https://user@example.com:8888" },
44+
{ "https://:@git.server.com/pathToRepo.git", "https://git.server.com/pathToRepo.git" },
45+
{ "https://:password@git.server.com/pathToRepo.git", "https://git.server.com/pathToRepo.git" },
46+
{ "git@github.com", "git@github.com" },
47+
{ "git@github.com:8888", "git@github.com:8888" },
48+
{ "user@host.xz:~user/path/to/repo.git", "user@host.xz:~user/path/to/repo.git" },
49+
{ "[user@mygithost:10022]:my-group/my-sample-project.git", "[user@mygithost:10022]:my-group/my-sample-project.git" },
50+
{ "ssh://git@github.com/", "ssh://git@github.com/" },
51+
{ "/path/to/repo.git/", "/path/to/repo.git/" },
52+
{ "file:///path/to/repo.git/", "file:///path/to/repo.git/"},
53+
{ "file:///C:\\Users\\test\\example", "file:///C:\\Users\\test\\example"},
54+
{ "file://C:\\Users\\test\\example", "file://C:\\Users\\test\\example" },
55+
// ensure a percent encoded password is stripped too, that should be allowed
56+
{ "https://user:passw%40rd@example.com:8888", "https://user@example.com:8888" },
57+
// Must Support: use of 'unreserved' characters as https://www.ietf.org/rfc/rfc2396.txt, Section "2.3. Unreserved Characters"
58+
{ "https://user:A-_.!~*'()Z@example.com:8888", "https://user@example.com:8888" },
59+
// Optional Support: use of 'reserved' characters as https://www.ietf.org/rfc/rfc2396.txt, Section "2.2. Reserved Characters"
60+
// note: left out '/', '?', '@' since we technically expect user's need to escape those
61+
{ "https://user:A;:&=+$,Z@example.com:8888", "https://user@example.com:8888" },
62+
// Optional Support: use of 'delims' characters as https://www.ietf.org/rfc/rfc2396.txt, Section "2.4.3. Excluded US-ASCII Characters"
63+
{ "https://user:A<>#%\"Z@example.com:8888", "https://user@example.com:8888" },
64+
// Optional Support: use of 'unwise' characters as https://www.ietf.org/rfc/rfc2396.txt, Section "2.4.3. Excluded US-ASCII Characters"
65+
{ "https://user:A{}|\\^[]`Z@example.com:8888", "https://user@example.com:8888" },
66+
};
67+
return Arrays.asList(data);
68+
}
69+
70+
@Test
71+
@Parameters(method = "parameters")
72+
public void testStripCredentialsFromOriginUrl(String input, String expected) throws GitCommitIdExecutionException {
73+
GitDataProvider gitDataProvider = mock(GitDataProvider.class);
74+
when(gitDataProvider.stripCredentialsFromOriginUrl(ArgumentMatchers.any())).thenCallRealMethod();
75+
String result = gitDataProvider.stripCredentialsFromOriginUrl(input);
76+
assertEquals(expected, result);
77+
}
78+
79+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* This file is part of git-commit-id-maven-plugin by Konrad 'ktoso' Malawski <konrad.malawski@java.pl>
3+
*
4+
* git-commit-id-maven-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-maven-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-maven-plugin. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package pl.project13.core.jgit;
19+
20+
import org.eclipse.jgit.lib.Repository;
21+
import org.junit.Test;
22+
import pl.project13.core.log.StdOutLoggerBridge;
23+
import pl.project13.core.git.GitDescribeConfig;
24+
25+
import static org.mockito.Mockito.*;
26+
27+
public class DescribeCommandOptionsTest {
28+
private static final String evaluateOnCommit = "HEAD";
29+
30+
@Test(expected = IllegalArgumentException.class)
31+
public void abbrev_shouldVerifyLengthContract_failOn41() throws Exception {
32+
// given
33+
final Repository repo = mock(Repository.class);
34+
final int length = 41;
35+
36+
DescribeCommand.on(evaluateOnCommit, repo, new StdOutLoggerBridge(true)).abbrev(length);
37+
}
38+
39+
@Test(expected = IllegalArgumentException.class)
40+
public void abbrev_shouldVerifyLengthContract_failOnMinus12() throws Exception {
41+
// given
42+
final Repository repo = mock(Repository.class);
43+
final int length = -12;
44+
45+
DescribeCommand.on(evaluateOnCommit, repo, new StdOutLoggerBridge(true)).abbrev(length);
46+
}
47+
48+
@Test
49+
public void apply_shouldDelegateToAllOptions() throws Exception {
50+
// given
51+
final String devel = "DEVEL";
52+
final String match = "*";
53+
final int abbrev = 12;
54+
55+
GitDescribeConfig config = new GitDescribeConfig(true, devel, match, abbrev, true, true);
56+
57+
Repository repo = mock(Repository.class);
58+
DescribeCommand command = DescribeCommand.on(evaluateOnCommit, repo, new StdOutLoggerBridge(true));
59+
DescribeCommand spiedCommand = spy(command);
60+
61+
// when
62+
spiedCommand.apply(config);
63+
64+
// then
65+
verify(spiedCommand).always(eq(true));
66+
verify(spiedCommand).abbrev(eq(abbrev));
67+
verify(spiedCommand).dirty(eq(devel));
68+
verify(spiedCommand).tags(eq(true));
69+
verify(spiedCommand).forceLongFormat(eq(true));
70+
}
71+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* This file is part of git-commit-id-maven-plugin by Konrad 'ktoso' Malawski <konrad.malawski@java.pl>
3+
*
4+
* git-commit-id-maven-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-maven-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-maven-plugin. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package pl.project13.core.jgit;
19+
20+
import org.junit.Test;
21+
import pl.project13.core.log.StdOutLoggerBridge;
22+
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
25+
public class JGitCommonTest {
26+
27+
@Test
28+
public void trimFullTagName_shouldTrimFullTagNamePrefix() throws Exception {
29+
// given
30+
String fullName = "refs/tags/v1.0.0";
31+
32+
// when
33+
String simpleName = new JGitCommon(new StdOutLoggerBridge(true)).trimFullTagName(fullName);
34+
35+
// then
36+
assertThat(simpleName).isEqualTo("v1.0.0");
37+
}
38+
}

0 commit comments

Comments
 (0)