<commit_before>package de.tototec.sbuild.addons.scalatest

import de.tototec.sbuild.Project
import java.io.File
import org.scalatest.tools.Runner

object ScalaTest {
  def apply(
    runPath: Seq[String] = null,
    reporter: String = null,
    configMap: Map[String, String] = null,
    includes: Seq[String] = null,
    excludes: Seq[String] = null)(implicit project: Project) =
    new ScalaTest(
      runPath = runPath,
      reporter = reporter,
      configMap = configMap,
      includes = includes,
      excludes = excludes
    ).execute

}

class ScalaTest()(implicit project: Project) {

  var runPath: Seq[String] = null
  var reporter: String = null
  var configMap: Map[String, String] = null
  var includes: Seq[String] = null
  var excludes: Seq[String] = null

  def this(
    runPath: Seq[String] = null,
    reporter: String = null,
    configMap: Map[String, String] = null,
    includes: Seq[String] = null,
    excludes: Seq[String] = null)(implicit project: Project) {
    this
    this.runPath = runPath
    this.reporter = reporter
    this.configMap = configMap
    this.includes = includes
    this.excludes = excludes
  }

  def execute {

    def whiteSpaceSeparated(seq: Seq[String]): String = seq.map(_.replaceAll(" ", "\\ ")).mkString(" ")

    var args = Array[String]()
    if (runPath != null) args ++= Array("-p", whiteSpaceSeparated(runPath))
    if (reporter != null) args ++= Array("-" + reporter)
    if (configMap != null) configMap foreach {
      case (key, value) => args ++= Array("-D" + key + "=" + value)
    }
    if (includes != null) args ++= Array("-n", whiteSpaceSeparated(includes))
    if (excludes != null) args ++= Array("-n", whiteSpaceSeparated(excludes))

    Runner.run(args)

  }

}<commit_msg>ScalaTest Addon: Removed need to have ScalaTest on classpath and added a new parameter 'classpath' from which ScalaTest will be loaded. This way, one can give ScalaTests as dependency.<commit_after>package de.tototec.sbuild.addons.scalatest

import de.tototec.sbuild.Project
import java.io.File
import java.net.URLClassLoader
import de.tototec.sbuild.ExecutionFailedException

object ScalaTest {
  def apply(
    classpath: Seq[File] = null,
    runPath: Seq[String] = null,
    reporter: String = null,
    configMap: Map[String, String] = null,
    includes: Seq[String] = null,
    excludes: Seq[String] = null)(implicit project: Project) =
    new ScalaTest(
      classpath = classpath,
      runPath = runPath,
      reporter = reporter,
      configMap = configMap,
      includes = includes,
      excludes = excludes
    ).execute

}

class ScalaTest(
  var classpath: Seq[File] = null,
  var runPath: Seq[String] = null,
  var reporter: String = null,
  var configMap: Map[String, String] = null,
  var includes: Seq[String] = null,
  var excludes: Seq[String] = null)(implicit project: Project) {

  def execute {

    def whiteSpaceSeparated(seq: Seq[String]): String = seq.map(_.replaceAll(" ", "\\ ")).mkString(" ")

    // As the runPath is seq of string, and we execute ScalaTest in the current VM, it is not
    // guaranteed that we run in the project directory, so relative runPathes must be converted
    // to absolute pathes
    lazy val absoluteRunPath = runPath.map { path =>
      path match {
        case x if x.startsWith("http:") => x
        case x if x.startsWith("https:") => x
        case x if x.startsWith("file:") => x
        case x => new File(x) match {
          case f if f.isAbsolute => x
          case f => new File(project.projectDirectory, x).getPath
        }
      }
    }

    var args = Array[String]()
    if (runPath != null) args ++= Array("-p", whiteSpaceSeparated(absoluteRunPath))
    if (reporter != null) args ++= Array("-" + reporter)
    if (configMap != null) configMap foreach {
      case (key, value) => args ++= Array("-D" + key + "=" + value)
    }
    if (includes != null) args ++= Array("-n", whiteSpaceSeparated(includes))
    if (excludes != null) args ++= Array("-n", whiteSpaceSeparated(excludes))

    val cl = classpath match {
      case null => getClass.getClassLoader
      case Seq() => getClass.getClassLoader
      case cp => new URLClassLoader(cp.map { f => f.toURI().toURL() }.toArray, getClass.getClassLoader)
    }

    //    Runner.run(args)

    val runnerClass = try {
      cl.loadClass("org.scalatest.tools.Runner")
    } catch {
      case e: ClassNotFoundException =>
        throw new ExecutionFailedException("org.scalatest.tools.Runner was not found on the classpath.\nPlease add it to the 'classpath' attribute or the SBuild classapth.")
    }

    project.log.debug("Running ScalaTest with\n  classpath: " + (cl match {
      case cp: URLClassLoader => cp.getURLs.mkString(", ")
      case x => "SBuild classpath"
    }) + "\n  args: " + args.mkString(", "))

    val runMethod = runnerClass.asInstanceOf[Class[_]].getMethod("run", classOf[Array[String]])
    runMethod.invoke(null, args) match {
      case x if x.isInstanceOf[Boolean] && x == true => // success
      case _ =>
        throw new ExecutionFailedException("Some ScalaTest tests failed.")
    }

  }

}
{'fraction_non_alphanumeric': 0.12946690099589925, 'fraction_numerical': 0.0, 'mean_word_length': 3.1239935587761676, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '987507', 'n_tokens_mistral': 1634, 'n_tokens_neox': 1606, 'n_words': 457}