@@ -3,6 +3,8 @@ 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
68import scala .util .Properties
79
810class SipScalaTests extends ScalaCliSuite with SbtTestHelper with MillTestHelper {
@@ -833,62 +835,98 @@ class SipScalaTests extends ScalaCliSuite with SbtTestHelper with MillTestHelper
833835 }
834836 }
835837
836- if (! Properties .isWin) // FIXME: run this test on Windows
837- test(" coursier scala installation works in --offline mode" ) {
838- TestInputs .empty.fromRoot { root =>
839- val localCache = root / " local-cache"
840- val localBin = root / " local-bin"
841- val sv = " 3.5.0-RC4"
842- os.proc(
843- TestUtil .cs,
844- " install" ,
845- " --cache" ,
846- localCache,
847- " --install-dir" ,
848- localBin,
849- s " scala: $sv"
850- ).call(cwd = root)
851- val scalaBinary : os.Path = localBin / " scala"
852- val fileBytes = os.read.bytes(scalaBinary)
853- val shebang = new String (fileBytes.takeWhile(_ != '\n ' ), " UTF-8" )
854- val binaryData = fileBytes.drop(shebang.length + 1 )
855- val execLine = new String (binaryData.takeWhile(_ != '\n ' ), " UTF-8" )
856- val scriptPathRegex = """ exec "([^"]+/bin/scala).*"""" .r
857- val scalaScript = execLine match { case scriptPathRegex(extractedPath) => extractedPath }
858- val scalaScriptPath = os.Path (scalaScript)
859- val lineToChange = " eval \" ${SCALA_CLI_CMD_BASH[@]}\" \\ "
860- // FIXME: the way the scala script calls the launcher currently ignores the --debug flag
861- val newContent = os.read(scalaScriptPath).replace(
862- lineToChange,
863- s """ SCALA_CLI_CMD_BASH=(\" \\ \" ${TestUtil .cliPath}\\ \"\")
864- | $lineToChange""" .stripMargin
865- )
866- os.write.over(scalaScriptPath, newContent)
867- val r =
868- os.proc(
869- scalaScript,
870- " --offline" ,
871- " --power" ,
872- " --with-compiler" ,
873- " -e" ,
874- " println(dotty.tools.dotc.config.Properties.versionNumberString)"
875- ).call(
876- cwd = root,
877- env = Map (" COURSIER_CACHE" -> localCache.toString),
878- check = false // need to clean up even on failure
838+ test(" coursier scala installation works in --offline mode" ) {
839+ 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
879873 )
880- // clean up cs local binaries
881- val csPrebuiltBinaryDir =
882- os.Path (scalaScript.substring(0 , scalaScript.indexOf(sv) + sv.length))
883- try os.remove.all(csPrebuiltBinaryDir)
884- catch {
885- case ex : java.nio.file.FileSystemException =>
886- println(s " Failed to remove $csPrebuiltBinaryDir: $ex" )
874+ expect(newContent != oldContent)
875+ os.write.over(batchScriptPath, newContent)
876+ batchWrapperScript
887877 }
888- expect(r.exitCode == 0 )
889- expect(r.out.trim() == sv)
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
893+ )
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" )
890925 }
926+ expect(r.exitCode == 0 )
927+ expect(r.out.trim() == sv)
891928 }
929+ }
892930
893931 // this check is just to ensure this isn't being run for LTS RC jobs
894932 // should be adjusted when a new LTS line is released
0 commit comments