<commit_before>package scalapb.compiler

import java.io.{File, FileInputStream, PrintWriter}
import java.nio.file.Files

import scala.collection.JavaConverters._
import com.google.protobuf.DescriptorProtos.FileDescriptorSet
import com.google.protobuf.Descriptors.FileDescriptor
import org.scalatest.{FlatSpec, MustMatchers}

class ProtoValidationSpec extends FlatSpec with MustMatchers {
  def generateFileSet(files: Seq[(String, String)]) = {
    val tmpDir = Files.createTempDirectory("validation").toFile
    val fileNames = files.map {
      case (name, content) =>
        val file = new File(tmpDir, name)
        val pw   = new PrintWriter(file)
        pw.write(content)
        pw.close()
        file.getAbsoluteFile
    }
    val outFile = new File(tmpDir, "descriptor.out")

    require(
      com.github.os72.protocjar.Protoc.runProtoc(
        Array(
          "-I",
          tmpDir.toString,
          s"--descriptor_set_out=${outFile.toString}",
          "--include_imports"
        ) ++ fileNames.map(_.toString)
      ) == 0,
      "protoc exited with an error"
    )

    val fileset: Seq[FileDescriptor] = {
      val fin = new FileInputStream(outFile)
      val fileset = try {
        FileDescriptorSet.parseFrom(fin)
      } finally {
        fin.close()
      }
      fileset.getFileList.asScala
        .foldLeft[Map[String, FileDescriptor]](Map.empty) {
          case (acc, fp) =>
            val deps = fp.getDependencyList.asScala.map(acc)
            acc + (fp.getName -> FileDescriptor.buildFrom(fp, deps.toArray))
        }
        .values
        .toVector
    }
    fileset
  }

  def runValidation(generatorParams: GeneratorParams, files: (String, String)*): Unit = {
    val fileset    = generateFileSet(files)
    val validation = new ProtoValidation(new DescriptorImplicits(generatorParams, fileset))
    fileset.foreach(validation.validateFile)
  }

  def runValidation(files: (String, String)*): Unit = {
    runValidation(new GeneratorParams(), files: _*)
  }

  "simple message" should "validate" in {
    val r = runValidation(
      "file.proto" ->
        """
          |syntax = "proto2";
          |message Foo { };
    """.stripMargin
    )
  }

  "No package name" should "fail validation when flat_package is true" in {
    intercept[GeneratorException] {
      runValidation(
         new GeneratorParams(flatPackage = true),
        "file.proto" ->
          """
            |syntax = "proto2";
          """.stripMargin
      )
    }.message must include(
      "a package name is required"
    )
  }

  "UNRECOGNIZED enum value" should "fail validation" in {<commit_msg>Remove test for packageless protos<commit_after>package scalapb.compiler

import java.io.{File, FileInputStream, PrintWriter}
import java.nio.file.Files

import scala.collection.JavaConverters._
import com.google.protobuf.DescriptorProtos.FileDescriptorSet
import com.google.protobuf.Descriptors.FileDescriptor
import org.scalatest.{FlatSpec, MustMatchers}

class ProtoValidationSpec extends FlatSpec with MustMatchers {
  def generateFileSet(files: Seq[(String, String)]) = {
    val tmpDir = Files.createTempDirectory("validation").toFile
    val fileNames = files.map {
      case (name, content) =>
        val file = new File(tmpDir, name)
        val pw   = new PrintWriter(file)
        pw.write(content)
        pw.close()
        file.getAbsoluteFile
    }
    val outFile = new File(tmpDir, "descriptor.out")

    require(
      com.github.os72.protocjar.Protoc.runProtoc(
        Array(
          "-I",
          tmpDir.toString,
          s"--descriptor_set_out=${outFile.toString}",
          "--include_imports"
        ) ++ fileNames.map(_.toString)
      ) == 0,
      "protoc exited with an error"
    )

    val fileset: Seq[FileDescriptor] = {
      val fin = new FileInputStream(outFile)
      val fileset = try {
        FileDescriptorSet.parseFrom(fin)
      } finally {
        fin.close()
      }
      fileset.getFileList.asScala
        .foldLeft[Map[String, FileDescriptor]](Map.empty) {
          case (acc, fp) =>
            val deps = fp.getDependencyList.asScala.map(acc)
            acc + (fp.getName -> FileDescriptor.buildFrom(fp, deps.toArray))
        }
        .values
        .toVector
    }
    fileset
  }

  def runValidation(generatorParams: GeneratorParams, files: (String, String)*): Unit = {
    val fileset    = generateFileSet(files)
    val validation = new ProtoValidation(new DescriptorImplicits(generatorParams, fileset))
    fileset.foreach(validation.validateFile)
  }

  def runValidation(files: (String, String)*): Unit = {
    runValidation(new GeneratorParams(), files: _*)
  }

  "simple message" should "validate" in {
    val r = runValidation(
      "file.proto" ->
        """
          |syntax = "proto2";
          |message Foo { };
    """.stripMargin
    )
  }

  "UNRECOGNIZED enum value" should "fail validation" in {
{'fraction_non_alphanumeric': 0.11197068403908794, 'fraction_numerical': 0.0018322475570032573, 'mean_word_length': 2.617820324005891, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 12, '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': '6204551', 'n_tokens_mistral': 1498, 'n_tokens_neox': 1467, 'n_words': 316}