From: David ‘Bombe’ Roden Date: Sat, 7 Oct 2017 14:32:13 +0000 (+0200) Subject: Add parser for version information X-Git-Tag: 0.9.7^2~5 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=2147e8dd2e9bd581d96acdc1a4c746796669275c;p=Sone.git Add parser for version information --- diff --git a/build.gradle b/build.gradle index 4ab4170..3df60e9 100644 --- a/build.gradle +++ b/build.gradle @@ -2,12 +2,14 @@ group = 'net.pterodactylus' version = '0.9.6' buildscript { + ext.kotlinVersion = '1.1.51' repositories { mavenCentral() } dependencies { classpath group: 'info.solidsoft.gradle.pitest', name: 'gradle-pitest-plugin', version: '1.1.11' - classpath group: 'org.jetbrains.kotlin', name: 'kotlin-gradle-plugin', version: '1.1.51' + classpath group: 'org.jetbrains.kotlin', name: 'kotlin-gradle-plugin', version: kotlinVersion + classpath group: 'org.jetbrains.kotlin', name: 'kotlin-noarg', version: kotlinVersion } } @@ -46,6 +48,7 @@ dependencies { compile group: 'com.google.inject', name: 'guice', version: '3.0' compile group: 'com.google.guava', name: 'guava', version: '14.0.1' compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.1' + compile group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-yaml', version: '2.9.1' compile group: 'com.google.code.findbugs', name: 'jsr305', version: '3.0.2' compile group: 'org.jsoup', name: 'jsoup', version: '1.10.2' @@ -117,3 +120,9 @@ task countLines { dependsOn tasks.countLinesMain dependsOn tasks.countLinesTest } + +apply plugin: 'kotlin-noarg' + +noArg { + annotation('net.pterodactylus.sone.main.NoArg') +} diff --git a/src/main/kotlin/net/pterodactylus/sone/main/NoArg.kt b/src/main/kotlin/net/pterodactylus/sone/main/NoArg.kt new file mode 100644 index 0000000..eccf929 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/main/NoArg.kt @@ -0,0 +1,7 @@ +package net.pterodactylus.sone.main + +/** + * Annotation for class that will have a no-argument constructor artificially generated by + * the no-arg Kotlin compiler plugin. + */ +annotation class NoArg diff --git a/src/main/kotlin/net/pterodactylus/sone/main/VersionParser.kt b/src/main/kotlin/net/pterodactylus/sone/main/VersionParser.kt new file mode 100644 index 0000000..cb79893 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/main/VersionParser.kt @@ -0,0 +1,14 @@ +package net.pterodactylus.sone.main + +import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory + +fun parseVersion(file: String = "/version.yaml"): Version? = + Version::class.java.getResourceAsStream(file)?.use { + objectMapper.readValue(it, Version::class.java) + } + +private val objectMapper = ObjectMapper(YAMLFactory()) + +@NoArg +data class Version(val id: String, val nice: String) diff --git a/src/test/kotlin/net/pterodactylus/sone/main/VersionParserTest.kt b/src/test/kotlin/net/pterodactylus/sone/main/VersionParserTest.kt new file mode 100644 index 0000000..93861a0 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/main/VersionParserTest.kt @@ -0,0 +1,32 @@ +package net.pterodactylus.sone.main + +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.equalTo +import org.hamcrest.Matchers.nullValue +import org.junit.Test + +/** + * Unit test for [parseVersion]. + */ +class VersionParserTest { + + @Test + fun `version from missing file can not be read`() { + assertThat(parseVersion("does-not-exist.yaml"), nullValue()) + } + + @Test + fun `custom version file can be parsed`() { + val version = parseVersion("custom-version.yaml")!! + assertThat(version.id, equalTo("some-id")) + assertThat(version.nice, equalTo("some-nice")) + } + + @Test + fun `default version file is parsed`() { + val version = parseVersion()!! + assertThat(version.id, equalTo("43f3e1c3a0f487e37e5851a2cc72756d271c7571")) + assertThat(version.nice, equalTo("0.9.6-466-g43f3e1c")) + } + +} diff --git a/src/test/resources/net/pterodactylus/sone/main/custom-version.yaml b/src/test/resources/net/pterodactylus/sone/main/custom-version.yaml new file mode 100644 index 0000000..94517d5 --- /dev/null +++ b/src/test/resources/net/pterodactylus/sone/main/custom-version.yaml @@ -0,0 +1,2 @@ +id: some-id +nice: some-nice diff --git a/src/test/resources/version.yaml b/src/test/resources/version.yaml new file mode 100644 index 0000000..22023b5 --- /dev/null +++ b/src/test/resources/version.yaml @@ -0,0 +1,2 @@ +id: 43f3e1c3a0f487e37e5851a2cc72756d271c7571 +nice: 0.9.6-466-g43f3e1c