From 6ca1cfe9a3e293b057ac9b84707f1819f1e7bd0d Mon Sep 17 00:00:00 2001 From: Maciej Gajek Date: Tue, 3 Oct 2023 18:00:31 +0200 Subject: [PATCH] Add support for offline for COURSIER_MODE env var and coursier.mode java prop --- .../cli/commands/shared/CoursierOptions.scala | 6 +- .../cli/commands/shared/SharedOptions.scala | 6 +- .../cli/integration/RunTestDefinitions.scala | 80 ++++++++++--------- 3 files changed, 52 insertions(+), 40 deletions(-) diff --git a/modules/cli/src/main/scala/scala/cli/commands/shared/CoursierOptions.scala b/modules/cli/src/main/scala/scala/cli/commands/shared/CoursierOptions.scala index 84216d5307..d84c400f62 100644 --- a/modules/cli/src/main/scala/scala/cli/commands/shared/CoursierOptions.scala +++ b/modules/cli/src/main/scala/scala/cli/commands/shared/CoursierOptions.scala @@ -47,11 +47,15 @@ final case class CoursierOptions( baseCache = baseCache.withTtl(ttl0) for (loc <- cache.filter(_.trim.nonEmpty)) baseCache = baseCache.withLocation(loc) - for (isOffline <- offline if isOffline) + for (isOffline <- getOffline() if isOffline) baseCache = baseCache.withCachePolicies(Seq(CachePolicy.LocalOnly)) baseCache } + + def getOffline(): Option[Boolean] = offline + .orElse(Option(System.getenv("COURSIER_MODE")).map(_ == "offline")) + .orElse(Option(System.getProperty("coursier.mode")).map(_ == "offline")) } object CoursierOptions { diff --git a/modules/cli/src/main/scala/scala/cli/commands/shared/SharedOptions.scala b/modules/cli/src/main/scala/scala/cli/commands/shared/SharedOptions.scala index f9abff76d8..9ca85a7504 100644 --- a/modules/cli/src/main/scala/scala/cli/commands/shared/SharedOptions.scala +++ b/modules/cli/src/main/scala/scala/cli/commands/shared/SharedOptions.scala @@ -403,7 +403,7 @@ final case class SharedOptions( strictBloopJsonCheck = strictBloopJsonCheck, interactive = Some(() => interactive), exclude = exclude.map(Positioned.commandLine), - offline = coursier.offline + offline = coursier.getOffline() ), notForBloopOptions = bo.PostBuildOptions( scalaJsLinkerOptions = linkerOptions(js), @@ -552,9 +552,9 @@ final case class SharedOptions( config, threads.bloop, strictBloopJsonCheckOrDefault, - coursier.offline.getOrElse(false) + coursier.getOffline().getOrElse(false) )) - case Left(ex) if coursier.offline.contains(true) => + case Left(ex) if coursier.getOffline().contains(true) => logger.diagnostic(WarningMessages.offlineModeBloopJvmNotFound, Severity.Warning) Right(SimpleScalaCompilerMaker("java", Nil)) case Left(ex) => Left(ex) diff --git a/modules/integration/src/test/scala/scala/cli/integration/RunTestDefinitions.scala b/modules/integration/src/test/scala/scala/cli/integration/RunTestDefinitions.scala index 9a38699be5..e658009f96 100644 --- a/modules/integration/src/test/scala/scala/cli/integration/RunTestDefinitions.scala +++ b/modules/integration/src/test/scala/scala/cli/integration/RunTestDefinitions.scala @@ -1890,7 +1890,7 @@ abstract class RunTestDefinitions(val scalaVersionOpt: Option[String]) val depScalaVersion = actualScalaVersion match { case sv if sv.startsWith("2.12") => "2.12" case sv if sv.startsWith("2.13") => "2.13" - case _ => "3" + case _ => "3" } val dep = s"com.lihaoyi:os-lib_$depScalaVersion:0.9.1" @@ -1919,14 +1919,14 @@ abstract class RunTestDefinitions(val scalaVersionOpt: Option[String]) val emptyCacheWalkSize = os.walk(cachePath).size val noArtifactsRes = os.proc( - TestUtil.cli, - "--power", - "NoDeps.scala", - extraOptions, - "--offline", - "--cache", - cachePath.toString - ) + TestUtil.cli, + "--power", + "NoDeps.scala", + extraOptions, + "--offline", + "--cache", + cachePath.toString + ) .call(cwd = root, check = false, mergeErrIntoOut = true) expect(noArtifactsRes.exitCode == 1) @@ -1946,16 +1946,16 @@ abstract class RunTestDefinitions(val scalaVersionOpt: Option[String]) val scalaJvmCacheWalkSize = os.walk(cachePath).size val scalaAndJvmRes = os.proc( - TestUtil.cli, - "--power", - "NoDeps.scala", - extraOptions, - "--offline", - "--cache", - cachePath.toString, - "-v", - "-v" - ) + TestUtil.cli, + "--power", + "NoDeps.scala", + extraOptions, + "--offline", + "--cache", + cachePath.toString, + "-v", + "-v" + ) .call(cwd = root, mergeErrIntoOut = true) expect(scalaAndJvmRes.exitCode == 0) expect(scalaAndJvmRes.out.trim().contains( @@ -1967,21 +1967,29 @@ abstract class RunTestDefinitions(val scalaVersionOpt: Option[String]) expect(scalaJvmCacheWalkSize == os.walk(cachePath).size) // Missing dependencies - val missingDepsRes = os.proc( + for { + (cliOption, extraEnvMode) <- Seq( + "--offline" -> Map.empty[String, String], + "-Dcoursier.mode=offline" -> Map.empty[String, String], + "" -> Map("COURSIER_MODE" -> "offline") + ) + } { + val missingDepsRes = os.proc( TestUtil.cli, "--power", + cliOption, "WithDeps.scala", extraOptions, - "--offline", "--cache", cachePath.toString ) - .call(cwd = root, check = false, mergeErrIntoOut = true) - expect(missingDepsRes.exitCode == 1) - expect(missingDepsRes.out.trim().contains("Error downloading com.lihaoyi:os-lib")) + .call(cwd = root, check = false, mergeErrIntoOut = true, env = extraEnvMode) + expect(missingDepsRes.exitCode == 1) + expect(missingDepsRes.out.trim().contains("Error downloading com.lihaoyi:os-lib")) - // Cache unchanged - expect(scalaJvmCacheWalkSize == os.walk(cachePath).size) + // Cache unchanged + expect(scalaJvmCacheWalkSize == os.walk(cachePath).size) + } // Download dependencies os.proc(TestUtil.cs, "fetch", dep) @@ -1990,16 +1998,16 @@ abstract class RunTestDefinitions(val scalaVersionOpt: Option[String]) val withDependencyCacheWalkSize = os.walk(cachePath).size val depsRes = os.proc( - TestUtil.cli, - "--power", - "WithDeps.scala", - extraOptions, - "--offline", - "--cache", - cachePath.toString, - "-v", - "-v" - ) + TestUtil.cli, + "--power", + "WithDeps.scala", + extraOptions, + "--offline", + "--cache", + cachePath.toString, + "-v", + "-v" + ) .call(cwd = root, mergeErrIntoOut = true) expect(depsRes.exitCode == 0) expect(