Skip to content

Commit

Permalink
Report error if scala script not exists (#538)
Browse files Browse the repository at this point in the history
  • Loading branch information
lwronski committed Jan 12, 2022
1 parent da7ce53 commit 7d3144d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package scala.build.preprocessing

import java.nio.charset.StandardCharsets

import scala.build.errors.{BuildException, FileNotFoundException}

object PreprocessingUtil {

private def defaultCharSet = StandardCharsets.UTF_8

def maybeRead(f: os.Path): Either[BuildException, String] =
if (os.isFile(f)) Right(os.read(f, defaultCharSet))
else Left(new FileNotFoundException(f))
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@ import java.nio.charset.StandardCharsets

import scala.build.EitherCps.{either, value}
import scala.build.Ops._
import scala.build.errors.{
BuildException,
CompositeBuildException,
DependencyFormatError,
FileNotFoundException
}
import scala.build.errors.{BuildException, CompositeBuildException, DependencyFormatError}
import scala.build.internal.{AmmUtil, Util}
import scala.build.options.{BuildOptions, BuildRequirements, ClassPathOptions}
import scala.build.preprocessing.directives._
Expand Down Expand Up @@ -72,12 +67,6 @@ case object ScalaPreprocessor extends Preprocessor {
RequireScopeDirectiveHandler
)

private def defaultCharSet = StandardCharsets.UTF_8

private def maybeRead(f: os.Path): Either[BuildException, String] =
if (os.isFile(f)) Right(os.read(f, defaultCharSet))
else Left(new FileNotFoundException(f))

def preprocess(
input: Inputs.SingleElement,
logger: Logger
Expand All @@ -89,7 +78,7 @@ case object ScalaPreprocessor extends Preprocessor {
(pkg :+ wrapper).map(_.raw).mkString(".")
}
val res = either {
val content = value(maybeRead(f.path))
val content = value(PreprocessingUtil.maybeRead(f.path))
val scopePath = ScopePath.fromPath(f.path)
val source = value(process(content, Right(f.path), scopePath / os.up, logger)) match {
case None =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ final case class ScriptPreprocessor(codeWrapper: CodeWrapper) extends Preprocess
): Option[Either[BuildException, Seq[PreprocessedSource]]] =
input match {
case script: Inputs.Script =>
val content = os.read(script.path)

val res = either {
val content = value(PreprocessingUtil.maybeRead(script.path))
val preprocessed = value {
ScriptPreprocessor.preprocess(
Right(script.path),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package scala.build.tests

import scala.build.Inputs
import scala.build.preprocessing.{ScalaPreprocessor, ScriptPreprocessor}
import com.eed3si9n.expecty.Expecty.{expect, assert => expect}

import scala.build.internal.CustomCodeWrapper

class PreprocessingTests extends munit.FunSuite {

test("Report error if scala file not exists") {
val logger = TestLogger()
val scalaFile = Inputs.ScalaFile(os.temp.dir(), os.SubPath("NotExists.scala"))

val res = ScalaPreprocessor.preprocess(scalaFile, logger)
val expectedMessage = s"File not found: ${scalaFile.path}"

assert(res.nonEmpty)
assert(res.get.isLeft)
expect(res.get.left.get.message == expectedMessage)
}

test("Report error if scala script not exists") {
val logger = TestLogger()
val scalaScript = Inputs.Script(os.temp.dir(), os.SubPath("NotExists.sc"))

val res = ScriptPreprocessor(CustomCodeWrapper).preprocess(scalaScript, logger)
val expectedMessage = s"File not found: ${scalaScript.path}"

assert(res.nonEmpty)
assert(res.get.isLeft)
expect(res.get.left.get.message == expectedMessage)
}
}

0 comments on commit 7d3144d

Please sign in to comment.