@@ -3,11 +3,12 @@ package scala.cli.integration
33import com .eed3si9n .expecty .Expecty .expect
44import os .CommandResult
55
6- import java .nio .charset .Charset
7- import scala .jdk .CollectionConverters .IteratorHasAsScala
86import scala .util .Properties
97
10- class SipScalaTests extends ScalaCliSuite with SbtTestHelper with MillTestHelper {
8+ class SipScalaTests extends ScalaCliSuite
9+ with SbtTestHelper
10+ with MillTestHelper
11+ with CoursierScalaInstallationTestHelper {
1112 implicit class StringEnrichment (s : String ) {
1213 def containsExperimentalWarningOf (featureNameAndType : String ): Boolean =
1314 s.contains(s " The $featureNameAndType is experimental " ) ||
@@ -837,94 +838,31 @@ class SipScalaTests extends ScalaCliSuite with SbtTestHelper with MillTestHelper
837838
838839 test(" coursier scala installation works in --offline mode" ) {
839840 TestInputs .empty.fromRoot { root =>
840- val localCache = root / " local-cache"
841- val localBin = root / " local-bin"
842- val sv = Constants .scala3NextAnnounced
843- os.proc(
844- TestUtil .cs,
845- " install" ,
846- " --cache" ,
847- localCache,
848- " --install-dir" ,
849- localBin,
850- s " scala: $sv"
851- ).call(cwd = root)
852- val launchScalaPath : os.Path =
853- if (Properties .isWin) {
854- val batchWrapperScript : os.Path = localBin / " scala.bat"
855- val charset = Charset .defaultCharset().toString
856- val batchWrapperContent = new String (os.read.bytes(batchWrapperScript), charset)
857- val setCommandLine = batchWrapperContent
858- .lines()
859- .iterator()
860- .asScala
861- .toList
862- .find(_.startsWith(" SET CMDLINE=" ))
863- .getOrElse(" " )
864- val scriptPathRegex = """ SET CMDLINE="(.*\\ bin\\ scala\.bat)" %CMD_LINE_ARGS%""" .r
865- val batchScript =
866- setCommandLine match { case scriptPathRegex(extractedPath) => extractedPath }
867- val batchScriptPath = os.Path (batchScript)
868- val oldContent = os.read(batchScriptPath)
869- val newContent = oldContent.replace(
870- " call %SCALA_CLI_CMD_WIN%" ,
871- s """ set "SCALA_CLI_CMD_WIN= ${TestUtil .cliPath}"
872- |call %SCALA_CLI_CMD_WIN% """ .stripMargin
873- )
874- expect(newContent != oldContent)
875- os.write.over(batchScriptPath, newContent)
876- batchWrapperScript
877- }
878- else {
879- val scalaBinary : os.Path = localBin / " scala"
880- val fileBytes = os.read.bytes(scalaBinary)
881- val shebang = new String (fileBytes.takeWhile(_ != '\n ' ), " UTF-8" )
882- val binaryData = fileBytes.drop(shebang.length + 1 )
883- val execLine = new String (binaryData.takeWhile(_ != '\n ' ), " UTF-8" )
884- val scriptPathRegex = """ exec "([^"]+/bin/scala).*"""" .r
885- val scalaScript = execLine match { case scriptPathRegex(extractedPath) => extractedPath }
886- val scalaScriptPath = os.Path (scalaScript)
887- val lineToChange = " eval \" ${SCALA_CLI_CMD_BASH[@]}\" \\ "
888- // FIXME: the way the scala script calls the launcher currently ignores the --debug flag
889- val newContent = os.read(scalaScriptPath).replace(
890- lineToChange,
891- s """ SCALA_CLI_CMD_BASH=(\" \\ \" ${TestUtil .cliPath}\\ \"\")
892- | $lineToChange""" .stripMargin
841+ val localCache = root / " local-cache"
842+ val localBin = root / " local-bin"
843+ val scalaVersion = Constants .scala3NextAnnounced
844+ withScalaRunnerWrapper(
845+ root = root,
846+ localCache = localCache,
847+ localBin = localBin,
848+ scalaVersion = scalaVersion
849+ ) { launchScalaPath =>
850+ val r =
851+ os.proc(
852+ launchScalaPath,
853+ " --offline" ,
854+ " --power" ,
855+ " --with-compiler" ,
856+ " -e" ,
857+ " println(dotty.tools.dotc.config.Properties.versionNumberString)"
858+ ).call(
859+ cwd = root,
860+ env = Map (" COURSIER_CACHE" -> localCache.toString),
861+ check = false // need to clean up even on failure
893862 )
894- os.write.over(scalaScriptPath, newContent)
895- scalaBinary
896- }
897- val wrapperVersion = os.proc(launchScalaPath, " version" , " --cli-version" )
898- .call(cwd = root).out.trim()
899- val cliVersion = os.proc(TestUtil .cli, " version" , " --cli-version" )
900- .call(cwd = root).out.trim()
901- expect(wrapperVersion == cliVersion)
902- val r =
903- os.proc(
904- launchScalaPath,
905- " --offline" ,
906- " --power" ,
907- " --with-compiler" ,
908- " -e" ,
909- " println(dotty.tools.dotc.config.Properties.versionNumberString)"
910- ).call(
911- cwd = root,
912- env = Map (" COURSIER_CACHE" -> localCache.toString),
913- check = false // need to clean up even on failure
914- )
915- // clean up cs local binaries
916- val csPrebuiltBinaryDir =
917- os.Path (launchScalaPath.toString().substring(
918- 0 ,
919- launchScalaPath.toString().indexOf(sv) + sv.length
920- ))
921- try os.remove.all(csPrebuiltBinaryDir)
922- catch {
923- case ex : java.nio.file.FileSystemException =>
924- println(s " Failed to remove $csPrebuiltBinaryDir: $ex" )
863+ expect(r.exitCode == 0 )
864+ expect(r.out.trim() == scalaVersion)
925865 }
926- expect(r.exitCode == 0 )
927- expect(r.out.trim() == sv)
928866 }
929867 }
930868
0 commit comments