diff --git a/.gitignore b/.gitignore index dcea54c..08d1fd1 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,6 @@ out *.iml *.ipr *.iws + +.vscode/ +bin/ \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index 943f0cb..a4b76b9 100644 Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 5083229..df97d72 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,7 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip networkTimeout=10000 +validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew index 65dcd68..f5feea6 100755 --- a/gradlew +++ b/gradlew @@ -15,6 +15,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +# SPDX-License-Identifier: Apache-2.0 +# ############################################################################## # @@ -55,7 +57,7 @@ # Darwin, MinGW, and NonStop. # # (3) This script is generated from the Groovy template -# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt # within the Gradle project. # # You can find Gradle at https://github.com/gradle/gradle/. @@ -83,10 +85,9 @@ done # This is normally unused # shellcheck disable=SC2034 APP_BASE_NAME=${0##*/} -APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit - -# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s +' "$PWD" ) || exit # Use the maximum available, or set MAX_FD != -1 to use that value. MAX_FD=maximum @@ -133,10 +134,13 @@ location of your Java installation." fi else JAVACMD=java - which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. Please set the JAVA_HOME variable in your environment to match the location of your Java installation." + fi fi # Increase the maximum file descriptors if we can. @@ -144,7 +148,7 @@ if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then case $MAX_FD in #( max*) # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. - # shellcheck disable=SC3045 + # shellcheck disable=SC2039,SC3045 MAX_FD=$( ulimit -H -n ) || warn "Could not query maximum file descriptor limit" esac @@ -152,7 +156,7 @@ if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then '' | soft) :;; #( *) # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. - # shellcheck disable=SC3045 + # shellcheck disable=SC2039,SC3045 ulimit -n "$MAX_FD" || warn "Could not set maximum file descriptor limit to $MAX_FD" esac @@ -197,11 +201,15 @@ if "$cygwin" || "$msys" ; then done fi -# Collect all arguments for the java command; -# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of -# shell script including quotes and variable substitutions, so put them in -# double quotes to make sure that they get re-expanded; and -# * put everything else in single quotes, so that it's not re-expanded. + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. set -- \ "-Dorg.gradle.appname=$APP_BASE_NAME" \ diff --git a/gradlew.bat b/gradlew.bat index 93e3f59..9d21a21 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -13,6 +13,8 @@ @rem See the License for the specific language governing permissions and @rem limitations under the License. @rem +@rem SPDX-License-Identifier: Apache-2.0 +@rem @if "%DEBUG%"=="" @echo off @rem ########################################################################## @@ -43,11 +45,11 @@ set JAVA_EXE=java.exe %JAVA_EXE% -version >NUL 2>&1 if %ERRORLEVEL% equ 0 goto execute -echo. -echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 goto fail @@ -57,11 +59,11 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe if exist "%JAVA_EXE%" goto execute -echo. -echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 goto fail diff --git a/src/main/java/org/shipkit/auto/version/AutoVersion.java b/src/main/java/org/shipkit/auto/version/AutoVersion.java index e43b0b3..26e4edf 100644 --- a/src/main/java/org/shipkit/auto/version/AutoVersion.java +++ b/src/main/java/org/shipkit/auto/version/AutoVersion.java @@ -16,16 +16,12 @@ class AutoVersion { private final static Logger LOG = Logging.getLogger(AutoVersion.class); - private final ProcessRunner runner; + private final GitValueSourceProviderFactory gitValueSourceProviderFactory; private final File versionFile; - AutoVersion(ProcessRunner runner, File versionFile) { - this.runner = runner; + AutoVersion(GitValueSourceProviderFactory gitValueSourceProviderFactory, File versionFile) { this.versionFile = versionFile; - } - - AutoVersion(File projectDir) { - this(new ProcessRunner(projectDir), new File(projectDir, "version.properties")); + this.gitValueSourceProviderFactory = gitValueSourceProviderFactory; } /** @@ -43,14 +39,16 @@ DeducedVersion deduceVersion(Logger log, String projectVersion) { VersionConfig config = VersionConfig.parseVersionFile(versionFile); try { - Collection versions = new VersionsProvider(runner).getAllVersions(config.getTagPrefix()); + VersionsProvider versionsProvider = new VersionsProvider(gitValueSourceProviderFactory); + Collection versions = versionsProvider.getAllVersions(config.getTagPrefix()); PreviousVersionFinder previousVersionFinder = new PreviousVersionFinder(); if (config.getVersionSpec().isPresent()) { previousVersion = previousVersionFinder.findPreviousVersion(versions, config); } - String nextVersion = new NextVersionPicker(runner, log).pickNextVersion(previousVersion, + NextVersionPicker nextVersionPicker = new NextVersionPicker(gitValueSourceProviderFactory, log); + String nextVersion = nextVersionPicker.pickNextVersion(previousVersion, config, projectVersion); if (!config.getVersionSpec().isPresent()) { diff --git a/src/main/java/org/shipkit/auto/version/AutoVersionPlugin.java b/src/main/java/org/shipkit/auto/version/AutoVersionPlugin.java index 49d48e8..22b9a36 100644 --- a/src/main/java/org/shipkit/auto/version/AutoVersionPlugin.java +++ b/src/main/java/org/shipkit/auto/version/AutoVersionPlugin.java @@ -1,17 +1,35 @@ package org.shipkit.auto.version; +import java.io.File; + +import javax.inject.Inject; + import org.gradle.api.Plugin; import org.gradle.api.Project; +import org.gradle.api.provider.ProviderFactory; /** - * The plugin, ideally with zero business logic, but only the Gradle integration code + * The plugin, ideally with zero business logic, but only the Gradle integration + * code */ public class AutoVersionPlugin implements Plugin { + + private final ProviderFactory providerFactory; + + @Inject + public AutoVersionPlugin(ProviderFactory providerFactory) { + this.providerFactory = providerFactory; + } public void apply(Project project) { - DeducedVersion version = new AutoVersion(project.getProjectDir()).deduceVersion(project.getVersion().toString()); + File versionFile = new File(project.getProjectDir(), "version.properties"); + GitValueSourceProviderFactory gitValueSourceProviderFactory = new GitValueSourceProviderFactory(project.getProjectDir(), providerFactory); + AutoVersion autoVersion = new AutoVersion(gitValueSourceProviderFactory, versionFile); + DeducedVersion version = autoVersion.deduceVersion(project.getVersion().toString()); + project.allprojects(p -> p.setVersion(version.getVersion())); - project.getExtensions().getExtraProperties().set("shipkit-auto-version.previous-version", version.getPreviousVersion()); + project.getExtensions().getExtraProperties().set("shipkit-auto-version.previous-version", + version.getPreviousVersion()); project.getExtensions().getExtraProperties().set("shipkit-auto-version.previous-tag", version.getPreviousTag()); } } diff --git a/src/main/java/org/shipkit/auto/version/GitValueSource.java b/src/main/java/org/shipkit/auto/version/GitValueSource.java new file mode 100644 index 0000000..80c938d --- /dev/null +++ b/src/main/java/org/shipkit/auto/version/GitValueSource.java @@ -0,0 +1,59 @@ +package org.shipkit.auto.version; + +import static java.lang.String.join; +import java.io.ByteArrayOutputStream; +import java.io.File; + +import javax.inject.Inject; + +import org.gradle.api.provider.ValueSource; +import org.gradle.process.ExecOperations; +import org.gradle.process.ExecResult; + +public abstract class GitValueSource implements ValueSource { + + private final ExecOperations execOperations; + + @Inject + public GitValueSource(ExecOperations execOperations) { + this.execOperations = execOperations; + } + + @Override + public String obtain() { + File workDir = getParameters().getWorkingDirectory().get(); + + String[] commands = getParameters().getCommands().get(); + String[] commandsArray = new String[commands.length + 1]; + commandsArray[0] = "git"; + System.arraycopy(commands, 0, commandsArray, 1, commands.length); + + int exitValue; + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + ExecResult result; + + try { + result = execOperations.exec(execSpec -> { + execSpec.commandLine((Object[]) commandsArray); + execSpec.workingDir(workDir); + execSpec.setStandardOutput(outputStream); + execSpec.setErrorOutput(outputStream); + }); + } catch (Exception e) { + String cmdLine = join(" ", commandsArray); + throw new ShipkitAutoVersionException("Problems executing command:\n " + cmdLine, e); + } + + String output = outputStream.toString(); + exitValue = result.getExitValue(); + + if (exitValue != 0) { + String cmdLine = join(" ", commandsArray); + throw new ShipkitAutoVersionException( + "Problems executing command (exit code: " + exitValue + "): " + cmdLine + "\n" + + "Output:\n" + output); + } + + return output; + } +} \ No newline at end of file diff --git a/src/main/java/org/shipkit/auto/version/GitValueSourceParameters.java b/src/main/java/org/shipkit/auto/version/GitValueSourceParameters.java new file mode 100644 index 0000000..8128390 --- /dev/null +++ b/src/main/java/org/shipkit/auto/version/GitValueSourceParameters.java @@ -0,0 +1,10 @@ +package org.shipkit.auto.version; + +import java.io.File; +import org.gradle.api.provider.Property; +import org.gradle.api.provider.ValueSourceParameters; + +public interface GitValueSourceParameters extends ValueSourceParameters { + Property getWorkingDirectory(); + Property getCommands(); +} diff --git a/src/main/java/org/shipkit/auto/version/GitValueSourceProviderFactory.java b/src/main/java/org/shipkit/auto/version/GitValueSourceProviderFactory.java new file mode 100644 index 0000000..8b8d5f1 --- /dev/null +++ b/src/main/java/org/shipkit/auto/version/GitValueSourceProviderFactory.java @@ -0,0 +1,27 @@ +package org.shipkit.auto.version; + +import org.gradle.api.provider.Provider; +import org.gradle.api.provider.ProviderFactory; + +import java.io.File; + +public class GitValueSourceProviderFactory { + + private final File workingDirectory; + private final ProviderFactory providerFactory; + + public GitValueSourceProviderFactory(File workingDirectory, ProviderFactory providerFactory) { + this.workingDirectory = workingDirectory; + this.providerFactory = providerFactory; + } + + Provider getProvider(String[] params) { + return providerFactory.of(GitValueSource.class, spec -> { + spec.parameters(parameters -> { + parameters.getCommands().set(params); + parameters.getWorkingDirectory().set(workingDirectory); + }); + }); + } + +} diff --git a/src/main/java/org/shipkit/auto/version/NextVersionPicker.java b/src/main/java/org/shipkit/auto/version/NextVersionPicker.java index d007e76..298cee2 100644 --- a/src/main/java/org/shipkit/auto/version/NextVersionPicker.java +++ b/src/main/java/org/shipkit/auto/version/NextVersionPicker.java @@ -2,6 +2,7 @@ import org.gradle.api.Project; import org.gradle.api.logging.Logger; +import org.gradle.api.provider.Provider; import java.util.Optional; import java.util.regex.Matcher; @@ -14,11 +15,12 @@ * Picks the next version to use. */ class NextVersionPicker { - private final ProcessRunner runner; + private final Logger log; + private final GitValueSourceProviderFactory gitValueSourceProviderFactory; - NextVersionPicker(ProcessRunner runner, Logger log) { - this.runner = runner; + public NextVersionPicker(GitValueSourceProviderFactory gitValueSourceProviderFactory, Logger log) { + this.gitValueSourceProviderFactory = gitValueSourceProviderFactory; this.log = log; } @@ -36,7 +38,8 @@ String pickNextVersion(Optional previousVersion, VersionConfig co String result; try { - tag = runner.run("git", "describe", "--tags").trim(); + Provider tagDescriptionProvider = gitValueSourceProviderFactory.getProvider(new String[]{"describe", "--tags"}); + tag = tagDescriptionProvider.get().trim(); } catch (Exception e) { result = "0.0.1-SNAPSHOT"; log.info("Process 'git describe --tags' exited with non-zero exit value. Assuming there are no tags. " + @@ -65,15 +68,16 @@ String pickNextVersion(Optional previousVersion, VersionConfig co } if (!config.isWildcard()) { - //if there is no wildcard we will use the version 'as is' + // if there is no wildcard we will use the version 'as is' explainVersion(log, config.getVersionSpec().get(), "uses verbatim version from version file"); return config.getVersionSpec().get(); } if (previousVersion.isPresent() && previousVersion.get().satisfies(config.getVersionSpec().get())) { VersionNumber prev = previousVersion.get(); - String gitOutput = runner.run( - "git", "log", "--pretty=oneline", TagConvention.tagFor(prev.toString(), config.getTagPrefix()) + "..HEAD"); + String[] params = { "log", "--pretty=oneline", TagConvention.tagFor(prev.toString(), config.getTagPrefix()) + "..HEAD" }; + Provider prettyLogProvider = gitValueSourceProviderFactory.getProvider(params); + String gitOutput = prettyLogProvider.get(); int commitCount = new CommitCounter().countCommitDelta(gitOutput); String result = prev.incrementBy(commitCount).toString(); explainVersion(log, result, "deduced version based on previous tag: '" + prev + "'"); diff --git a/src/main/java/org/shipkit/auto/version/VersionsProvider.java b/src/main/java/org/shipkit/auto/version/VersionsProvider.java index 5e96b09..6136218 100644 --- a/src/main/java/org/shipkit/auto/version/VersionsProvider.java +++ b/src/main/java/org/shipkit/auto/version/VersionsProvider.java @@ -4,16 +4,19 @@ import java.util.Set; import java.util.TreeSet; +import org.gradle.api.provider.Provider; + import static org.shipkit.auto.version.VersionConfig.isSupportedVersion; /** * Finds versions based on tags, finds tags by running Git command */ class VersionsProvider { - private final ProcessRunner runner; - VersionsProvider(ProcessRunner runner) { - this.runner = runner; + private final GitValueSourceProviderFactory gitValueSourceProviderFactory; + + public VersionsProvider(GitValueSourceProviderFactory gitValueSourceProviderFactory) { + this.gitValueSourceProviderFactory = gitValueSourceProviderFactory; } /** @@ -22,7 +25,10 @@ class VersionsProvider { * @param tagPrefix tag prefix */ Collection getAllVersions(String tagPrefix) { - String gitOutput = runner.run("git", "tag"); + + Provider gitTagProvider = gitValueSourceProviderFactory.getProvider(new String[]{"tag"}); + + String gitOutput = gitTagProvider.get(); String[] tagOutput = gitOutput.split("\\R"); Set result = new TreeSet<>(); diff --git a/src/test/groovy/org/shipkit/auto/version/AutoVersionTest.groovy b/src/test/groovy/org/shipkit/auto/version/AutoVersionTest.groovy index cd73d9f..e1117e2 100644 --- a/src/test/groovy/org/shipkit/auto/version/AutoVersionTest.groovy +++ b/src/test/groovy/org/shipkit/auto/version/AutoVersionTest.groovy @@ -3,23 +3,26 @@ package org.shipkit.auto.version import org.gradle.api.Project import org.gradle.api.logging.Logger +import org.gradle.api.provider.Provider class AutoVersionTest extends TmpFolderSpecification { - ProcessRunner runner = Mock() File versionFile AutoVersion autoVersion def log = Mock(Logger) + GitValueSourceProviderFactory gitValueSourceProviderFactory = Mock(GitValueSourceProviderFactory) def setup() { versionFile = writeFile("") - autoVersion = new AutoVersion(runner, versionFile) + autoVersion = new AutoVersion(gitValueSourceProviderFactory, versionFile) } def "happy path smoke test"() { //others scenarios are covered in other test classes versionFile << "version=1.1.*" - runner.run("git", "tag") >> "v1.0.1" + Provider provider = Mock() + gitValueSourceProviderFactory.getProvider(["tag"] as String[]) >> provider + provider.get() >> "v1.0.1" when: def v = autoVersion.deduceVersion(log, Project.DEFAULT_VERSION) @@ -32,8 +35,13 @@ class AutoVersionTest extends TmpFolderSpecification { def "happy path smoke test when no version property"() { //others scenarios are covered in other test classes versionFile << "version=" - runner.run("git", "tag") >> "v1.0.4" - runner.run("git", "describe", "--tags") >> "v1.0.5" + Provider tagProvider = Mock() + gitValueSourceProviderFactory.getProvider(["tag"] as String[]) >> tagProvider + tagProvider.get() >> "v1.0.4" + Provider describeTagsProvider = Mock() + gitValueSourceProviderFactory.getProvider(["describe", "--tags"] as String[]) >> describeTagsProvider + describeTagsProvider.get() >> "v1.0.5" + when: def v = autoVersion.deduceVersion(log, Project.DEFAULT_VERSION) @@ -45,7 +53,9 @@ class AutoVersionTest extends TmpFolderSpecification { def "no build failure when deducing versions fails"() { versionFile << "version=1.0.*" - runner.run("git", "tag") >> "v1.0.1" + Provider tagProvider = Mock() + gitValueSourceProviderFactory.getProvider(["tag"] as String[]) >> tagProvider + tagProvider.get() >> "v1.0.1" when: def v = autoVersion.deduceVersion(log, Project.DEFAULT_VERSION) @@ -60,7 +70,9 @@ class AutoVersionTest extends TmpFolderSpecification { } def "no build failure when no version config present and deducing versions fails"() { - runner.run("git", "tag") >> { + Provider tagProvider = Mock() + gitValueSourceProviderFactory.getProvider(["tag"] as String[]) >> tagProvider + tagProvider.get() >> { throw new Exception() } diff --git a/src/test/groovy/org/shipkit/auto/version/NextVersionPickerTest.groovy b/src/test/groovy/org/shipkit/auto/version/NextVersionPickerTest.groovy index a1abc05..7cd13da 100644 --- a/src/test/groovy/org/shipkit/auto/version/NextVersionPickerTest.groovy +++ b/src/test/groovy/org/shipkit/auto/version/NextVersionPickerTest.groovy @@ -1,20 +1,23 @@ package org.shipkit.auto.version -import com.github.zafarkhaja.semver.Version import org.gradle.api.Project import org.gradle.api.logging.Logger +import org.gradle.api.provider.Provider import spock.lang.Specification + class NextVersionPickerTest extends Specification { - ProcessRunner runner = Mock() - NextVersionPicker picker Logger log = Mock() + GitValueSourceProviderFactory gitValueSourceProviderFactory = Mock(GitValueSourceProviderFactory) + Provider provider = Mock() + NextVersionPicker picker def setup() { - picker = new NextVersionPicker(runner, log) + picker = new NextVersionPicker(gitValueSourceProviderFactory, log) } + def "picks version as configured on the Gradle project"() { when: def v = picker.pickNextVersion( @@ -62,7 +65,8 @@ class NextVersionPickerTest extends Specification { } def "picks incremented version"() { - runner.run("git", "log", "--pretty=oneline", "v1.0.0..HEAD") >> """ + gitValueSourceProviderFactory.getProvider(["log", "--pretty=oneline", "v1.0.0..HEAD"] as String[]) >> provider + provider.get() >> """ some commit #1 some commit #2 """ @@ -80,10 +84,11 @@ some commit #2 } def "picks version when no tag prefix"() { - runner.run("git", "log", "--pretty=oneline", "1.0.0..HEAD") >> """ + gitValueSourceProviderFactory.getProvider(["log", "--pretty=oneline", "1.0.0..HEAD"] as String[]) >> provider + provider.get() >> """ some commit """ - + when: def v = picker.pickNextVersion( Optional.of(new VersionNumber("1.0.0")), @@ -95,11 +100,12 @@ some commit } def "picks version when no config file and not checked out on tag"() { - runner.run("git", "describe", "--tags") >> "v1.1.0-1-sha12345" + gitValueSourceProviderFactory.getProvider(["describe", "--tags"] as String[]) >> provider + provider.get() >> "v1.1.0-1-sha12345" when: def v = picker.pickNextVersion(Optional.empty(), - new VersionConfig(null,"v"), + new VersionConfig(null, "v"), Project.DEFAULT_VERSION) then: @@ -107,11 +113,12 @@ some commit } def "picks 4-part version when no config file and not checked out on tag"() { - runner.run("git", "describe", "--tags") >> "v1.2.3.4-1-sha12345" + gitValueSourceProviderFactory.getProvider(["describe", "--tags"] as String[]) >> provider + provider.get() >> "v1.2.3.4-1-sha12345" when: def v = picker.pickNextVersion(Optional.empty(), - new VersionConfig(null,"v"), + new VersionConfig(null, "v"), Project.DEFAULT_VERSION) then: @@ -119,11 +126,12 @@ some commit } def "picks version when no config file and checked out on tag"() { - runner.run("git", "describe", "--tags") >> "v1.1.0" + gitValueSourceProviderFactory.getProvider(["describe", "--tags"] as String[]) >> provider + provider.get() >> "v1.1.0" when: def v = picker.pickNextVersion(Optional.empty(), - new VersionConfig(null,"v"), + new VersionConfig(null, "v"), Project.DEFAULT_VERSION) then: @@ -131,11 +139,12 @@ some commit } def "picks version when no config file and checked out on not valid tag"() { - runner.run("git", "describe", "--tags") >> "ver-1.1.0" + gitValueSourceProviderFactory.getProvider(["describe", "--tags"] as String[]) >> provider + provider.get() >> "ver-1.1.0" when: def v = picker.pickNextVersion(Optional.empty(), - new VersionConfig(null,"v"), + new VersionConfig(null, "v"), Project.DEFAULT_VERSION) then: @@ -143,13 +152,14 @@ some commit } def "picks version when no tags found in project"() { - runner.run("git", "describe", "--tags") >> { + gitValueSourceProviderFactory.getProvider(["describe", "--tags"] as String[]) >> provider + provider.get() >> { throw new ShipkitAutoVersionException("Problems executing command") } when: def v = picker.pickNextVersion(Optional.empty(), - new VersionConfig(null,"v"), + new VersionConfig(null, "v"), Project.DEFAULT_VERSION) then: diff --git a/src/test/groovy/org/shipkit/auto/version/VersionsProviderTest.groovy b/src/test/groovy/org/shipkit/auto/version/VersionsProviderTest.groovy index b2a374a..d9dd110 100644 --- a/src/test/groovy/org/shipkit/auto/version/VersionsProviderTest.groovy +++ b/src/test/groovy/org/shipkit/auto/version/VersionsProviderTest.groovy @@ -1,26 +1,29 @@ package org.shipkit.auto.version - +import org.gradle.api.provider.Provider import spock.lang.Specification class VersionsProviderTest extends Specification { - ProcessRunner runner = Mock() - VersionsProvider provider + GitValueSourceProviderFactory gitValueSourceProviderFactory = Mock(GitValueSourceProviderFactory) + Provider provider = Mock() + VersionsProvider versionsProvider def setup() { - provider = new VersionsProvider(runner) + versionsProvider = new VersionsProvider(gitValueSourceProviderFactory) } def "returns empty collection when no tags"() { - runner.run("git", "tag") >> "" + gitValueSourceProviderFactory.getProvider(["tag"]) >> provider + provider.get() >> "" expect: - provider.getAllVersions("v").isEmpty() + versionsProvider.getAllVersions("v").isEmpty() } def "gets all versions from tags"() { - runner.run("git", "tag") >> """ + gitValueSourceProviderFactory.getProvider(["tag"]) >> provider + provider.get() >> """ v1.0.1 v1.0.2 v2.0.0 @@ -34,11 +37,12 @@ class VersionsProviderTest extends Specification { """ expect: - provider.getAllVersions("v").toString() == "[1.0.0.0, 1.0.1, 1.0.2, 2.0.0, 22.333.4444]" + versionsProvider.getAllVersions("v").toString() == "[1.0.0.0, 1.0.1, 1.0.2, 2.0.0, 22.333.4444]" } def "gets all versions when no tag prefix"() { - runner.run("git", "tag") >> """ + gitValueSourceProviderFactory.getProvider(["tag"]) >> provider + provider.get() >> """ 1.0.1 1.0.2 foo @@ -46,6 +50,6 @@ class VersionsProviderTest extends Specification { """ expect: - provider.getAllVersions("").toString() == "[1.0.1, 1.0.2]" + versionsProvider.getAllVersions("").toString() == "[1.0.1, 1.0.2]" } } diff --git a/src/main/java/org/shipkit/auto/version/ProcessRunner.java b/src/test/java/org/shipkit/auto/version/ProcessRunner.java similarity index 98% rename from src/main/java/org/shipkit/auto/version/ProcessRunner.java rename to src/test/java/org/shipkit/auto/version/ProcessRunner.java index 844e6c6..8f3c821 100644 --- a/src/main/java/org/shipkit/auto/version/ProcessRunner.java +++ b/src/test/java/org/shipkit/auto/version/ProcessRunner.java @@ -4,7 +4,6 @@ import java.io.File; import java.io.IOException; import java.io.InputStreamReader; -import java.util.Arrays; import static java.lang.String.join; import static java.util.Arrays.asList; diff --git a/version.properties b/version.properties index c46cdc3..ad90f37 100644 --- a/version.properties +++ b/version.properties @@ -1,3 +1,3 @@ # Version of produced binaries. # The '*' part is automatically resolved by shipkit-auto-version Gradle plugin (https://github.com/shipkit/shipkit-auto-version). -version=2.0.* +version=2.1.*