|
| 1 | +/* |
| 2 | + * Copyright 2019-2026 Ossum, Inc. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +package com.ossuminc.riddl.commands |
| 8 | + |
| 9 | +import com.ossuminc.riddl.utils.{pc, PlatformContext} |
| 10 | +import org.ekrich.config.* |
| 11 | +import org.scalatest.matchers.must.Matchers |
| 12 | +import org.scalatest.wordspec.AnyWordSpec |
| 13 | + |
| 14 | +import java.nio.file.{Files, Path} |
| 15 | +import scala.jdk.StreamConverters.* |
| 16 | + |
| 17 | +/** Comprehensive BAST round-trip test against all riddl-models. |
| 18 | + * |
| 19 | + * For each model in the riddl-models repository: |
| 20 | + * 1. Validate the original RIDDL source |
| 21 | + * 2. Bastify (RIDDL -> BAST) |
| 22 | + * 3. Unbastify (BAST -> RIDDL via PrettifyPass with flatten=true) |
| 23 | + * 4. Prettify original with --single-file (same code path) |
| 24 | + * 5. Compare unbastify output with prettified original |
| 25 | + * |
| 26 | + * Both outputs go through PrettifyPass, so any PrettifyPass |
| 27 | + * formatting quirks cancel out. What we're testing is that |
| 28 | + * the BAST serialization/deserialization is lossless. |
| 29 | + */ |
| 30 | +class RiddlModelsRoundTripTest extends AnyWordSpec with Matchers { |
| 31 | + |
| 32 | + given io: PlatformContext = pc |
| 33 | + |
| 34 | + private val riddlModelsDir = Path.of("../riddl-models") |
| 35 | + |
| 36 | + private val commonArgs = Array( |
| 37 | + "--quiet", |
| 38 | + "--show-missing-warnings=false", |
| 39 | + "--show-style-warnings=false", |
| 40 | + "--show-usage-warnings=false" |
| 41 | + ) |
| 42 | + |
| 43 | + "BAST round-trip" should { |
| 44 | + assume( |
| 45 | + Files.isDirectory(riddlModelsDir), |
| 46 | + "riddl-models not found at ../riddl-models — skipping" |
| 47 | + ) |
| 48 | + |
| 49 | + val confFiles = discoverModels(riddlModelsDir) |
| 50 | + assume(confFiles.nonEmpty, "No .conf files found in riddl-models") |
| 51 | + |
| 52 | + confFiles.foreach { case (confFile, riddlFile) => |
| 53 | + val relPath = riddlModelsDir.relativize(confFile.getParent) |
| 54 | + |
| 55 | + s"round-trip $relPath" in { |
| 56 | + roundTripTest(confFile, riddlFile) |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** Discover models: find .conf files at depth 3, parse input-file */ |
| 62 | + private def discoverModels(base: Path): Seq[(Path, Path)] = { |
| 63 | + val allConf = Files |
| 64 | + .walk(base, 5) |
| 65 | + .filter(p => |
| 66 | + p.toString.endsWith(".conf") && Files.isRegularFile(p) |
| 67 | + ) |
| 68 | + .toScala(Seq) |
| 69 | + |
| 70 | + allConf.flatMap { confFile => |
| 71 | + val depth = base.relativize(confFile).getNameCount - 1 |
| 72 | + if depth == 3 then |
| 73 | + parseInputFile(confFile).map(riddlFile => |
| 74 | + (confFile, riddlFile) |
| 75 | + ) |
| 76 | + else None |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + /** Parse a .conf file to extract the input-file path */ |
| 81 | + private def parseInputFile(confFile: Path): Option[Path] = { |
| 82 | + try { |
| 83 | + val config = ConfigFactory.parseFile(confFile.toFile) |
| 84 | + if config.hasPath("validate.input-file") then |
| 85 | + val inputFile = config.getString("validate.input-file") |
| 86 | + Some(confFile.getParent.resolve(inputFile)) |
| 87 | + else None |
| 88 | + } catch { |
| 89 | + case _: ConfigException => None |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /** Run the round-trip for a single model: |
| 94 | + * 1. Validate original |
| 95 | + * 2. Bastify |
| 96 | + * 3. Unbastify (produces flattened .riddl) |
| 97 | + * 4. Prettify original with --single-file |
| 98 | + * 5. Compare unbastify output with prettified original |
| 99 | + */ |
| 100 | + private def roundTripTest( |
| 101 | + confFile: Path, |
| 102 | + riddlFile: Path |
| 103 | + ): Unit = { |
| 104 | + val tempDir = Files.createTempDirectory("bast-roundtrip") |
| 105 | + val unbastDir = tempDir.resolve("unbast") |
| 106 | + val prettyDir = tempDir.resolve("pretty-original") |
| 107 | + |
| 108 | + try { |
| 109 | + val riddlPath = riddlFile.toAbsolutePath.toString |
| 110 | + val bastPath = riddlPath.replaceAll("\\.riddl$", ".bast") |
| 111 | + |
| 112 | + // Step 1: Validate original |
| 113 | + val validateArgs = |
| 114 | + commonArgs ++ Array("validate", riddlPath) |
| 115 | + Commands.runMainForTest(validateArgs) match { |
| 116 | + case Left(messages) => |
| 117 | + fail( |
| 118 | + s"Step 1 (validate original) failed:\n" + |
| 119 | + s"${messages.format}" |
| 120 | + ) |
| 121 | + case Right(_) => // ok |
| 122 | + } |
| 123 | + |
| 124 | + // Step 2: Bastify |
| 125 | + val bastifyArgs = |
| 126 | + commonArgs ++ Array("bastify", riddlPath) |
| 127 | + Commands.runMainForTest(bastifyArgs) match { |
| 128 | + case Left(messages) => |
| 129 | + fail(s"Step 2 (bastify) failed:\n${messages.format}") |
| 130 | + case Right(_) => |
| 131 | + assert( |
| 132 | + Files.exists(Path.of(bastPath)), |
| 133 | + s"BAST file not created: $bastPath" |
| 134 | + ) |
| 135 | + } |
| 136 | + |
| 137 | + try { |
| 138 | + // Step 3: Unbastify (uses PrettifyPass with flatten=true) |
| 139 | + val unbastifyArgs = commonArgs ++ Array( |
| 140 | + "unbastify", |
| 141 | + bastPath, |
| 142 | + "-o", |
| 143 | + unbastDir.toAbsolutePath.toString |
| 144 | + ) |
| 145 | + Commands.runMainForTest(unbastifyArgs) match { |
| 146 | + case Left(messages) => |
| 147 | + fail( |
| 148 | + s"Step 3 (unbastify) failed:\n${messages.format}" |
| 149 | + ) |
| 150 | + case Right(_) => |
| 151 | + assert( |
| 152 | + Files.exists(unbastDir), |
| 153 | + s"Unbastify output dir not created" |
| 154 | + ) |
| 155 | + } |
| 156 | + |
| 157 | + // Find the unbastify output file |
| 158 | + val outputRiddlFiles = Files |
| 159 | + .list(unbastDir) |
| 160 | + .filter(p => p.toString.endsWith(".riddl")) |
| 161 | + .toScala(Seq) |
| 162 | + assert( |
| 163 | + outputRiddlFiles.nonEmpty, |
| 164 | + "No .riddl files in unbastify output" |
| 165 | + ) |
| 166 | + val unbastContent = |
| 167 | + Files.readString(outputRiddlFiles.head) |
| 168 | + |
| 169 | + // Step 4: Prettify original with --single-file |
| 170 | + val prettyArgs = commonArgs ++ Array( |
| 171 | + "prettify", |
| 172 | + riddlPath, |
| 173 | + "-o", |
| 174 | + prettyDir.toAbsolutePath.toString, |
| 175 | + "-s", |
| 176 | + "true" |
| 177 | + ) |
| 178 | + Commands.runMainForTest(prettyArgs) match { |
| 179 | + case Left(messages) => |
| 180 | + fail( |
| 181 | + s"Step 4 (prettify original) failed:\n" + |
| 182 | + s"${messages.format}" |
| 183 | + ) |
| 184 | + case Right(_) => // ok |
| 185 | + } |
| 186 | + |
| 187 | + val prettyFile = |
| 188 | + prettyDir.resolve("prettify-output.riddl") |
| 189 | + assert( |
| 190 | + Files.exists(prettyFile), |
| 191 | + "Prettified original not found" |
| 192 | + ) |
| 193 | + val prettyContent = Files.readString(prettyFile) |
| 194 | + |
| 195 | + // Step 5: Compare unbastify output with prettified |
| 196 | + // original. Both go through PrettifyPass, so format |
| 197 | + // quirks cancel out. Differences = BAST data loss. |
| 198 | + if unbastContent != prettyContent then |
| 199 | + val lines1 = |
| 200 | + prettyContent.linesIterator.toIndexedSeq |
| 201 | + val lines2 = |
| 202 | + unbastContent.linesIterator.toIndexedSeq |
| 203 | + val firstDiff = lines1 |
| 204 | + .zipAll(lines2, "<missing>", "<missing>") |
| 205 | + .zipWithIndex |
| 206 | + .find { case ((a, b), _) => a != b } |
| 207 | + |
| 208 | + firstDiff match { |
| 209 | + case Some(((line1, line2), idx)) => |
| 210 | + fail( |
| 211 | + s"Round-trip differs at line ${idx + 1}:\n" + |
| 212 | + s" original: $line1\n" + |
| 213 | + s" round-trip: $line2" |
| 214 | + ) |
| 215 | + case None => |
| 216 | + if lines1.length != lines2.length then |
| 217 | + fail( |
| 218 | + s"Round-trip differs in length: " + |
| 219 | + s"${lines1.length} vs ${lines2.length}" |
| 220 | + ) |
| 221 | + end if |
| 222 | + } |
| 223 | + end if |
| 224 | + } finally { |
| 225 | + // Clean up .bast file created next to source |
| 226 | + Files.deleteIfExists(Path.of(bastPath)) |
| 227 | + } |
| 228 | + } finally { |
| 229 | + deleteRecursively(tempDir) |
| 230 | + } |
| 231 | + } |
| 232 | + |
| 233 | + private def deleteRecursively(path: Path): Unit = { |
| 234 | + if Files.isDirectory(path) then |
| 235 | + Files |
| 236 | + .list(path) |
| 237 | + .forEach(p => |
| 238 | + deleteRecursively(p.asInstanceOf[Path]) |
| 239 | + ) |
| 240 | + end if |
| 241 | + Files.deleteIfExists(path) |
| 242 | + } |
| 243 | +} |
0 commit comments