Skip to content

Commit

Permalink
Merge pull request #1760 from Gedochao/less-shaky-version-tests
Browse files Browse the repository at this point in the history
Don't fail in case of connection errors in the version sub-command
  • Loading branch information
Gedochao authored Jan 11, 2023
2 parents 518d63d + ba23fc3 commit a809da5
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 29 deletions.
41 changes: 23 additions & 18 deletions modules/cli/src/main/scala/scala/cli/commands/update/Update.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.github.plokhotnyuk.jsoniter_scala.macros.*
import coursier.core

import scala.build.Logger
import scala.build.errors.CheckScalaCliVersionError
import scala.build.internal.Constants.{ghName, ghOrg, version as scalaCliVersion}
import scala.cli.CurrentParams
import scala.cli.commands.{CommandUtils, ScalaCommand}
Expand All @@ -31,29 +32,28 @@ object Update extends ScalaCommand[UpdateOptions] {

private lazy val releaseListCodec: JsonValueCodec[List[Release]] = JsonCodecMaker.make

def newestScalaCliVersion(tokenOpt: Option[Secret[String]]): String = {

def newestScalaCliVersion(tokenOpt: Option[Secret[String]])
: Either[CheckScalaCliVersionError, String] = {
// FIXME Do we need paging here?
val url = s"https://api.github.com/repos/$ghOrg/$ghName/releases"
val headers =
Seq("Accept" -> "application/vnd.github.v3+json") ++
tokenOpt.toSeq.map(tk => "Authorization" -> s"token ${tk.value}")
val resp = ProcUtil.download(url, headers: _*)

val releases =
try readFromArray(resp)(releaseListCodec)
catch {
case e: JsonReaderException =>
throw new Exception(s"Error reading $url", e)
}

releases
.filter(_.actualRelease)
.maxByOption(_.version)
.map(_.version.repr)
.getOrElse {
sys.error(s"No $fullRunnerName versions found in $url")
}
try {
val resp = ProcUtil.download(url, headers: _*)
readFromArray(resp)(releaseListCodec).filter(_.actualRelease)
.maxByOption(_.version)
.map(_.version.repr)
.toRight(CheckScalaCliVersionError(s"No $fullRunnerName versions found in $url"))
}
catch {
case e: JsonReaderException => Left(CheckScalaCliVersionError(s"Error reading $url", e))
case e: Throwable => Left(CheckScalaCliVersionError(
s"Failed to check for the newest Scala CLI version upstream: ${e.getMessage}",
e
))
}
}

def installDirPath(options: UpdateOptions): os.Path =
Expand Down Expand Up @@ -107,7 +107,12 @@ object Update extends ScalaCommand[UpdateOptions] {

private def update(options: UpdateOptions, currentVersion: String, logger: Logger): Unit = {

val newestScalaCliVersion0 = newestScalaCliVersion(options.ghToken.map(_.get()))
val newestScalaCliVersion0 = newestScalaCliVersion(options.ghToken.map(_.get())) match {
case Left(e) =>
logger.log(e.message)
sys.error(e.message)
case Right(v) => v
}
val isOutdated = CommandUtils.isOutOfDateVersion(newestScalaCliVersion0, currentVersion)

if (!options.isInternalRun)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,28 @@ object Version extends ScalaCommand[VersionOptions] {
)

override def runCommand(options: VersionOptions, args: RemainingArgs, logger: Logger): Unit = {
lazy val newestScalaCliVersion = Update.newestScalaCliVersion(options.ghToken.map(_.get()))
lazy val isVersionOutOfDate: Boolean =
CommandUtils.isOutOfDateVersion(newestScalaCliVersion, Constants.version)
lazy val maybeNewerScalaCliVersion: Option[String] =
Update.newestScalaCliVersion(options.ghToken.map(_.get())) match {
case Left(e) =>
logger.debug(e.message)
None
case Right(newestScalaCliVersion) =>
if CommandUtils.isOutOfDateVersion(newestScalaCliVersion, Constants.version) then
Some(newestScalaCliVersion)
else None
}
if options.cliVersion then println(Constants.version)
else if options.scalaVersion then println(Constants.defaultScalaVersion)
else if !options.offline && isVersionOutOfDate then {
else {
println(versionInfo)
logger.message(
s"""Your $fullRunnerName version is outdated. The newest version is $newestScalaCliVersion
|It is recommended that you update $fullRunnerName through the same tool or method you used for its initial installation for avoiding the creation of outdated duplicates.""".stripMargin
)
if !options.offline then
maybeNewerScalaCliVersion.foreach { v =>
logger.message(
s"""Your $fullRunnerName version is outdated. The newest version is $v
|It is recommended that you update $fullRunnerName through the same tool or method you used for its initial installation for avoiding the creation of outdated duplicates.""".stripMargin
)
}
}
else println(versionInfo)
}

private def versionInfo: String =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package scala.build.errors

class CheckScalaCliVersionError(val message: String, val cause: Throwable)
extends Exception(message, cause)

object CheckScalaCliVersionError {
def apply(message: String, cause: Throwable = null): CheckScalaCliVersionError =
new CheckScalaCliVersionError(message, cause)
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class SipScalaTests extends ScalaCliSuite {
def testVersionCommand(binaryName: String): Unit =
TestInputs.empty.fromRoot { root =>
val binary = binaryName.prepareBinary(root)
for { versionOption <- Seq("version", "-version", "--version") } {
for { versionOption <- VersionTests.variants } {
val version = os.proc(binary, versionOption).call(check = false)
assert(
version.exitCode == 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package scala.cli.integration

import com.eed3si9n.expecty.Expecty.expect

import scala.cli.integration.VersionTests.variants

class VersionTests extends ScalaCliSuite {
test("version command") {
// tests if the format is correct instead of comparing to a version passed via Constants
// in order to catch errors in Mill configuration, too
val versionRegex = ".*\\d+[.]\\d+[.]\\d+.*".r
for (versionOption <- Seq("version", "--version")) {
for (versionOption <- variants) {
val version = os.proc(TestUtil.cli, versionOption).call(check = false)
assert(
versionRegex.findFirstMatchIn(version.out.text()).isDefined,
Expand All @@ -18,3 +20,7 @@ class VersionTests extends ScalaCliSuite {
}

}

object VersionTests {
val variants = Seq("version", "-version", "--version")
}

0 comments on commit a809da5

Please sign in to comment.