|
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 | + |
| 19 | + Assertions.assertThat(BuildInfo.BRANCH).as("branch").isEqualTo(BRANCH); |
| 20 | + Assertions.assertThat(BuildInfo.COMMIT_ID).as("commit.full").isEqualTo(COMMIT_ID); |
| 21 | + Assertions.assertThat(COMMIT_ID).as("commit.abbrev").startsWith(BuildInfo.COMMIT_ID_ABBREV); |
17 | 22 | } |
18 | 23 |
|
19 | 24 | /** Get current non-abbreviated Git commit hash. */ |
20 | | - private static String gitCommit() throws IOException { |
| 25 | + private static String gitCommit() { |
21 | 26 | return runCommand("git", "rev-parse", "HEAD"); |
22 | 27 | } |
23 | 28 |
|
24 | 29 | /** Get current git branch. */ |
25 | | - private static String gitBranch() throws IOException { |
| 30 | + private static String gitBranch() { |
26 | 31 | return runCommand("git", "branch", "--show-current"); |
27 | 32 | } |
28 | 33 |
|
29 | 34 | /** 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()); |
| 35 | + private static String runCommand(String... cmdarray) { |
| 36 | + try { |
| 37 | + var process = Runtime.getRuntime().exec(cmdarray); |
| 38 | + var r = new BufferedReader(new InputStreamReader(process.getInputStream())); |
| 39 | + return String.join("\n", (Iterable<String>) () -> r.lines().iterator()); |
| 40 | + } catch (IOException e) { |
| 41 | + return null; |
| 42 | + } |
34 | 43 | } |
35 | 44 | } |
0 commit comments