|
5 | 5 | import java.io.InputStreamReader; |
6 | 6 |
|
7 | 7 | import org.assertj.core.api.Assertions; |
| 8 | +import org.junit.Assume; |
8 | 9 | import org.junit.Test; |
9 | 10 |
|
10 | 11 | public class BuildInfoTest { |
| 12 | + private static final String BRANCH = gitBranch(); |
| 13 | + private static final String COMMIT_ID = gitCommit(); |
11 | 14 |
|
12 | 15 | @Test |
13 | 16 | public void testBuildInfo() throws IOException { |
14 | | - Assertions.assertThat(BuildInfo.BRANCH).as("branch").isEqualTo(gitBranch()); |
15 | | - Assertions.assertThat(BuildInfo.COMMIT_ID).as("commit.full").isEqualTo(gitCommit()); |
16 | | - Assertions.assertThat(gitCommit()).as("commit.abbrev").startsWith(BuildInfo.COMMIT_ID_ABBREV); |
| 17 | + Assume.assumeNotNull(BRANCH, COMMIT_ID); |
| 18 | + Assume.assumeTrue("found git branch", !BRANCH.isBlank()); |
| 19 | + Assume.assumeTrue("found git commit", !COMMIT_ID.isBlank()); |
| 20 | + |
| 21 | + Assertions.assertThat(BuildInfo.BRANCH).as("branch").isEqualTo(BRANCH); |
| 22 | + Assertions.assertThat(BuildInfo.COMMIT_ID).as("commit.full").isEqualTo(COMMIT_ID); |
| 23 | + Assertions.assertThat(COMMIT_ID).as("commit.abbrev").startsWith(BuildInfo.COMMIT_ID_ABBREV); |
17 | 24 | } |
18 | 25 |
|
19 | 26 | /** Get current non-abbreviated Git commit hash. */ |
20 | | - private static String gitCommit() throws IOException { |
21 | | - return runCommand("git", "rev-parse", "HEAD"); |
| 27 | + private static String gitCommit() { |
| 28 | + return runCommand("/usr/bin/git", "rev-parse", "HEAD"); |
22 | 29 | } |
23 | 30 |
|
24 | 31 | /** Get current git branch. */ |
25 | | - private static String gitBranch() throws IOException { |
26 | | - return runCommand("git", "branch", "--show-current"); |
| 32 | + private static String gitBranch() { |
| 33 | + return runCommand("/usr/bin/git", "branch", "--show-current"); |
27 | 34 | } |
28 | 35 |
|
29 | 36 | /** Run shell command and return the output as multi-line string. */ |
30 | | - private static String runCommand(String... cmdarray) throws IOException { |
31 | | - var process = Runtime.getRuntime().exec(cmdarray); |
32 | | - var r = new BufferedReader(new InputStreamReader(process.getInputStream())); |
33 | | - return String.join("\n", (Iterable<String>) () -> r.lines().iterator()); |
| 37 | + private static String runCommand(String... cmdarray) { |
| 38 | + try { |
| 39 | + var process = Runtime.getRuntime().exec(cmdarray); |
| 40 | + var r = new BufferedReader(new InputStreamReader(process.getInputStream())); |
| 41 | + return String.join("\n", (Iterable<String>) () -> r.lines().iterator()); |
| 42 | + } catch (IOException e) { |
| 43 | + return null; |
| 44 | + } |
34 | 45 | } |
35 | 46 | } |
0 commit comments