Skip to content

Commit

Permalink
Fix using directives not working with the shebang line in .scala files
Browse files Browse the repository at this point in the history
  • Loading branch information
Gedochao committed Dec 5, 2022
1 parent 55210e9 commit 13a214e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,9 @@ case object ScalaPreprocessor extends Preprocessor {
maybeRecoverOnError: BuildException => Option[BuildException],
allowRestrictedFeatures: Boolean
): Either[BuildException, Option[ProcessingOutput]] = either {
val (contentWithNoShebang, _) = SheBang.ignoreSheBangLines(content)
val extractedDirectives = value(ExtractedDirectives.from(
content.toCharArray,
contentWithNoShebang.toCharArray,
path,
logger,
UsingDirectiveKind.values(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package scala.build.tests

import com.eed3si9n.expecty.Expecty.expect

import scala.build.input.SourceScalaFile
import scala.build.preprocessing.{PreprocessedSource, ScalaPreprocessor}

class ScalaPreprocessorTests extends munit.FunSuite {

test("should respect using directives in a .scala file with the shebang line") {
TestInputs(os.rel / "Main.scala" ->
"""#!/usr/bin/env -S scala-cli shebang
|//> using lib "com.lihaoyi::os-lib::0.8.1"
|
|object Main {
| def main(args: Array[String]): Unit = {
| println(os.pwd)
| }
|}""".stripMargin).fromRoot { root =>
val scalaFile = SourceScalaFile(root, os.sub / "Main.scala")
val Some(Right(result)) = ScalaPreprocessor.preprocess(
scalaFile,
logger = TestLogger(),
allowRestrictedFeatures = false
)
expect(result.nonEmpty)
val Some(directivesPositions) = result.head.directivesPositions
expect(directivesPositions.specialCommentDirectives.startPos == 0 -> 0)
expect(directivesPositions.specialCommentDirectives.endPos == 3 -> 0)
}
}

test("should respect using directives in a .sc file with the shebang line") {
TestInputs(os.rel / "sample.sc" ->
"""#!/usr/bin/env -S scala-cli shebang
|//> using lib "com.lihaoyi::os-lib::0.8.1"
|println(os.pwd)
|""".stripMargin).fromRoot { root =>
val scalaFile = SourceScalaFile(root, os.sub / "sample.sc")
val Some(Right(result)) = ScalaPreprocessor.preprocess(
scalaFile,
logger = TestLogger(),
allowRestrictedFeatures = false
)
expect(result.nonEmpty)
val Some(directivesPositions) = result.head.directivesPositions
expect(directivesPositions.specialCommentDirectives.startPos == 0 -> 0)
expect(directivesPositions.specialCommentDirectives.endPos == 2 -> 0)
}
}
}

0 comments on commit 13a214e

Please sign in to comment.