Skip to content

Commit

Permalink
Fix non-working checks
Browse files Browse the repository at this point in the history
  • Loading branch information
romanowski committed Oct 11, 2021
1 parent 7fec501 commit c71313a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 26 deletions.
35 changes: 20 additions & 15 deletions docs_checker/check.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,22 @@ def parse(content: Seq[String], currentCommands: Seq[Commands], context: Context
case Nil => currentCommands
case ScalaCodeBlock(name) :: tail =>
val (codeLines, rest, newContext) = untilEndOfSnippet(tail)(using context)

parse(rest, currentCommands :+ Commands.Snippet(name, codeLines, context), newContext)
case ScalaCliBlock(failGroup) :: tail =>
val (codeLines, rest, newContext) = untilEndOfSnippet(tail)
assert(codeLines.size != 0)
val runCmd = Commands.Run(codeLines.filterNot(_.trim.startsWith("#")).map(_.split(" ").toList), failGroup != null, newContext)
val runCmd = Commands.Run(
codeLines.filterNot(_.trim.startsWith("#")).map(_.split(" ").toList),
failGroup != null,
newContext
)
parse(rest, currentCommands :+ runCmd, newContext)
case CheckBlock(regexOpt) :: tail =>
val isRegex = regexOpt == "-regex"
val (patterns, rest, newContext) = untilEndOfSnippet(tail, CheckBlockEnd)
parse(rest, currentCommands :+ Commands.Check(patterns, isRegex, context), newContext)
case _ :: tail =>
case _ :: tail =>
parse(tail, currentCommands, context.copy(line = context.line + 1))

case class TestCase(path: os.Path, failure: Option[Throwable])
Expand Down Expand Up @@ -76,7 +80,7 @@ def checkFile(file: os.Path, dest: Option[os.Path]) =
val out = os.temp.dir(prefix = destName)

var lastOutput: String = null
val allSources = Set.newBuilder[os.Path]
val allSources = Set.newBuilder[os.Path]

try
println(s"Using $out as output to process $file")
Expand All @@ -87,13 +91,13 @@ def checkFile(file: os.Path, dest: Option[os.Path]) =
case Commands.Run(cmds, shouldFail, _) =>
cmds.foreach { cmd =>
println(s"### Running: ${cmd.mkString(" ")}:")
val res = os.proc(cmd).call(cwd = out,mergeErrIntoOut=true, check = false)
val res = os.proc(cmd).call(cwd = out, mergeErrIntoOut = true, check = false)
println(res.out.text())
if shouldFail then
assert(res.exitCode != 0)
else
assert(res.exitCode == 0)

val outputChunks = res.chunks.map {
case Left(c) =>
c
Expand All @@ -103,17 +107,18 @@ def checkFile(file: os.Path, dest: Option[os.Path]) =
lastOutput = res.out.text()
}
case Commands.Snippet(name, code, c) =>
val (prefixLines, codeLines) = code match
case shbang :: tail if shbang.startsWith("#!") =>
List(shbang + "\n") -> tail
case other =>
Nil -> other

val file = out / name
val (prefixLines, codeLines) =
code match
case shbang :: tail if shbang.startsWith("#!") =>
List(shbang + "\n") -> tail
case other =>
Nil -> other

val file = out / name
allSources += file
println(s"### Writting $name with:\n${codeLines.mkString("\n")}\n---")
val prefix = prefixLines.mkString("", "",s"$fakeLineMarker\n" * c.line)

val prefix = prefixLines.mkString("", "", s"$fakeLineMarker\n" * c.line)
os.write(file, code.mkString(prefix, "\n", ""))
case Commands.Check(patterns, regex, line) =>
assert(lastOutput != null, msg("No output stored from previous commands"))
Expand Down
8 changes: 4 additions & 4 deletions website/docs/cookbooks/scala-jvm.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ object Main extends App {

Pass `--jvm` to the `scala-cli` command to run your application with the specified java version.

```bash
```bash ignore
scala-cli --jvm adopt:11 Main.scala
```

<!-- Expected:
<!-- ignored Expected:
Hello from ScalaCli
-->

To test your application with Java 8, change the value of `--jvm` parameter.
```bash fail
```bash ignore fail
scala-cli --jvm 8 Main.scala
# In this case, it raises an error because the `Files.createTempFile` method is not available in java 8
#
Expand All @@ -42,7 +42,7 @@ scala-cli --jvm 8 Main.scala
# at method main in modules/runner/src/main/scala/scala/cli/runner/Runner.scala:22 inside runner_3.jar
```

<!-- Expected:
<!-- ignored Expected:
java.lang.NoSuchMethodError
java.nio.file.Files.writeString
-->
14 changes: 7 additions & 7 deletions website/docs/cookbooks/scala-versions.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ object ScalaVersion extends App {
val scalaVersion = checkScala3(manifests)
val javaVersion = System.getProperty("java.version")

println(s"Scala: $scalaVersion Java: $javaVersion")
println(s"Scala: $scalaVersion")
}
```

Expand All @@ -48,7 +48,7 @@ scala-cli ScalaVersion.scala
```

<!-- Expected-regex:
Scala: 3\..* Java: 11\..+
Scala: 3\..*
-->


Expand All @@ -60,7 +60,7 @@ Scala version can be also provided from command line using `--scala` (with `-S`
scala-cli -S 2.13.5 ScalaVersion.scala
```
<!-- Expected-regex:
Scala: 2\.13\.5 Java: .+
Scala: 2\.13\.5
-->

In most cases we do not care for a precise Scala version and 'any Scala 2' or `2.13` is good enough for us.
Expand All @@ -71,7 +71,7 @@ Scala cli accepts version prefixes so:
scala-cli -S 2 ScalaVersion.scala
```
<!-- Expected-regex:
Scala: 2\..+ Java: 11\..+
Scala: 2\..+
-->

will result in picking up a latest stable release for Scala 2 (`2.13.6` as of when this doc is written) and
Expand All @@ -80,7 +80,7 @@ will result in picking up a latest stable release for Scala 2 (`2.13.6` as of wh
scala-cli -S 2.12 ScalaVersion.scala
```
<!-- Expected-regex:
Scala: 2\.12\.15 Java: 11\..+
Scala: 2\.12\.15
-->

will use latest stable release of `2.12` `2.12.15`.
Expand Down Expand Up @@ -108,7 +108,7 @@ scala-cli ScalaVersion.scala version.scala
```

<!-- Expected-regex: TODO -
Scala: 2\.12\.5 Java: 11\..+
Scala: 2\.12\.5
-->

We will results in using `2.12.5`.
Expand All @@ -124,7 +124,7 @@ scala-cli -S 2.13.5 ScalaVersion.scala version.scala
Will result in using `2.13.5`

<!-- Expected-regex:
Scala: 2\.13\.5 Java: 11\..+
Scala: 2\.13\.5
-->

## When should I provide a full version of scala?
Expand Down

0 comments on commit c71313a

Please sign in to comment.