Skip to content

Commit

Permalink
#783: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSnoozer committed Sep 1, 2024
1 parent e70100d commit 42725b0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@
import java.util.Map;
import java.util.Properties;
import java.util.TimeZone;
import java.util.stream.Stream;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.FileUtils;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.ResetCommand;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import pl.project13.core.CommitIdPropertiesOutputFormat;
import pl.project13.core.git.GitDescribeConfig;
Expand Down Expand Up @@ -237,9 +239,20 @@ public void shouldSkipDescribeWhenConfiguredToDoSo(boolean useNativeGit) throws
.satisfies(new DoesNotContainKeyCondition("git.commit.id.describe"));
}

static Stream<Arguments> useNativeGitWithBranches() {
return useNativeGit().flatMap(arg ->
Stream.of(
"test_branch",
"feature/my-git-branch"
).map(str ->
Arguments.of(arg.get()[0], str)
)
);
}

@ParameterizedTest
@MethodSource("useNativeGit")
public void shouldNotUseBuildEnvironmentBranchInfoWhenParameterSet(boolean useNativeGit)
@MethodSource("useNativeGitWithBranches")
public void shouldNotUseBuildEnvironmentBranchInfoWhenParameterSet(boolean useNativeGit, String branchName)
throws Exception {
mavenSandbox
.withParentProject("my-jar-project", "jar")
Expand All @@ -262,14 +275,14 @@ public void shouldNotUseBuildEnvironmentBranchInfoWhenParameterSet(boolean useNa
// reset repo and force detached HEAD
try (final Git git = git("my-jar-project")) {
git.reset().setMode(ResetCommand.ResetType.HARD).setRef("b6a73ed").call();
git.checkout().setCreateBranch(true).setName("test_branch").setForceRefUpdate(true).call();
git.checkout().setCreateBranch(true).setName(branchName).setForceRefUpdate(true).call();
}

// when
mojo.execute();

// then
assertPropertyPresentAndEqual(targetProject.getProperties(), "git.branch", "test_branch");
assertPropertyPresentAndEqual(targetProject.getProperties(), "git.branch", branchName);
}

@ParameterizedTest
Expand Down
9 changes: 7 additions & 2 deletions src/test/java/pl/project13/maven/git/GitIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.Optional;
import java.util.Properties;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Stream;
import javax.annotation.Nonnull;
import org.apache.commons.io.FileUtils;
import org.apache.maven.execution.MavenSession;
Expand All @@ -40,6 +41,7 @@
import org.eclipse.jgit.api.Git;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.provider.Arguments;
import pl.project13.core.CommitIdPropertiesOutputFormat;

/**
Expand All @@ -53,8 +55,11 @@ public abstract class GitIntegrationTest {
private static final boolean UseJGit = false;
private static final boolean UseNativeGit = true;

public static Collection<?> useNativeGit() {
return asList(UseJGit, UseNativeGit);
public static Stream<Arguments> useNativeGit() {
return Stream.of(
Arguments.of(UseJGit),
Arguments.of(UseNativeGit)
);
}

/** Sandbox directory with unique name for current test. */
Expand Down
11 changes: 7 additions & 4 deletions src/test/java/pl/project13/maven/git/GitPropertiesFileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@

package pl.project13.maven.git;

import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Properties;
import java.util.stream.Stream;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.FileUtils;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import pl.project13.core.CommitIdPropertiesOutputFormat;
import pl.project13.core.util.GenericFileManager;
Expand All @@ -40,8 +40,11 @@ public class GitPropertiesFileTest extends GitIntegrationTest {
static final boolean USE_JGIT = false;
static final boolean USE_NATIVE_GIT = true;

public static Collection<?> useNativeGit() {
return asList(USE_JGIT, USE_NATIVE_GIT);
public static Stream<Arguments> useNativeGit() {
return Stream.of(
Arguments.of(USE_JGIT),
Arguments.of(USE_NATIVE_GIT)
);
}

@ParameterizedTest
Expand Down

0 comments on commit 42725b0

Please sign in to comment.