Skip to content

Commit

Permalink
Handle build-info.properties file not present case
Browse files Browse the repository at this point in the history
  • Loading branch information
Hayanesh committed Sep 18, 2023
1 parent cc1c5c7 commit 282da0d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public Resource createResource(ConfigProperties config) {

private Optional<String> getServiceVersionFromBuildInfo() {
try (InputStream in = system.openClasspathResource("build-info.properties", "META-INF")) {
return getServiceVersionPropertyFromStream(in);
return in != null ? getServiceVersionPropertyFromStream(in) : Optional.empty();
} catch (Exception e) {
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,25 @@ void givenBuildVersionIsPresentInBuildInfProperties_thenReturnBuildVersion() {
assertThat(result.getAttribute(SERVICE_VERSION)).isEqualTo("0.0.2");
}

@Test
void givenBuildVersionFileNotPresent_thenReturnEmptyResource() {
when(system.openClasspathResource(BUILD_PROPS, META_INFO)).thenReturn(null);

SpringBootServiceVersionDetector guesser = new SpringBootServiceVersionDetector(system);
Resource result = guesser.createResource(config);
assertThat(result).isEqualTo(Resource.empty());
}

@Test
void givenBuildVersionFileIsPresentButBuildVersionPropertyNotPresent_thenReturnEmptyResource() {
when(system.openClasspathResource(BUILD_PROPS, META_INFO))
.thenReturn(openClasspathResource(BUILD_PROPS));

SpringBootServiceVersionDetector guesser = new SpringBootServiceVersionDetector(system);
Resource result = guesser.createResource(config);
assertThat(result).isEqualTo(Resource.empty());
}

private InputStream openClasspathResource(String resource) {
return getClass().getClassLoader().getResourceAsStream(resource);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build.artifact=something
build.name=some-name

0 comments on commit 282da0d

Please sign in to comment.