|
| 1 | +package io.weaviate.client6.v1.internal; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStreamReader; |
| 6 | + |
| 7 | +import org.assertj.core.api.Assertions; |
| 8 | +import org.junit.Assume; |
| 9 | +import org.junit.Test; |
| 10 | + |
| 11 | +public class BuildInfoTest { |
| 12 | + private static final String BRANCH = gitBranch(); |
| 13 | + private static final String COMMIT_ID = gitCommit(); |
| 14 | + |
| 15 | + @Test |
| 16 | + public void testBuildInfo() throws IOException { |
| 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); |
| 24 | + } |
| 25 | + |
| 26 | + /** Get current non-abbreviated Git commit hash. */ |
| 27 | + private static String gitCommit() { |
| 28 | + return runCommand("/usr/bin/git", "rev-parse", "HEAD"); |
| 29 | + } |
| 30 | + |
| 31 | + /** Get current git branch. */ |
| 32 | + private static String gitBranch() { |
| 33 | + return runCommand("/usr/bin/git", "branch", "--show-current"); |
| 34 | + } |
| 35 | + |
| 36 | + /** Run shell command and return the output as multi-line string. */ |
| 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 | + } |
| 45 | + } |
| 46 | +} |
0 commit comments