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

Report error if scala script not exists #538

Merged
merged 1 commit into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}