Skip to content

Commit

Permalink
Improve version regex and add tests (#616)
Browse files Browse the repository at this point in the history
Co-authored-by: Joaquim Stähli <[email protected]>
  • Loading branch information
MGaetan89 and StaehliJ authored Jul 2, 2024
1 parent 4eac1bd commit 5365411
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 19 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ jobs:
- name: Run Unit Tests
run: >
./gradlew
:build-logic:plugins:koverXmlReport
:pillarbox-analytics:koverXmlReportDebug
:pillarbox-cast:koverXmlReportDebug
:pillarbox-core-business:koverXmlReportDebug
Expand Down
14 changes: 7 additions & 7 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ on:
push:
# Pattern matched against refs/tags
tags:
- '[0-9]+.[0-9].[0-9]'
- '[0-9]+.[0-9].[0-9]-[0-9a-zA-Z]+'
- '[0-9]+.[0-9]+.[0-9]+'
- '[0-9]+.[0-9]+.[0-9]+-[0-9a-zA-Z]+'
jobs:
build:
runs-on: ubuntu-latest
Expand All @@ -19,7 +19,7 @@ jobs:
- name: Check if pre release tag
id: check-tag
run: |
if [[ "${GITHUB_REF_NAME}" =~ [0-9]+.[0-9].[0-9]-[0-9a-zA-Z]+ ]]; then
if [[ "${GITHUB_REF_NAME}" =~ ^[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,2}-[0-9a-zA-Z]+$ ]]; then
echo "prerelease=true" >> $GITHUB_OUTPUT
fi
- name: Print release tag
Expand Down Expand Up @@ -52,7 +52,7 @@ jobs:
- name: Create Github release
uses: ncipollo/release-action@v1
with:
draft: true
prerelease: steps.check-tag.outputs.prerelease == 'true'
skipIfReleaseExists: true
generateReleaseNotes: true
draft: true
prerelease: steps.check-tag.outputs.prerelease == 'true'
skipIfReleaseExists: true
generateReleaseNotes: true
3 changes: 3 additions & 0 deletions build-logic/plugins/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
`kotlin-dsl`
alias(libs.plugins.kotlinx.kover)
}

group = "ch.srgssr.pillarbox.gradle"
Expand All @@ -26,6 +27,8 @@ dependencies {
compileOnly(libs.android.gradle.api)
compileOnly(libs.kotlinx.kover.gradle)
compileOnly(libs.kotlin.gradle.plugin)

testImplementation(libs.kotlin.test)
}

tasks {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class PillarboxAndroidApplicationPlugin : Plugin<Project> {
defaultConfig {
applicationId = namespace
targetSdk = AppConfig.targetSdk
versionCode = VersionConfig.versionCode()
versionName = VersionConfig.versionName()
versionCode = VersionConfig().versionCode()
versionName = VersionConfig().versionName()
vectorDrawables.useSupportLibrary = true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class PillarboxAndroidLibraryPublishingPlugin : Plugin<Project> {
extensions.configure<LibraryExtension> {
defaultConfig {
group = "ch.srgssr.pillarbox"
version = VersionConfig.versionName()
version = VersionConfig().versionName()
}

publishing {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ package ch.srgssr.pillarbox.gradle.internal
* VersionConfig will build
* - VersionName for Pillarbox Demo
* - Version for Libraries
*
* @param envVersionName Environment variable set by workflow.
*/
internal object VersionConfig {
/**
* Environment variable set by workflow.
*/
private val ENV_VERSION_NAME: String? = System.getenv("VERSION_NAME")
private val versionOnlyRegex = "[0-9]+.[0-9].[0-9]".toRegex()
internal class VersionConfig(
private val envVersionName: String? = System.getenv("VERSION_NAME"),
) {
private val versionOnlyRegex = "^[0-9]{1,2}\\.[0-9]{1,2}\\.[0-9]{1,2}$".toRegex()

/**
* Version name
*
* @return "Local" if [ENV_VERSION_NAME] no set.
* @return "Local" if [envVersionName] no set.
*/
internal fun versionName(): String {
return ENV_VERSION_NAME ?: "Local"
return envVersionName ?: "Local"
}

/**
Expand All @@ -31,7 +31,7 @@ internal object VersionConfig {
* 0.0.0, 0.0.99, 0.1.0, 0.99.99
*/
internal fun versionCode(): Int {
return ENV_VERSION_NAME
return envVersionName
?.let { versionOnlyRegex.find(it)?.value }
?.let {
val versions = it.split(".").map { value -> value.toInt() }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/
package ch.srgssr.pillarbox.gradle.internal

import org.junit.runner.RunWith
import org.junit.runners.Parameterized
import org.junit.runners.Parameterized.Parameters
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals

@RunWith(Parameterized::class)
class VersionConfigTest(
private val envVersionName: String?,
private val versionName: String,
private val versionCode: Int,
) {
private lateinit var versionConfig: VersionConfig

@BeforeTest
fun setUp() {
versionConfig = VersionConfig(envVersionName)
}

@Test
fun `version name`() {
assertEquals(versionName, versionConfig.versionName())
}

@Test
fun `version code`() {
assertEquals(versionCode, versionConfig.versionCode())
}

companion object {
@JvmStatic
@Parameters(name = "{index}: envVersionName={0}, versionName={1}, versionCode={2}")
fun parameters(): Iterable<Any> {
return listOf(
// Invalid envVersionName
arrayOf(null, "Local", 9999),
arrayOf("", "", 9999),
arrayOf("Foo", "Foo", 9999),

// Invalid version format
arrayOf("1.2", "1.2", 9999),
arrayOf("1a2b3", "1a2b3", 9999),
arrayOf("1.2.3.4", "1.2.3.4", 9999),
arrayOf("111.2.3", "111.2.3", 9999),
arrayOf("1.222.3", "1.222.3", 9999),
arrayOf("1.2.333", "1.2.333", 9999),

// Valid version format
arrayOf("0.0.0", "0.0.0", 0),
arrayOf("1.2.3", "1.2.3", 10203),
arrayOf("12.34.56", "12.34.56", 123456),
arrayOf("01.02.03", "01.02.03", 10203),
arrayOf("99.99.99", "99.99.99", 999999),
)
}
}
}
8 changes: 8 additions & 0 deletions build-logic/settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
* License information is available from the LICENSE file.
*/

pluginManagement {
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
}

dependencyResolutionManagement {
repositoriesMode = RepositoriesMode.FAIL_ON_PROJECT_REPOS

Expand Down

0 comments on commit 5365411

Please sign in to comment.