Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drive-relative paths in Windows SHELL environments #2516

Merged
merged 11 commits into from
Nov 24, 2023
Merged
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ out/
.scala-build
dest/
target/

# ignore vim backup files
*.sw[op]
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ object InstallCompletions extends ScalaCommand[InstallCompletionsOptions] {
def getFormat(format: Option[String]): Option[String] =
format.map(_.trim).filter(_.nonEmpty)
.orElse {
Option(System.getenv("SHELL")).map(_.split(File.separator).last).map {
Option(System.getenv("SHELL")).map(_.split("[\\/]+").last).map {
case "bash" => Bash.id
case "zsh" => Zsh.id
case other => other
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package scala.cli.integration
import com.eed3si9n.expecty.Expecty.expect

import java.io.File
import java.util.regex.Pattern

import scala.cli.integration.util.BloopUtil

Expand Down Expand Up @@ -585,7 +584,7 @@ abstract class CompileTestDefinitions(val scalaVersionOpt: Option[String])
"."
).call(cwd = root)
val classPath = res.out.trim().split(File.pathSeparator)
val classPathFileNames = classPath.map(_.split(Pattern.quote(File.separator)).last)
val classPathFileNames = classPath.map(_.split("[\\\\/]+").last)
expect(classPathFileNames.exists(_.startsWith("spark-core_")))
// usually a duplicate is there if we don't call .distrinct when necessary here or there
expect(classPathFileNames.exists(_.startsWith("snappy-java")))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class ConfigTests extends ScalaCliSuite {
TestUtil.putCsInPathViaEnv(root / "bin")

val res = os.proc(TestUtil.cli, "--power", "config", "httpProxy.address")
.call(cwd = root, env = extraEnv)
.call(cwd = root, env = extraEnv, mergeErrIntoOut = false)
philwalk marked this conversation as resolved.
Show resolved Hide resolved
val value = res.out.trim()
expect(value == proxyAddr)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import java.io.{File, InputStream}
import java.nio.charset.StandardCharsets
import java.nio.file.Files
import java.util
import java.util.regex.Pattern
import java.util.zip.ZipFile

import scala.cli.integration.TestUtil.removeAnsiColors
Expand Down Expand Up @@ -731,7 +730,7 @@ abstract class PackageTestDefinitions(val scalaVersionOpt: Option[String])
if (actualScalaVersion.startsWith("2.")) actualScalaVersion
else {
val scalaLibJarName = scalaLibCp.split(File.pathSeparator)
.map(_.split(Pattern.quote(File.separator)).last).find(_.startsWith("scala-library-"))
.map(_.split("[\\\\/]+").last).find(_.startsWith("scala-library-"))
.getOrElse {
sys.error(s"scala-library not found in provided class path $scalaLibCp")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package scala.cli.integration

import com.eed3si9n.expecty.Expecty.expect

import java.io.File

import scala.cli.integration.TestUtil.normalizeConsoleOutput
import scala.util.Properties

Expand Down Expand Up @@ -549,4 +551,65 @@ trait RunScriptTestDefinitions { _: RunTestDefinitions =>
expect(p.out.trim() == "42")
}
}

test("verify os.Path attributes") {
// requires os-lib 0.9.2 or later to succeed in Windows
val dr = os.Path.driveRoot
val testStr = "/omg"
val p = os.Path(testStr) // <<< must not throw Exception
val absPath = p.toString.replace('\\', '/')
val relPath = if (dr.nonEmpty) absPath.drop(2) else absPath
val synPath = s"${os.Path.driveRoot}$relPath"
printf("absPath[%s]\n", absPath)
printf("synPath[%s]\n", synPath)
printf("relPath[%s]\n", relPath)
expect(absPath == synPath)
expect(relPath == testStr)
expect(absPath endsWith testStr)
}
philwalk marked this conversation as resolved.
Show resolved Hide resolved

test("verify drive-relative JAVA_HOME works") {
val java8Home =
os.Path(os.proc(TestUtil.cs, "java-home", "--jvm", "zulu:8").call().out.trim(), os.pwd)

val dr = os.Path.driveRoot
printf("driveRoot: %s\n", dr)
philwalk marked this conversation as resolved.
Show resolved Hide resolved

// forward slash is legal in `Windows`
val javaHomeNoDriveRoot = java8Home.toString.drop(dr.length()).replace('\\', '/')
printf("java8HomeNoDriveRoot: %s\n", javaHomeNoDriveRoot)
philwalk marked this conversation as resolved.
Show resolved Hide resolved
expect(javaHomeNoDriveRoot.startsWith("/"))

val sysPath: String = System.getenv("PATH")
val newPath: String = s"$javaHomeNoDriveRoot/bin" + File.pathSeparator + sysPath

val extraEnv = Map(
"JAVA_HOME" -> javaHomeNoDriveRoot,
"PATH" -> newPath
)

val inputs = TestInputs(
os.rel / "script-with-shebang" ->
s"""|#!/usr/bin/env -S ${TestUtil.cli.mkString(" ")} shebang -S 2.13
|//> using scala "$actualScalaVersion"
|println(args.toList)""".stripMargin
)
inputs.fromRoot { root =>
val proc = if (!Properties.isWin) {
os.perms.set(root / "script-with-shebang", os.PermSet.fromString("rwx------"))
os.proc("./script-with-shebang", "1", "2", "3", "-v")
}
else
os.proc(TestUtil.cli, "shebang", "script-with-shebang", "1", "2", "3", "-v")

val output = proc.call(cwd = root, env = extraEnv).out.trim()

val expectedOutput = "List(1, 2, 3, -v)"
if (output != expectedOutput)
printf("JAVA_HOME test: output is [%s]\n", output)
philwalk marked this conversation as resolved.
Show resolved Hide resolved

expect(output == expectedOutput)
}
}

}
2 changes: 1 addition & 1 deletion project/deps.sc
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ object Deps {
def munit = ivy"org.scalameta::munit:0.7.29"
def nativeTestRunner = ivy"org.scala-native::test-runner:${Versions.scalaNative}"
def nativeTools = ivy"org.scala-native::tools:${Versions.scalaNative}"
def osLib = ivy"com.lihaoyi::os-lib:0.9.1"
def osLib = ivy"com.lihaoyi::os-lib:0.9.2"
philwalk marked this conversation as resolved.
Show resolved Hide resolved
def pprint = ivy"com.lihaoyi::pprint:0.8.1"
def pythonInterface = ivy"io.github.alexarchambault.python:interface:0.1.0"
def pythonNativeLibs = ivy"ai.kien::python-native-libs:0.2.4"
Expand Down
Loading