diff --git a/.shellspec b/.shellspec index dd850ae..4a21452 100644 --- a/.shellspec +++ b/.shellspec @@ -1,5 +1,5 @@ # kcov (coverage) options ---kcov-options "--include-pattern=build-poetry,get-build-number,pr_cleanup,promote,build-gradle,config-maven,build-maven,config-npm,build-npm,build-yarn,shared" +--kcov-options "--include-pattern=build-poetry,get-build-number,pr_cleanup,promote,build-gradle,config-maven,build-maven,config-npm,build-npm,build-yarn,shared,config-gradle" # --kcov-options "--exclude-pattern=.github,.idea,.git" # define minimum coverage (fail otherwise) diff --git a/build-gradle/action.yml b/build-gradle/action.yml index 1a19986..e1e7951 100644 --- a/build-gradle/action.yml +++ b/build-gradle/action.yml @@ -73,7 +73,7 @@ inputs: outputs: project-version: description: The release version set as Gradle project version in gradle.properties - value: ${{ steps.build.outputs.project-version }} + value: ${{ steps.config-gradle.outputs.project-version }} BUILD_NUMBER: description: The build number, incremented or reused if already cached value: ${{ steps.config-gradle.outputs.BUILD_NUMBER }} diff --git a/build-gradle/build.sh b/build-gradle/build.sh index 67842bf..594ddeb 100755 --- a/build-gradle/build.sh +++ b/build-gradle/build.sh @@ -12,6 +12,7 @@ # - SQC_EU_URL: URL of SonarQube server for sqc-eu platform # - SQC_EU_TOKEN: Access token to send analysis reports to SonarQube for sqc-eu platform # - RUN_SHADOW_SCANS: If true, run sonar scanner on all 3 platforms. If false, run on the platform provided by SONAR_PLATFORM. +# - CURRENT_VERSION: Current project version as in gradle.properties # - ARTIFACTORY_ACCESS_TOKEN: Access token to read Repox repositories # - ARTIFACTORY_DEPLOY_REPO: Name of deployment repository # - ARTIFACTORY_DEPLOY_USERNAME: Username to deploy to Artifactory @@ -52,6 +53,7 @@ source "$(dirname "${BASH_SOURCE[0]}")/../shared/common-functions.sh" : "${GITHUB_OUTPUT:?}" : "${PULL_REQUEST?}" "${DEFAULT_BRANCH:?}" : "${RUN_SHADOW_SCANS:?}" +: "${CURRENT_VERSION:?}" if [[ "${SONAR_PLATFORM:?}" != "none" ]]; then : "${NEXT_URL:?}" "${NEXT_TOKEN:?}" "${SQC_US_URL:?}" "${SQC_US_TOKEN:?}" "${SQC_EU_URL:?}" "${SQC_EU_TOKEN:?}" fi @@ -82,26 +84,6 @@ set_build_env() { git_fetch_unshallow } -set_project_version() { - current_version=$($GRADLE_CMD properties --no-scan --no-daemon --console plain | grep 'version:' | tr -d "[:space:]" | cut -d ":" -f 2) - if [[ -z "$current_version" || "$current_version" == "unspecified" ]]; then - echo "ERROR: Could not get valid version from Gradle properties. Got: '$current_version'" >&2 - exit 1 - fi - export CURRENT_VERSION=$current_version - release_version="${current_version/-SNAPSHOT/}" - if [[ "${release_version}" =~ ^[0-9]+\.[0-9]+$ ]]; then - release_version="${release_version}.0" - fi - release_version="${release_version}.${BUILD_NUMBER}" - echo "Replacing version $current_version with $release_version" - sed -i.bak "s/$current_version/$release_version/g" gradle.properties - echo "project-version=$release_version" >> "$GITHUB_OUTPUT" - echo "PROJECT_VERSION=$release_version" >> "$GITHUB_ENV" - echo "PROJECT_VERSION=$release_version" - export PROJECT_VERSION=$release_version -} - should_deploy() { # Disable deployment when explicitly requested if [[ "${DEPLOY}" != "true" ]]; then @@ -300,7 +282,6 @@ main() { check_tool java -version set_gradle_cmd set_build_env - set_project_version gradle_build } diff --git a/build-maven/build.sh b/build-maven/build.sh index dbf12e2..64ca553 100755 --- a/build-maven/build.sh +++ b/build-maven/build.sh @@ -17,6 +17,7 @@ # - ARTIFACTORY_DEPLOY_REPO: Deployment repository name # - ARTIFACTORY_DEPLOY_USERNAME: Username used by artifactory-maven-plugin # - ARTIFACTORY_DEPLOY_PASSWORD: Access token to deploy to the repository +# - CURRENT_VERSION: Current project version as in pom.xml # - DEFAULT_BRANCH: Default branch name (e.g. main) # - PULL_REQUEST: Pull request number (e.g. 1234) or empty string # diff --git a/config-gradle/action.yml b/config-gradle/action.yml index b090314..28aec1a 100644 --- a/config-gradle/action.yml +++ b/config-gradle/action.yml @@ -33,7 +33,13 @@ inputs: outputs: BUILD_NUMBER: description: The build number, incremented or reused if already cached - value: ${{ steps.get_build_number.outputs.BUILD_NUMBER }} + value: ${{ steps.get-build-number.outputs.BUILD_NUMBER }} + current-version: + description: The project version set in the gradle.properties (before replacement). Also set as environment variable CURRENT_VERSION + value: ${{ steps.set-version.outputs.current-version }} + project-version: + description: The project version with build number (after replacement). Also set as environment variable PROJECT_VERSION + value: ${{ steps.set-version.outputs.project-version }} runs: using: composite @@ -63,12 +69,20 @@ runs: echo "::endgroup::" - name: Get build number - id: get_build_number + id: get-build-number uses: ./.actions/get-build-number with: host-actions-root: ${{ steps.set-path.outputs.host_actions_root }} + - id: config-gradle-completed + if: env.CONFIG_GRADLE_COMPLETED != '' + shell: bash + run: | + echo "Action already called by $CONFIG_GRADLE_COMPLETED, execution skipped." + echo "skip=true" >> $GITHUB_OUTPUT + - name: Set parameter for Vault + if: steps.config-gradle-completed.outputs.skip != 'true' shell: bash env: ARTIFACTORY_READER_ROLE: ${{ inputs.artifactory-reader-role != '' && inputs.artifactory-reader-role || @@ -78,6 +92,7 @@ runs: - name: Get secrets from Vault id: secrets + if: steps.config-gradle-completed.outputs.skip != 'true' uses: SonarSource/vault-action-wrapper@320bd31b03e5dacaac6be51bbbb15adf7caccc32 # 3.1.0 with: secrets: | @@ -86,6 +101,7 @@ runs: ${{ inputs.use-develocity == 'true' && 'development/kv/data/develocity token | DEVELOCITY_TOKEN;' || '' }} - name: Set environment variables for Artifactory authentication + if: steps.config-gradle-completed.outputs.skip != 'true' shell: bash env: ARTIFACTORY_URL: @@ -117,6 +133,7 @@ runs: # Configure Gradle for comprehensive caching and build summary reporting. It does not install Gradle. - name: Configure Gradle + if: steps.config-gradle-completed.outputs.skip != 'true' uses: gradle/actions/setup-gradle@748248ddd2a24f49513d8f472f81c3a07d4d50e1 # v4.4.4 with: develocity-injection-enabled: ${{ inputs.use-develocity == 'true' }} @@ -124,7 +141,7 @@ runs: develocity-plugin-version: '4.0' - name: Generate Gradle Cache Key - if: ${{ inputs.disable-caching != 'true' }} + if: steps.config-gradle-completed.outputs.skip != 'true' && inputs.disable-caching != 'true' shell: bash run: | # Generate cache key from all Gradle files @@ -139,7 +156,7 @@ runs: rm -f gradle-md5-sums.txt - name: Restore Gradle Cache - if: ${{ inputs.disable-caching != 'true' }} + if: steps.config-gradle-completed.outputs.skip != 'true' && inputs.disable-caching != 'true' uses: SonarSource/gh-action_cache@v1 id: gradle-cache-restore with: @@ -150,22 +167,34 @@ runs: # $GRADLE_USER_HOME is typically set to ~/.gradle/ by gradle/actions/setup-gradle - name: Configure Gradle Authentication + if: steps.config-gradle-completed.outputs.skip != 'true' shell: bash run: | GRADLE_INIT_DIR="$GRADLE_USER_HOME/init.d" mkdir -p "$GRADLE_INIT_DIR" cp "${GITHUB_ACTION_PATH}/resources/repoxAuth.init.gradle.kts" "$GRADLE_INIT_DIR/" + - name: Update project version and set current-version and project-version variables + id: set-version + if: steps.config-gradle-completed.outputs.skip != 'true' + shell: bash + run: ${GITHUB_ACTION_PATH}/set_gradle_project_version.sh + - name: Extract Develocity hostname id: develocity-hostname - if: ${{ inputs.use-develocity == 'true' }} + if: steps.config-gradle-completed.outputs.skip != 'true' && inputs.use-develocity == 'true' shell: bash run: echo "hostname=$(echo '${{ inputs.develocity-url }}' | sed 's|https://||' | sed 's|/$||')" >> $GITHUB_OUTPUT - name: Deactivate UseContainerSupport on github-ubuntu-* runners - if: runner.os == 'Linux' && runner.environment == 'github-hosted' + if: steps.config-gradle-completed.outputs.skip != 'true' && runner.os == 'Linux' && runner.environment == 'github-hosted' shell: bash run: | echo "::warning title=Deactivating UseContainerSupport::The GitHub-hosted Ubuntu runners have an issue with Java's" \ "UseContainerSupport feature." echo "JAVA_TOOL_OPTIONS=${JAVA_TOOL_OPTIONS:=-XX:-UseContainerSupport}" >> "$GITHUB_ENV" + + - name: Set Config Gradle completed + if: steps.config-gradle-completed.outputs.skip != 'true' + shell: bash + run: echo "CONFIG_GRADLE_COMPLETED=$GITHUB_ACTION" >> "$GITHUB_ENV" diff --git a/config-gradle/set_gradle_project_version.sh b/config-gradle/set_gradle_project_version.sh new file mode 100755 index 0000000..14f3b53 --- /dev/null +++ b/config-gradle/set_gradle_project_version.sh @@ -0,0 +1,69 @@ +#!/bin/bash +# Config script for SonarSource Maven projects. +# +# Required environment variables (must be explicitly provided): +# - BUILD_NUMBER: Build number for versioning +# +# GitHub Actions auto-provided: +# - GITHUB_OUTPUT: Path to GitHub Actions output file +# - GITHUB_ENV: Path to GitHub Actions environment file +# +# Optional user customization: +# - CURRENT_VERSION and PROJECT_VERSION: If both are set, they will be used as-is and no version update will be performed. + +set -euo pipefail + +: "${BUILD_NUMBER:?}" +: "${GITHUB_OUTPUT:?}" "${GITHUB_ENV:?}" + +# shellcheck source=SCRIPTDIR/../shared/common-functions.sh +source "$(dirname "${BASH_SOURCE[0]}")/../shared/common-functions.sh" + +set_gradle_cmd() { + if [[ -f "./gradlew" ]]; then + export GRADLE_CMD="./gradlew" + elif check_tool gradle; then + export GRADLE_CMD="gradle" + else + echo "Neither ./gradlew nor gradle command found!" >&2 + exit 1 + fi +} + +set_project_version() { + local current_version + current_version=$($GRADLE_CMD properties --no-scan --no-daemon --console plain | grep 'version:' | tr -d "[:space:]" | cut -d ":" -f 2) + if [[ -z "$current_version" || "$current_version" == "unspecified" ]]; then + echo "ERROR: Could not get valid version from Gradle properties. Got: '$current_version'" >&2 + return 1 + fi + + # Saving the snapshot version to the output and environment variables + # This is used by the sonar-scanner to set the value of sonar.projectVersion without the build number + echo "CURRENT_VERSION=$current_version" + echo "CURRENT_VERSION=$current_version" >> "$GITHUB_ENV" + export CURRENT_VERSION=$current_version + + release_version="${current_version/-SNAPSHOT/}" + if [[ "${release_version}" =~ ^[0-9]+\.[0-9]+$ ]]; then + release_version="${release_version}.0" + fi + release_version="${release_version}.${BUILD_NUMBER}" + echo "Replacing version $current_version with $release_version" + sed -i.bak "s/$current_version/$release_version/g" gradle.properties + echo "project-version=$release_version" >> "$GITHUB_OUTPUT" + echo "PROJECT_VERSION=$release_version" >> "$GITHUB_ENV" + echo "PROJECT_VERSION=$release_version" + export PROJECT_VERSION=$release_version +} + +if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then + if [[ -n "${CURRENT_VERSION:-}" && -n "${PROJECT_VERSION:-}" ]]; then + echo "Using provided CURRENT_VERSION $CURRENT_VERSION and PROJECT_VERSION $PROJECT_VERSION without changes." + echo "current-version=$CURRENT_VERSION" >> "$GITHUB_OUTPUT" + echo "project-version=$PROJECT_VERSION" >> "$GITHUB_OUTPUT" + else + set_gradle_cmd + set_project_version + fi +fi diff --git a/config-maven/action.yml b/config-maven/action.yml index daed605..c4a3548 100644 --- a/config-maven/action.yml +++ b/config-maven/action.yml @@ -37,13 +37,13 @@ inputs: outputs: BUILD_NUMBER: description: The current build number. Also set as environment variable BUILD_NUMBER - value: ${{ steps.get_build_number.outputs.BUILD_NUMBER }} + value: ${{ steps.get-build-number.outputs.BUILD_NUMBER }} current-version: description: The project version set in the pom.xml (before replacement). Also set as environment variable CURRENT_VERSION - value: ${{ steps.config.outputs.current-version }} + value: ${{ steps.set-version.outputs.current-version }} project-version: description: The project version with build number (after replacement). Also set as environment variable PROJECT_VERSION - value: ${{ steps.config.outputs.project-version }} + value: ${{ steps.set-version.outputs.project-version }} runs: using: composite @@ -74,7 +74,7 @@ runs: echo "::endgroup::" - uses: ./.actions/get-build-number - id: get_build_number + id: get-build-number with: host-actions-root: ${{ steps.set-path.outputs.host_actions_root }} @@ -173,7 +173,7 @@ runs: restore-keys: maven-${{ runner.os }}-${{ github.workflow }}- - name: Update project version and set current-version and project-version variables - id: config + id: set-version if: steps.from-env.outputs.skip != 'true' shell: bash working-directory: ${{ inputs.working-directory }} diff --git a/config-maven/set_maven_project_version.sh b/config-maven/set_maven_project_version.sh index 040e34a..53bf7ec 100755 --- a/config-maven/set_maven_project_version.sh +++ b/config-maven/set_maven_project_version.sh @@ -34,13 +34,6 @@ get_current_version() { # Update current_version variable with the current project version. # Then remove the -SNAPSHOT suffix if present, complete with '.0' if needed, and append the build number at the end. set_project_version() { - if [[ -n "${CURRENT_VERSION:-}" && -n "${PROJECT_VERSION:-}" ]]; then - echo "Using provided CURRENT_VERSION $CURRENT_VERSION and PROJECT_VERSION $PROJECT_VERSION without changes." - echo "current-version=$CURRENT_VERSION" >> "$GITHUB_OUTPUT" - echo "project-version=$PROJECT_VERSION" >> "$GITHUB_OUTPUT" - return 0 - fi - local current_version if ! current_version=$(get_current_version 2>&1); then echo -e "::error file=pom.xml,title=Maven project version::Could not get 'project.version' from Maven project\nERROR: $current_version" @@ -79,6 +72,12 @@ set_project_version() { } if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then - check_tool mvn --version - set_project_version + if [[ -n "${CURRENT_VERSION:-}" && -n "${PROJECT_VERSION:-}" ]]; then + echo "Using provided CURRENT_VERSION $CURRENT_VERSION and PROJECT_VERSION $PROJECT_VERSION without changes." + echo "current-version=$CURRENT_VERSION" >> "$GITHUB_OUTPUT" + echo "project-version=$PROJECT_VERSION" >> "$GITHUB_OUTPUT" + else + check_tool mvn --version + set_project_version + fi fi diff --git a/spec/build-gradle_spec.sh b/spec/build-gradle_spec.sh index 87f6b28..15fb7dc 100755 --- a/spec/build-gradle_spec.sh +++ b/spec/build-gradle_spec.sh @@ -62,6 +62,7 @@ export DEPLOY_PULL_REQUEST="false" export SKIP_TESTS="false" export GRADLE_ARGS="" export GITHUB_OUTPUT=/dev/null +export CURRENT_VERSION="1.2.3-SNAPSHOT" # Duplicate environment variables removed GITHUB_EVENT_PATH=$(mktemp) export GITHUB_EVENT_PATH @@ -129,59 +130,6 @@ Describe 'set_build_env' End End -Describe 'set_project_version' - It 'processes version correctly' - echo "version=1.0-SNAPSHOT" > gradle.properties - When call set_project_version - The output should include "Replacing version 1.2.3-SNAPSHOT with 1.2.3.42" - The variable CURRENT_VERSION should equal "1.2.3-SNAPSHOT" - The variable PROJECT_VERSION should equal "1.2.3.42" - rm -f gradle.properties gradle.properties.bak - End - - It 'adds .0 to two-digit version' - echo "version=1.2-SNAPSHOT" > gradle.properties - Mock gradle - echo "version: 1.2-SNAPSHOT" - End - When call set_project_version - The output should include "Replacing version 1.2-SNAPSHOT with 1.2.0.42" - The variable CURRENT_VERSION should equal "1.2-SNAPSHOT" - The variable PROJECT_VERSION should equal "1.2.0.42" - rm -f gradle.properties gradle.properties.bak - End - - It 'fails when version is empty' - echo "version=1.0-SNAPSHOT" > gradle.properties - Mock gradle - if [[ "$*" == "properties --no-scan --no-daemon --console plain" ]]; then - echo "version: " - else - echo "gradle $*" - fi - End - When run set_project_version - The status should be failure - The stderr should include "ERROR: Could not get valid version from Gradle properties" - rm -f gradle.properties gradle.properties.bak - End - - It 'fails when version is unspecified' - echo "version=1.0-SNAPSHOT" > gradle.properties - Mock gradle - if [[ "$*" == "properties --no-scan --no-daemon --console plain" ]]; then - echo "version: unspecified" - else - echo "gradle $*" - fi - End - When run set_project_version - The status should be failure - The stderr should include "ERROR: Could not get valid version from Gradle properties. Got: 'unspecified'" - rm -f gradle.properties gradle.properties.bak - End -End - Describe 'should_deploy' It 'does not deploy when deployment is disabled' export DEPLOY="false" @@ -537,9 +485,6 @@ Describe 'main function' Mock set_build_env echo "env set" End - Mock set_project_version - echo "version set" - End Mock gradle_build echo "build done" End @@ -549,8 +494,7 @@ Describe 'main function' The line 1 should equal "java ok" The line 2 should equal "gradle ok" The line 3 should equal "env set" - The line 4 should equal "version set" - The line 5 should equal "build done" + The line 4 should equal "build done" End End diff --git a/spec/build-npm_spec.sh b/spec/build-npm_spec.sh index 836de44..d9215bc 100755 --- a/spec/build-npm_spec.sh +++ b/spec/build-npm_spec.sh @@ -172,7 +172,6 @@ Describe 'export_built_artifacts()' It 'skips silently when should-deploy=false' GITHUB_OUTPUT=$(mktemp) export GITHUB_OUTPUT - rm -rf .attestation-artifacts mkdir -p .attestation-artifacts touch .attestation-artifacts/ignored-1.0.0.tgz echo "deployed=false" >> "$GITHUB_OUTPUT" @@ -180,6 +179,7 @@ Describe 'export_built_artifacts()' When call export_built_artifacts The status should be success The output should be blank + rm -rf .attestation-artifacts End End diff --git a/spec/build-poetry_spec.sh b/spec/build-poetry_spec.sh index dea99a8..47d617c 100755 --- a/spec/build-poetry_spec.sh +++ b/spec/build-poetry_spec.sh @@ -128,19 +128,16 @@ Describe 'set_build_env()' End Describe 'export_built_artifacts()' + # shellcheck disable=SC2329,SC2317 # Function invoked indirectly by Before setup() { - # shellcheck disable=SC2317 mkdir -p dist - # shellcheck disable=SC2317 return 0 } + # shellcheck disable=SC2329,SC2317 # Function invoked indirectly by After cleanup() { - # shellcheck disable=SC2317 [[ -f "$GITHUB_OUTPUT" ]] && rm -f "$GITHUB_OUTPUT" - # shellcheck disable=SC2317 rm -rf dist - # shellcheck disable=SC2317 return 0 } Before 'setup' diff --git a/spec/build-yarn_spec.sh b/spec/build-yarn_spec.sh index 3cdccbd..22754eb 100755 --- a/spec/build-yarn_spec.sh +++ b/spec/build-yarn_spec.sh @@ -137,13 +137,13 @@ Describe 'build-yarn/build.sh' It 'skips silently when should-deploy=false' echo "should-deploy=false" >> "$GITHUB_OUTPUT" - rm -rf .attestation-artifacts mkdir -p .attestation-artifacts touch .attestation-artifacts/ignored-1.0.0.tgz When call export_built_artifacts The status should be success The output should be blank + rm -rf .attestation-artifacts End End diff --git a/spec/set_gradle_project_version_spec.sh b/spec/set_gradle_project_version_spec.sh new file mode 100644 index 0000000..468d7b5 --- /dev/null +++ b/spec/set_gradle_project_version_spec.sh @@ -0,0 +1,206 @@ +#!/bin/bash +eval "$(shellspec - -c) exit 1" + +# Set required environment variables for script inclusion +export BUILD_NUMBER="1" +export GITHUB_ENV=/dev/null +export GITHUB_OUTPUT=/dev/null + +Mock gradle + if [[ "$*" == "properties --no-scan --no-daemon --console plain" ]]; then + echo "version: 1.2.3-SNAPSHOT" + else + echo "gradle $*" + fi +End + +Describe 'config-gradle/set_gradle_project_version.sh' + It 'does not run main when sourced' + When run source config-gradle/set_gradle_project_version.sh + The status should be success + The output should equal "" + End +End + +Include config-gradle/set_gradle_project_version.sh + +Describe 'set_gradle_cmd()' + It 'uses gradlew when available' + touch ./gradlew + + When call set_gradle_cmd + The status should be success + The lines of output should equal 0 + The variable GRADLE_CMD should equal "./gradlew" + + rm -f ./gradlew + End + + It 'uses gradle when gradlew not found' + Mock check_tool + [[ "$1" == "gradle" ]] && true + End + When call set_gradle_cmd + The status should be success + The lines of output should equal 0 + The variable GRADLE_CMD should equal "gradle" + End + + It 'fails when neither gradle nor gradlew are available' + Mock check_tool + [[ "$1" == "gradle" ]] && false + End + When run set_gradle_cmd + The status should be failure + The lines of output should equal 0 + The lines of error should equal 1 + The line 1 of error should equal "Neither ./gradlew nor gradle command found!" + End +End + +Describe 'set_project_version()' + # shellcheck disable=SC2329,SC2317 # Function invoked indirectly by BeforeEach + common_setup() { + BUILD_NUMBER="42" + GITHUB_OUTPUT=$(mktemp) + GITHUB_ENV=$(mktemp) + # shellcheck disable=SC2034 # Used by set_project_version() function + GRADLE_CMD="gradle" + echo "version=1.0-SNAPSHOT" > gradle.properties + return 0 + } + # shellcheck disable=SC2329,SC2317 # Function invoked indirectly by AfterEach + common_cleanup() { + [[ -f "$GITHUB_OUTPUT" ]] && rm "$GITHUB_OUTPUT" + [[ -f "$GITHUB_ENV" ]] && rm "$GITHUB_ENV" + rm -f gradle.properties gradle.properties.bak + return 0 + } + + BeforeEach 'common_setup' + AfterEach 'common_cleanup' + + It 'processes version correctly' + Mock gradle + echo "version: 1.2.3-SNAPSHOT" + End + When call set_project_version + The status should be success + The lines of output should equal 3 + The line 1 should equal "CURRENT_VERSION=1.2.3-SNAPSHOT" + The line 2 should equal "Replacing version 1.2.3-SNAPSHOT with 1.2.3.42" + The line 3 should equal "PROJECT_VERSION=1.2.3.42" + The variable CURRENT_VERSION should equal "1.2.3-SNAPSHOT" + The variable PROJECT_VERSION should equal "1.2.3.42" + The lines of contents of file "$GITHUB_OUTPUT" should equal 1 + The line 1 of contents of file "$GITHUB_OUTPUT" should equal "project-version=1.2.3.42" + The lines of contents of file "$GITHUB_ENV" should equal 2 + The line 1 of contents of file "$GITHUB_ENV" should equal "CURRENT_VERSION=1.2.3-SNAPSHOT" + The line 2 of contents of file "$GITHUB_ENV" should equal "PROJECT_VERSION=1.2.3.42" + End + + It 'adds .0 to two-digit version' + Mock gradle + echo "version: 1.2-SNAPSHOT" + End + When call set_project_version + The status should be success + The lines of output should equal 3 + The line 1 should equal "CURRENT_VERSION=1.2-SNAPSHOT" + The line 2 should equal "Replacing version 1.2-SNAPSHOT with 1.2.0.42" + The line 3 should equal "PROJECT_VERSION=1.2.0.42" + The variable CURRENT_VERSION should equal "1.2-SNAPSHOT" + The variable PROJECT_VERSION should equal "1.2.0.42" + The lines of contents of file "$GITHUB_OUTPUT" should equal 1 + The line 1 of contents of file "$GITHUB_OUTPUT" should equal "project-version=1.2.0.42" + The lines of contents of file "$GITHUB_ENV" should equal 2 + The line 1 of contents of file "$GITHUB_ENV" should equal "CURRENT_VERSION=1.2-SNAPSHOT" + The line 2 of contents of file "$GITHUB_ENV" should equal "PROJECT_VERSION=1.2.0.42" + End + + It 'fails when version is empty' + Mock gradle + echo "version:" + End + When run set_project_version + The status should be failure + The lines of output should equal 0 + The lines of error should equal 1 + The line 1 of error should equal "ERROR: Could not get valid version from Gradle properties. Got: ''" + End + + It 'fails when version is unspecified' + Mock gradle + echo "version: unspecified" + End + When run set_project_version + The status should be failure + The lines of output should equal 0 + The lines of error should equal 1 + The line 1 of error should equal "ERROR: Could not get valid version from Gradle properties. Got: 'unspecified'" + End +End + +Describe 'main()' + common_setup() { + BUILD_NUMBER="42" + GITHUB_OUTPUT=$(mktemp) + GITHUB_ENV=$(mktemp) + echo "version=1.0-SNAPSHOT" > gradle.properties + return 0 + } + + common_cleanup() { + rm -f "$GITHUB_OUTPUT" "$GITHUB_ENV" gradle.properties gradle.properties.bak + return 0 + } + + BeforeEach 'common_setup' + AfterEach 'common_cleanup' + + # Mock gradle to prove set_gradle_cmd and set_project_version were called + Mock gradle + # Output from set_project_version + echo "version: 1.2.4-SNAPSHOT" + End + + It 'runs tool checks and calls set_project_version' + When run script config-gradle/set_gradle_project_version.sh + The status should be success + The lines of output should equal 5 + The line 1 should include "gradle" + The line 2 should equal "version: 1.2.4-SNAPSHOT" + The line 3 should equal "CURRENT_VERSION=1.2.4-SNAPSHOT" + The line 4 should equal "Replacing version 1.2.4-SNAPSHOT with 1.2.4.42" + The line 5 should equal "PROJECT_VERSION=1.2.4.42" + End + + It 'uses provided CURRENT_VERSION and PROJECT_VERSION without changes' + export CURRENT_VERSION="1.2.3-SNAPSHOT" + export PROJECT_VERSION="1.2.3.42" + + When run script config-gradle/set_gradle_project_version.sh + The status should be success + The lines of output should equal 1 + The line 1 should equal "Using provided CURRENT_VERSION 1.2.3-SNAPSHOT and PROJECT_VERSION 1.2.3.42 without changes." + The lines of contents of file "$GITHUB_OUTPUT" should equal 2 + The line 1 of contents of file "$GITHUB_OUTPUT" should equal "current-version=1.2.3-SNAPSHOT" + The line 2 of contents of file "$GITHUB_OUTPUT" should equal "project-version=1.2.3.42" + End + + It 'proceeds normally when only CURRENT_VERSION is provided' + export CURRENT_VERSION="1.2.3-SNAPSHOT" + + When run script config-gradle/set_gradle_project_version.sh + The status should be success + The lines of output should equal 5 + End + + It 'proceeds normally when only PROJECT_VERSION is provided' + export PROJECT_VERSION="1.2.3.42" + + When run script config-gradle/set_gradle_project_version.sh + The status should be success + The lines of output should equal 5 + End +End diff --git a/spec/set_maven_project_version_spec.sh b/spec/set_maven_project_version_spec.sh index 327d8a0..bbea3eb 100644 --- a/spec/set_maven_project_version_spec.sh +++ b/spec/set_maven_project_version_spec.sh @@ -40,16 +40,18 @@ Describe 'get_current_version()' End Describe 'set_project_version()' + # shellcheck disable=SC2329,SC2317 # Function invoked indirectly by BeforeEach common_setup() { - export BUILD_NUMBER="999" + BUILD_NUMBER="999" GITHUB_OUTPUT=$(mktemp) - export GITHUB_OUTPUT GITHUB_ENV=$(mktemp) - export GITHUB_ENV + return 0 } + # shellcheck disable=SC2329,SC2317 # Function invoked indirectly by AfterEach common_cleanup() { [[ -f "$GITHUB_OUTPUT" ]] && rm "$GITHUB_OUTPUT" [[ -f "$GITHUB_ENV" ]] && rm "$GITHUB_ENV" + return 0 } BeforeEach 'common_setup' @@ -136,31 +138,76 @@ Describe 'set_project_version()' The line 1 of output should include "Could not get 'project.version' from Maven project" The line 2 of output should start with "ERROR:" End - - It 'skips version update when already set in environment' - export CURRENT_VERSION="1.2.3-SNAPSHOT" - export PROJECT_VERSION="1.2.3-42" - export BUILD_NUMBER="42" - When call set_project_version - The status should be success - The line 1 should equal "Using provided CURRENT_VERSION 1.2.3-SNAPSHOT and PROJECT_VERSION 1.2.3-42 without changes." - The contents of file "$GITHUB_OUTPUT" should include "current-version=1.2.3-SNAPSHOT" - The contents of file "$GITHUB_OUTPUT" should include "project-version=1.2.3-42" - End End Describe 'main()' - Mock get_current_version - echo "1.2.3-SNAPSHOT" + common_setup() { + BUILD_NUMBER="1" + GITHUB_OUTPUT=$(mktemp) + GITHUB_ENV=$(mktemp) + return 0 + } + + common_cleanup() { + rm -f "$GITHUB_OUTPUT" "$GITHUB_ENV" + return 0 + } + + BeforeEach 'common_setup' + AfterEach 'common_cleanup' + + Mock mvn + if [[ "$*" == "--version" ]]; then + echo "mvn --version" + elif [[ "$*" == *"exec-maven-plugin"* ]]; then + echo "1.2.3-SNAPSHOT" + elif [[ "$*" == *"versions-maven-plugin"* ]]; then + echo "mvn versions" + fi End + It 'runs tool checks and calls set_project_version' - export CURRENT_VERSION="1.2.3-SNAPSHOT" - export PROJECT_VERSION="1.2.3-42" When run script config-maven/set_maven_project_version.sh The status should be success - The lines of stdout should equal 3 + The lines of output should equal 7 The line 1 should include "mvn" The line 2 should equal "mvn --version" - The line 3 should start with "Using provided CURRENT_VERSION 1.2.3-SNAPSHOT and PROJECT_VERSION 1.2.3-42 without changes." + The line 3 should equal "CURRENT_VERSION=1.2.3-SNAPSHOT (from pom.xml)" + The line 4 should equal "Replacing version 1.2.3-SNAPSHOT with 1.2.3.1" + The line 5 should start with "Maven command:" + The line 6 should equal "mvn versions" + The line 7 should equal "PROJECT_VERSION=1.2.3.1" + End + + It 'uses provided CURRENT_VERSION and PROJECT_VERSION without changes' + export BUILD_NUMBER="999" + export CURRENT_VERSION="1.2.3-SNAPSHOT" + export PROJECT_VERSION="1.2.3.999" + + When run script config-maven/set_maven_project_version.sh + The status should be success + The lines of output should equal 1 + The line 1 should equal "Using provided CURRENT_VERSION 1.2.3-SNAPSHOT and PROJECT_VERSION 1.2.3.999 without changes." + The lines of contents of file "$GITHUB_OUTPUT" should equal 2 + The line 1 of contents of file "$GITHUB_OUTPUT" should equal "current-version=1.2.3-SNAPSHOT" + The line 2 of contents of file "$GITHUB_OUTPUT" should equal "project-version=1.2.3.999" + End + + It 'proceeds normally when only CURRENT_VERSION is provided' + export BUILD_NUMBER="999" + export CURRENT_VERSION="1.2.3-SNAPSHOT" + + When run script config-maven/set_maven_project_version.sh + The status should be success + The lines of output should equal 7 + End + + It 'proceeds normally when only PROJECT_VERSION is provided' + export BUILD_NUMBER="999" + export PROJECT_VERSION="1.2.3.999" + + When run script config-maven/set_maven_project_version.sh + The status should be success + The lines of output should equal 7 End End