Switched from spek2 to kotlin-test.

This commit is contained in:
Erik C. Thauvin 2021-07-09 23:25:12 -07:00
parent 808ca2a6f2
commit bc2813cd31
12 changed files with 536 additions and 633 deletions

39
.gitignore vendored
View file

@ -1,22 +1,7 @@
.vscode/*
!.vscode/extensions.json
!.vscode/launch.json
!.vscode/settings.json
!.vscode/tasks.json
__pycache__
.classpath
.DS_Store
.gradle
.history
.kobalt
.mtj.tmp/
.mvn/timing.properties
.mvn/wrapper/maven-wrapper.jar
.nb-gradle
.project
.scannerwork
.settings
*.class
*.code-workspace
*.ctxt
@ -27,7 +12,21 @@ __pycache__
*.sublime-*
*.tar.gz
*.zip
/**/.idea_modules/
.DS_Store
.classpath
.gradle
.history
.kobalt
.mtj.tmp/
.mvn/timing.properties
.mvn/wrapper/maven-wrapper.jar
.nb-gradle
.project
.scannerwork
.settings
.vscode/*
/**/.idea/$CACHE_FILE$
/**/.idea/$PRODUCT_WORKSPACE_FILE$
/**/.idea/**/caches/build_file_checksums.ser
/**/.idea/**/contentModel.xml
/**/.idea/**/dataSources.ids
@ -48,8 +47,10 @@ __pycache__
/**/.idea/**/uiDesigner.xml
/**/.idea/**/usage.statistics.xml
/**/.idea/**/workspace.xml
/**/.idea/$CACHE_FILE$
/**/.idea/$PRODUCT_WORKSPACE_FILE$
/**/.idea/sonarlint*
/**/.idea_modules/
Thumbs.db
__pycache__
atlassian-ide-plugin.xml
bin/
build/
@ -71,6 +72,7 @@ lib/kotlin*
libs/
local.properties
out/
pom.xml.asc
pom.xml.next
pom.xml.releaseBackup
pom.xml.tag
@ -80,5 +82,4 @@ project.properties
release.properties
target/
test-output
Thumbs.db
venv

View file

@ -26,5 +26,15 @@
<option name="name" value="MavenLocal" />
<option name="url" value="file:$MAVEN_REPOSITORY$/" />
</remote-repository>
<remote-repository>
<option name="id" value="MavenRepo" />
<option name="name" value="MavenRepo" />
<option name="url" value="https://repo.maven.apache.org/maven2/" />
</remote-repository>
<remote-repository>
<option name="id" value="maven" />
<option name="name" value="maven" />
<option name="url" value="https://oss.sonatype.org/content/repositories/snapshots" />
</remote-repository>
</component>
</project>

View file

@ -0,0 +1,65 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="libraries-with-intellij-classes">
<option name="intellijApiContainingLibraries">
<list>
<LibraryCoordinatesState>
<option name="artifactId" value="ideaIU" />
<option name="groupId" value="com.jetbrains.intellij.idea" />
</LibraryCoordinatesState>
<LibraryCoordinatesState>
<option name="artifactId" value="ideaIU" />
<option name="groupId" value="com.jetbrains" />
</LibraryCoordinatesState>
<LibraryCoordinatesState>
<option name="artifactId" value="ideaIC" />
<option name="groupId" value="com.jetbrains.intellij.idea" />
</LibraryCoordinatesState>
<LibraryCoordinatesState>
<option name="artifactId" value="ideaIC" />
<option name="groupId" value="com.jetbrains" />
</LibraryCoordinatesState>
<LibraryCoordinatesState>
<option name="artifactId" value="pycharmPY" />
<option name="groupId" value="com.jetbrains.intellij.pycharm" />
</LibraryCoordinatesState>
<LibraryCoordinatesState>
<option name="artifactId" value="pycharmPY" />
<option name="groupId" value="com.jetbrains" />
</LibraryCoordinatesState>
<LibraryCoordinatesState>
<option name="artifactId" value="pycharmPC" />
<option name="groupId" value="com.jetbrains.intellij.pycharm" />
</LibraryCoordinatesState>
<LibraryCoordinatesState>
<option name="artifactId" value="pycharmPC" />
<option name="groupId" value="com.jetbrains" />
</LibraryCoordinatesState>
<LibraryCoordinatesState>
<option name="artifactId" value="clion" />
<option name="groupId" value="com.jetbrains.intellij.clion" />
</LibraryCoordinatesState>
<LibraryCoordinatesState>
<option name="artifactId" value="clion" />
<option name="groupId" value="com.jetbrains" />
</LibraryCoordinatesState>
<LibraryCoordinatesState>
<option name="artifactId" value="riderRD" />
<option name="groupId" value="com.jetbrains.intellij.rider" />
</LibraryCoordinatesState>
<LibraryCoordinatesState>
<option name="artifactId" value="riderRD" />
<option name="groupId" value="com.jetbrains" />
</LibraryCoordinatesState>
<LibraryCoordinatesState>
<option name="artifactId" value="goland" />
<option name="groupId" value="com.jetbrains.intellij.goland" />
</LibraryCoordinatesState>
<LibraryCoordinatesState>
<option name="artifactId" value="goland" />
<option name="groupId" value="com.jetbrains" />
</LibraryCoordinatesState>
</list>
</option>
</component>
</project>

2
.idea/misc.xml generated
View file

@ -13,7 +13,7 @@
<component name="NodePackageJsonFileManager">
<packageJsonPaths />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_6" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_6" project-jdk-name="15" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View file

@ -18,10 +18,6 @@ plugins {
version = "1.0.5"
group = "net.thauvin.erik.gradle"
object Versions {
const val SPEK = "2.0.15" // Don't upgrade until 2.0.17
}
val github = "https://github.com/ethauvin/semver-gradle"
val packageName = "net.thauvin.erik.gradle.semver"
@ -35,14 +31,11 @@ dependencies {
implementation(platform(kotlin("bom")))
implementation(kotlin("stdlib"))
testImplementation(kotlin("reflect"))
testImplementation(kotlin("test"))
//testImplementation(gradleTestKit())
testImplementation("org.spekframework.spek2:spek-dsl-jvm:${Versions.SPEK}")
testRuntimeOnly("org.spekframework.spek2:spek-runner-junit5:${Versions.SPEK}")
testImplementation(kotlin("test"))
testImplementation(kotlin("test-junit"))
}
tasks {
@ -57,11 +50,7 @@ tasks {
withType<Test> {
testLogging {
exceptionFormat = TestExceptionFormat.FULL
events = setOf(TestLogEvent.SKIPPED, TestLogEvent.FAILED)
}
useJUnitPlatform {
includeEngines("spek2")
events = setOf(TestLogEvent.PASSED, TestLogEvent.SKIPPED, TestLogEvent.FAILED)
}
}

View file

@ -2,6 +2,7 @@
<SmellBaseline>
<ManuallySuppressedIssues></ManuallySuppressedIssues>
<CurrentIssues>
<ID>LongMethod:UtilsTest.kt$UtilsTest$@Test fun testSystemProperties()</ID>
<ID>MagicNumber:Utils.kt$Utils$3</ID>
<ID>MagicNumber:Utils.kt$Utils$4</ID>
<ID>MagicNumber:Utils.kt$Utils$5</ID>

View file

@ -1,125 +0,0 @@
/*
* SemverConfigSpec.kt
*
* Copyright (c) 2018-2021, Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of this project nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.thauvin.erik.gradle.semver
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.gherkin.Feature
import kotlin.test.assertEquals
import kotlin.test.assertTrue
@Suppress("unused")
object SemverConfigSpec : Spek({
Feature("SemverConfig") {
val config = SemverConfig(Version())
Scenario("Testing configs") {
val vars = listOf(
config.semverKey,
config.majorKey,
config.minorKey,
config.patchKey,
config.preReleaseKey,
config.preReleasePrefixKey,
config.buildMetaKey,
config.buildMetaPrefixKey,
config.separatorKey
)
val defaults = listOf(
SemverConfig.DEFAULT_SEMVER_KEY,
SemverConfig.DEFAULT_MAJOR_KEY,
SemverConfig.DEFAULT_MINOR_KEY,
SemverConfig.DEFAULT_PATCH_KEY,
SemverConfig.DEFAULT_PRERELEASE_KEY,
SemverConfig.DEFAULT_PRERELEASE_PREFIX_KEY,
SemverConfig.DEFAULT_BUILDMETA_KEY,
SemverConfig.DEFAULT_BUILDMETA_PREFIX_KEY,
SemverConfig.DEFAULT_SEPARATOR
)
When("checking defaults") {}
defaults.forEachIndexed { i, d ->
Then(" ${vars[i]} should be the same: ${config.keysPrefix}$d") {
assertEquals(vars[i], "${config.keysPrefix}$d")
}
}
Then("config.properties should be version.properties") {
assertEquals(config.properties, "version.properties")
}
lateinit var newKeys: List<String>
When("setting keyPrefix to test.") {
config.keysPrefix = "test."
newKeys = listOf(
config.semverKey,
config.majorKey,
config.minorKey,
config.patchKey,
config.preReleaseKey,
config.preReleasePrefixKey,
config.buildMetaKey,
config.buildMetaPrefixKey,
config.separatorKey
)
}
Then("all config keys should start with test.xxxx") {
newKeys.forEach { k ->
assertTrue(k.startsWith("test."), k)
}
}
When("checking extension properties") {}
Then("semver should be defaults") {
val defaultSemver =
"${Version.DEFAULT_MAJOR}${Version.DEFAULT_SEPARATOR}${Version.DEFAULT_MINOR}" +
"${Version.DEFAULT_SEPARATOR}${Version.DEFAULT_PATCH}"
assertEquals(config.semver, defaultSemver)
assertEquals(
"${config.major}${config.separator}${config.minor}${config.separator}${config.patch}",
defaultSemver
)
assertEquals(config.preRelease, Version.DEFAULT_EMPTY)
assertEquals(config.buildMeta, Version.DEFAULT_EMPTY)
assertEquals(config.preReleasePrefix, Version.DEFAULT_PRERELEASE_PREFIX)
assertEquals(config.buildMetaPrefix, Version.DEFAULT_BUILDMETA_PREFIX)
}
Then("semver = version") {
assertEquals(config.semver, config.version)
}
}
}
})

View file

@ -0,0 +1,110 @@
/*
* SemverConfigTest.kt
*
* Copyright (c) 2018-2021, Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of this project nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.thauvin.erik.gradle.semver
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
class SemverConfigTest {
private val config = SemverConfig(Version())
@Test
fun testConfigs() {
val vars = listOf(
config.semverKey,
config.majorKey,
config.minorKey,
config.patchKey,
config.preReleaseKey,
config.preReleasePrefixKey,
config.buildMetaKey,
config.buildMetaPrefixKey,
config.separatorKey
)
val defaults = listOf(
SemverConfig.DEFAULT_SEMVER_KEY,
SemverConfig.DEFAULT_MAJOR_KEY,
SemverConfig.DEFAULT_MINOR_KEY,
SemverConfig.DEFAULT_PATCH_KEY,
SemverConfig.DEFAULT_PRERELEASE_KEY,
SemverConfig.DEFAULT_PRERELEASE_PREFIX_KEY,
SemverConfig.DEFAULT_BUILDMETA_KEY,
SemverConfig.DEFAULT_BUILDMETA_PREFIX_KEY,
SemverConfig.DEFAULT_SEPARATOR
)
defaults.forEachIndexed { i, d ->
assertEquals(vars[i], "${config.keysPrefix}$d", " ${vars[i]} should be the same: ${config.keysPrefix}$d")
}
assertEquals(config.properties, "version.properties", "config.properties should be version.properties")
}
@Test
fun testExtensionProperties() {
config.keysPrefix = "test."
val newKeys = listOf(
config.semverKey,
config.majorKey,
config.minorKey,
config.patchKey,
config.preReleaseKey,
config.preReleasePrefixKey,
config.buildMetaKey,
config.buildMetaPrefixKey,
config.separatorKey
)
newKeys.forEach { k ->
assertTrue(k.startsWith("test."), "$k: all config keys should start with test.xxxx")
}
val defaultSemver =
"${Version.DEFAULT_MAJOR}${Version.DEFAULT_SEPARATOR}${Version.DEFAULT_MINOR}" +
"${Version.DEFAULT_SEPARATOR}${Version.DEFAULT_PATCH}"
assertEquals(config.semver, defaultSemver, "semver should be defaults")
assertEquals(
"${config.major}${config.separator}${config.minor}${config.separator}${config.patch}",
defaultSemver,
"major-minor-patch should be defaults."
)
assertEquals(config.preRelease, Version.DEFAULT_EMPTY, "preRelease empty default")
assertEquals(config.buildMeta, Version.DEFAULT_EMPTY, "buildMeta empty default")
assertEquals(config.preReleasePrefix, Version.DEFAULT_PRERELEASE_PREFIX, "preReleasePrefix default")
assertEquals(config.buildMetaPrefix, Version.DEFAULT_BUILDMETA_PREFIX, "buildMetaPrefix default")
assertEquals(config.semver, config.version, "semver = version")
}
}

View file

@ -1,203 +0,0 @@
/*
* SemverVersionSpec.kt
*
* Copyright (c) 2018-2021, Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of this project nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.thauvin.erik.gradle.semver
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.gherkin.Feature
import kotlin.test.assertEquals
@Suppress("unused")
object SemverVersionSpec : Spek({
Feature("SemverVersion") {
val version = Version()
Scenario("Testing Versions") {
When("validating default version") {}
Then("major should be 1") {
assertEquals(1, version.major)
}
Then("minor should be 1") {
assertEquals(0, version.minor)
}
Then("patch should be 0") {
assertEquals(0, version.patch)
}
Then("prerelease should be empty") {
assertEquals("", version.preRelease)
}
Then("meta should be empty") {
assertEquals("", version.buildMeta)
}
Then("preRelease prefix should be -") {
assertEquals("-", version.preReleasePrefix)
}
Then("meta prefix should be +") {
assertEquals("+", version.buildMetaPrefix)
}
Then("separator should be .") {
assertEquals(".", version.separator)
}
Then("version should be 1.0.0") {
assertEquals("1.0.0", version.semver)
}
When("incrementing major") {
version.increment(isMajor = true)
}
Then("should return 2.0.0") {
assertEquals("2.0.0", version.semver)
}
When("incrementing minor") {
version.increment(isMinor = true)
}
Then("should return 2.1.0") {
assertEquals("2.1.0", version.semver)
}
When("incrementing patch") {
version.increment(isPatch = true)
}
Then("should return 2.1.1") {
assertEquals("2.1.1", version.semver)
}
When("incrementing minor again") {
version.increment(isMinor = true)
}
Then("should return 2.2.0") {
assertEquals("2.2.0", version.semver)
}
When("incrementing major again") {
version.increment(isMajor = true)
}
Then("should return 3.0.0") {
assertEquals("3.0.0", version.semver)
}
When("incrementing all") {
version.increment(isMajor = true, isMinor = true, isPatch = true)
}
Then("should return 4.1.1") {
assertEquals("4.1.1", version.semver)
}
When("incrementing major and minor") {
version.increment(isMajor = true, isMinor = true)
}
Then("should return 5.1.0") {
assertEquals("5.1.0", version.semver)
}
When("incrementing minor and patch") {
version.increment(isMinor = true, isPatch = true)
}
Then("should return 5.2.1") {
assertEquals("5.2.1", version.semver)
}
When("incrementing nothing") {
version.increment()
}
Then("should still return 5.2.1") {
assertEquals("5.2.1", version.semver)
}
When("resetting version") {
version.major = 1
version.minor = 0
version.patch = 0
}
Then("should return 1.0.0") {
assertEquals("1.0.0", version.semver)
}
When("adding prerelease") {
version.preRelease = "beta"
}
Then("should return 1.0.0-beta") {
assertEquals("1.0.0-beta", version.semver)
}
When("adding metadata") {
version.buildMeta = "007"
}
Then("should return 1.0.0-beta+007") {
assertEquals("1.0.0-beta+007", version.semver)
}
When("changing prerelease prefix") {
version.preReleasePrefix = "--"
}
Then("should return 1.0.0--beta+007") {
assertEquals("1.0.0--beta+007", version.semver)
}
When("changing meta prefix") {
version.buildMetaPrefix = "++"
}
Then("should return 1.0.0--beta++007") {
assertEquals("1.0.0--beta++007", version.semver)
}
When("changing separator") {
version.separator = "-"
}
Then("should return 1-0-0--beta++007") {
assertEquals("1-0-0--beta++007", version.semver)
}
}
}
})

View file

@ -1,271 +0,0 @@
/*
* UtilsSpec.kt
*
* Copyright (c) 2018-2021, Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of this project nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.thauvin.erik.gradle.semver
import net.thauvin.erik.gradle.semver.Utils.canReadFile
import org.gradle.api.GradleException
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.gherkin.Feature
import java.io.File
import java.util.*
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertNull
import kotlin.test.assertTrue
@Suppress("unused")
object UtilsSpec : Spek(
{
Feature("Utils") {
val version = Version()
val config = SemverConfig(version)
val propsFile = File("test.properties")
val projectDir = File("./")
lateinit var props: Properties
Scenario("Save/Load Properties") {
When("saving the property") {
config.properties = propsFile.name
Utils.saveProperties(projectDir, config, version)
}
Then("properties file should exists and be readable") {
assertEquals(propsFile.canReadFile(), propsFile.canRead() && propsFile.isFile)
}
When("loading the properties file") {
props = Utils.loadProperties(propsFile)
propsFile.delete()
}
Then("version and properties should be the same.") {
assertEquals(props.getProperty(config.majorKey), version.major.toString(), "Major")
assertEquals(props.getProperty(config.minorKey), version.minor.toString(), "Minor")
assertEquals(props.getProperty(config.patchKey), version.patch.toString(), "Patch")
assertEquals(props.getProperty(config.preReleaseKey), version.preRelease, "PreRelease")
assertNull(props.getProperty(config.preReleasePrefixKey), "PreRelease Prefix")
assertEquals(props.getProperty(config.buildMetaKey), version.buildMeta, "Build Meta")
assertNull(props.getProperty(config.buildMetaPrefixKey), "Build Meta Prefix")
assertNull(props.getProperty(config.separatorKey), "Separator")
assertEquals(props.getProperty(config.semverKey), version.semver, "semver")
}
}
Scenario("System Properties") {
lateinit var sysProps: Array<Pair<String, String>>
Given("new system properties") {
sysProps = arrayOf(
Pair(config.majorKey, "2"),
Pair(config.minorKey, "1"),
Pair(config.patchKey, "1"),
Pair(config.preReleaseKey, "beta"),
Pair(config.buildMetaKey, "007")
)
}
Then("none should already exists") {
assertTrue(
Utils.isNotSystemProperty(
setOf(
config.majorKey,
config.minorKey,
config.patchKey,
config.preReleaseKey,
config.buildMetaKey
)
)
)
}
Then("version should match system properties") {
sysProps.forEach {
System.getProperties().setProperty(it.first, it.second)
if (it.first == config.majorKey || it.first == config.minorKey || it.first == config.patchKey) {
assertEquals(Utils.loadIntProperty(props, it.first, -1), it.second.toInt())
} else {
assertEquals(Utils.loadProperty(props, it.first, ""), it.second)
}
}
}
When("loading version") {
Utils.loadVersion(config, version, props)
}
Then("version should be identical") {
assertEquals(version.semver, "2.1.1-beta+007")
}
When("saving properties") {
Utils.saveProperties(projectDir, config, version)
}
lateinit var newProps: Properties
And("loading properties file") {
newProps = Utils.loadProperties(propsFile)
}
Then("new properties should validate") {
sysProps.forEach {
assertEquals(newProps.getProperty(it.first), it.second, it.second)
}
propsFile.delete()
}
When("setting the version as system property") {
System.getProperties().setProperty(config.semverKey, "3.2.2")
}
And("loading the properties") {
Utils.loadVersion(config, version, props)
}
Then("versions should match") {
assertEquals(version.semver, System.getProperty(config.semverKey))
}
}
Scenario("Version Parsing") {
When("validating version parsing") {}
Then("versions should parse") {
listOf(
"1.0.0",
"2.1.0-beta",
"3.2.1-beta+007",
"4.3.2+007",
"11.11.1",
"111.11.11-beta",
"1111.111.11-beta+001.12"
).forEach {
assertTrue(Utils.parseSemVer(it, version), "parsing semver: $it")
assertEquals(it, version.semver, it)
}
}
Then("should throw exceptions") {
assertFailsWith<GradleException>("2.1.1a") {
Utils.parseSemVer("2.1.1a", version)
}
assertFailsWith<GradleException>("2a.1.1") {
Utils.parseSemVer("2a.1.1", version)
}
assertFailsWith<GradleException>("2.1a.1") {
Utils.parseSemVer("2.1a.1", version)
}
assertFailsWith<GradleException>("2.1") {
Utils.parseSemVer("2.1", version)
}
}
Given("new prefixes") {
version.preReleasePrefix = "."
version.buildMetaPrefix = "."
}
Then("prefixes should parse") {
listOf("2.1.0.beta.1", "2.1.1.1", "3.2.1.beta.1.007").forEach {
assertTrue(Utils.parseSemVer(it, version), "parsing semver: $it")
assertEquals(it, version.semver, it)
}
}
Then("last pre-release and meta should match") {
assertEquals(version.preRelease, "beta")
assertEquals(version.buildMeta, "1.007")
}
}
Scenario("Load locked properties") {
lateinit var locked: File
Given("the locked location") {
locked = File("locked")
}
Then("loading locked properties") {
assertFailsWith<GradleException> {
Utils.loadProperties(File(locked, propsFile.name))
}
locked.delete()
}
}
Scenario("Save to locked properties") {
lateinit var propsLocked: File
Given("the locked properties") {
propsLocked = File(System.getProperty("user.home") + File.separator + "locked.properties")
propsLocked.createNewFile()
propsLocked.setReadOnly()
config.properties = propsLocked.name
}
Then("saving the locked properties file") {
if (!propsLocked.canWrite()) {
assertFailsWith<GradleException> {
Utils.saveProperties(propsLocked.parentFile, config, version)
}
}
propsLocked.delete()
}
}
Scenario("Save/Load Properties in foo") {
lateinit var fooDir: File
lateinit var fooProps: File
When("saving the foo property") {
fooDir = File("foo")
fooDir.mkdir()
fooProps = File(fooDir, propsFile.name)
config.properties = fooProps.absolutePath
Utils.saveProperties(projectDir, config, version)
}
Then("foo properties file should exists and be readable") {
assertEquals(fooProps.canReadFile(), fooProps.canRead() && fooProps.isFile)
}
When("loading the foo properties file") {
props = Utils.loadProperties(fooProps)
fooProps.delete()
fooDir.delete()
}
Then("version in foo properties should be the same") {
assertEquals(props.getProperty(config.semverKey), version.semver)
}
}
}
}
)

View file

@ -0,0 +1,220 @@
/*
* UtilsTest.kt
*
* Copyright (c) 2018-2021, Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of this project nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.thauvin.erik.gradle.semver
import net.thauvin.erik.gradle.semver.Utils.canReadFile
import org.gradle.api.GradleException
import java.io.File
import kotlin.test.*
/**
* [Utils] Tests
*/
class UtilsTest {
private val version = Version()
private val config = SemverConfig(version)
private val propsFile = File("test.properties")
private val projectDir = File("./")
@Test
fun testExceptions() {
assertFailsWith<GradleException>("2.1.1a") {
Utils.parseSemVer("2.1.1a", version)
}
assertFailsWith<GradleException>("2a.1.1") {
Utils.parseSemVer("2a.1.1", version)
}
assertFailsWith<GradleException>("2.1a.1") {
Utils.parseSemVer("2.1a.1", version)
}
assertFailsWith<GradleException>("2.1") {
Utils.parseSemVer("2.1", version)
}
}
@Test
fun testFooProperties() {
val fooDir = File("foo")
fooDir.mkdir()
val fooFile = File(fooDir, propsFile.name)
config.properties = fooFile.absolutePath
Utils.saveProperties(projectDir, config, version)
assertEquals(
fooFile.canReadFile(),
fooFile.canRead() && fooFile.isFile,
"foo properties file should exists and be readable"
)
val fooProps = Utils.loadProperties(fooFile)
fooFile.delete()
fooDir.delete()
assertEquals(
fooProps.getProperty(config.semverKey),
version.semver,
"version in foo properties should be the same"
)
}
@Test
fun testLoadSaveProperties() {
config.properties = propsFile.name
Utils.saveProperties(projectDir, config, version)
assertEquals(
propsFile.canReadFile(),
propsFile.canRead() && propsFile.isFile,
"properties file should exists and be readable"
)
val props = Utils.loadProperties(propsFile)
assertEquals(props.getProperty(config.majorKey), version.major.toString(), "Major")
assertEquals(props.getProperty(config.minorKey), version.minor.toString(), "Minor")
assertEquals(props.getProperty(config.patchKey), version.patch.toString(), "Patch")
assertEquals(props.getProperty(config.preReleaseKey), version.preRelease, "PreRelease")
assertNull(props.getProperty(config.preReleasePrefixKey), "PreRelease Prefix")
assertEquals(props.getProperty(config.buildMetaKey), version.buildMeta, "Build Meta")
assertNull(props.getProperty(config.buildMetaPrefixKey), "Build Meta Prefix")
assertNull(props.getProperty(config.separatorKey), "Separator")
assertEquals(props.getProperty(config.semverKey), version.semver, "semver")
propsFile.delete()
}
@Test
fun testLockedProperties() {
var locked = File("locked")
assertFailsWith<GradleException> {
Utils.loadProperties(File(locked, propsFile.name))
}
locked.delete()
locked = File(System.getProperty("user.home") + File.separator + "locked.properties")
locked.createNewFile()
locked.setReadOnly()
config.properties = locked.name
if (!locked.canWrite()) {
assertFailsWith<GradleException> {
Utils.saveProperties(locked.parentFile, config, version)
}
}
locked.delete()
}
@Test
fun testPrefix() {
version.preReleasePrefix = "."
version.buildMetaPrefix = "."
listOf("2.1.0.beta.1", "2.1.1.1", "3.2.1.beta.1.007").forEach {
assertTrue(Utils.parseSemVer(it, version), "parsing semver: $it")
assertEquals(it, version.semver, it)
}
assertEquals(version.preRelease, "beta", "last pre-release should match")
assertEquals(version.buildMeta, "1.007", "last meta should match")
}
@Test
fun testSystemProperties() {
val sysProps = arrayOf(
Pair(config.majorKey, "2"),
Pair(config.minorKey, "1"),
Pair(config.patchKey, "1"),
Pair(config.preReleaseKey, "beta"),
Pair(config.buildMetaKey, "007")
)
assertTrue(
Utils.isNotSystemProperty(
setOf(
config.majorKey,
config.minorKey,
config.patchKey,
config.preReleaseKey,
config.buildMetaKey
)
),
"none should already exists"
)
val props = Utils.loadProperties(propsFile)
sysProps.forEach {
val msg = "${it.first} should match system properties"
System.getProperties().setProperty(it.first, it.second)
if (it.first == config.majorKey || it.first == config.minorKey || it.first == config.patchKey) {
assertEquals(Utils.loadIntProperty(props, it.first, -1), it.second.toInt(), msg)
} else {
assertEquals(Utils.loadProperty(props, it.first, ""), it.second, msg)
}
}
Utils.loadVersion(config, version, props)
assertEquals(version.semver, "2.1.1-beta+007", "version should be identical")
Utils.saveProperties(projectDir, config, version)
val newPropsFile = File(config.properties)
val newProps = Utils.loadProperties(newPropsFile)
sysProps.forEach {
assertEquals(newProps.getProperty(it.first), it.second, "new properties should validate")
}
newPropsFile.delete()
System.getProperties().setProperty(config.semverKey, "3.2.2")
Utils.loadVersion(config, version, props)
assertEquals(version.semver, System.getProperty(config.semverKey), "versions should match")
}
@Test
fun testVersionParsing() {
listOf(
"1.0.0",
"2.1.0-beta",
"3.2.1-beta+007",
"4.3.2+007",
"11.11.1",
"111.11.11-beta",
"1111.111.11-beta+001.12"
).forEach {
assertTrue(Utils.parseSemVer(it, version), "parsing semver: $it")
assertEquals(it, version.semver, it)
}
}
}

View file

@ -0,0 +1,106 @@
/*
* SemverVersionTest.kt
*
* Copyright (c) 2018-2021, Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of this project nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.thauvin.erik.gradle.semver
import kotlin.test.Test
import kotlin.test.assertEquals
class VersionTest {
private val version = Version()
@Test
fun testDefaultVersion() {
assertEquals(1, version.major, "major should be 1")
assertEquals(0, version.minor, "minor should be 1")
assertEquals(0, version.patch, "patch should be 0")
assertEquals("", version.preRelease, "prerelease should be empty")
assertEquals("", version.buildMeta, "meta should be empty")
assertEquals("-", version.preReleasePrefix, "preRelease prefix should be -")
assertEquals("+", version.buildMetaPrefix, "meta prefix should be +")
assertEquals(".", version.separator, "separator should be .")
assertEquals("1.0.0", version.semver, "version should be 1.0.0")
}
@Test
fun testIncrement() {
version.increment(isMajor = true)
assertEquals("2.0.0", version.semver, "should return 2.0.0")
version.increment(isMinor = true)
assertEquals("2.1.0", version.semver, "should return 2.1.0")
version.increment(isPatch = true)
assertEquals("2.1.1", version.semver, "should return 2.1.1")
version.increment(isMinor = true)
assertEquals("2.2.0", version.semver, "should return 2.2.0")
version.increment(isMajor = true)
assertEquals("3.0.0", version.semver, "should return 3.0.0")
version.increment(isMajor = true, isMinor = true, isPatch = true)
assertEquals("4.1.1", version.semver, "should return 4.1.1")
version.increment(isMajor = true, isMinor = true)
assertEquals("5.1.0", version.semver, "should return 5.1.0")
version.increment(isMinor = true, isPatch = true)
assertEquals("5.2.1", version.semver, "should return 5.2.1")
version.increment()
assertEquals("5.2.1", version.semver, "should still return 5.2.1")
}
@Test
fun testVersion() {
version.major = 1
version.minor = 0
version.patch = 0
assertEquals("1.0.0", version.semver, "should return 1.0.0")
version.preRelease = "beta"
assertEquals("1.0.0-beta", version.semver, "should return 1.0.0-beta")
version.buildMeta = "007"
assertEquals("1.0.0-beta+007", version.semver, "should return 1.0.0-beta+007")
version.preReleasePrefix = "--"
assertEquals("1.0.0--beta+007", version.semver, "should return 1.0.0--beta+007")
version.buildMetaPrefix = "++"
assertEquals("1.0.0--beta++007", version.semver, "should return 1.0.0--beta++007")
version.separator = "-"
assertEquals("1-0-0--beta++007", version.semver, "should return 1-0-0--beta++007")
}
}