Skip to content

Commit e5ad9ee

Browse files
committed
Add basic integration tests for static site
1 parent 00de3af commit e5ad9ee

File tree

10 files changed

+155
-21
lines changed

10 files changed

+155
-21
lines changed

project/Build.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,7 @@ object Build {
11591159

11601160
val testcasesOutputDir = taskKey[String]("Root directory where tests classses are generated")
11611161
val testcasesSourceRoot = taskKey[String]("Root directory where tests sources are generated")
1162+
val testDocumentationRoot = taskKey[String]("Root directory where tests documentation are stored")
11621163
val generateSelfDocumentation = taskKey[Unit]("Generate example documentation")
11631164
// Note: the two tasks below should be one, but a bug in Tasty prevents that
11641165
val generateScala3Documentation = taskKey[Unit]("Generate documentation for dotty lib")
@@ -1532,7 +1533,9 @@ object Build {
15321533
buildInfoKeys in Test := Seq[BuildInfoKey](
15331534
Build.testcasesOutputDir.in(Test),
15341535
Build.testcasesSourceRoot.in(Test),
1536+
Build.testDocumentationRoot,
15351537
),
1538+
testDocumentationRoot := (baseDirectory.value / "test-documentations").getAbsolutePath,
15361539
buildInfoPackage in Test := "dotty.dokka",
15371540
BuildInfoPlugin.buildInfoScopedSettings(Test),
15381541
BuildInfoPlugin.buildInfoDefaultSettings,

scala3doc/src/dotty/dokka/Main.scala

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,13 @@ case class Args(
7878
tastyRoots: Seq[File],
7979
classpath: String,
8080
output: File,
81-
docsRoot: Option[String],
82-
projectVersion: Option[String],
83-
projectTitle: Option[String],
84-
projectLogo: Option[String],
85-
defaultSyntax: Option[Args.CommentSyntax],
86-
sourceLinks: List[String],
87-
revision: Option[String]
81+
docsRoot: Option[String] = None,
82+
projectVersion: Option[String] = None,
83+
projectTitle: Option[String] = None,
84+
projectLogo: Option[String] = None,
85+
defaultSyntax: Option[Args.CommentSyntax] = None,
86+
sourceLinks: List[String] = Nil,
87+
revision: Option[String] = None
8888
)
8989

9090
object Args:
@@ -121,12 +121,8 @@ enum DocConfiguration extends BaseDocConfiguration:
121121
* - [](package.DottyDokkaConfig) is our config for Dokka.
122122
*/
123123
object Main:
124-
def main(args: Array[String]): Unit =
124+
def main(parsedArgs: Args): Unit =
125125
try
126-
val rawArgs = new RawArgs
127-
new CmdLineParser(rawArgs).parseArgument(args:_*)
128-
val parsedArgs = rawArgs.toArgs
129-
130126
val (files, dirs) = parsedArgs.tastyRoots.partition(_.isFile)
131127
val (providedTastyFiles, jars) = files.toList.map(_.getAbsolutePath).partition(_.endsWith(".tasty"))
132128
jars.foreach(j => if(!j.endsWith(".jar")) sys.error(s"Provided file $j is not jar not tasty file") )
@@ -147,11 +143,17 @@ object Main:
147143
new DokkaGenerator(new DottyDokkaConfig(config), DokkaConsoleLogger.INSTANCE).generate()
148144

149145
println("Done")
150-
151-
// Sometimes jvm is hanging, so we want to be sure that we force shout down the jvm
152-
sys.exit(0)
153146
catch
154147
case a: Exception =>
155148
a.printStackTrace()
156149
// Sometimes jvm is hanging, so we want to be sure that we force shout down the jvm
157150
sys.exit(1)
151+
152+
def main(args: Array[String]): Unit =
153+
val rawArgs = new RawArgs
154+
new CmdLineParser(rawArgs).parseArgument(args:_*)
155+
main(rawArgs.toArgs)
156+
// Sometimes jvm is hanging, so we want to be sure that we force shout down the jvm
157+
sys.exit(0)
158+
159+

scala3doc/src/dotty/dokka/transformers/PackageHierarchyTransformer.scala

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,9 @@ class PackageHierarchyTransformer(context: DokkaContext) extends PageTransformer
6363

6464
val packagePagesWithTokens = packagePages.map(page => (("""\.""".r.split(page.getName)).toSeq, page))
6565

66-
val maxDepthElem = packagePagesWithTokens.maxBy( (tokens, page) => tokens.size )
67-
68-
page.modified(
69-
page.getName,
66+
val newPages = if packagePagesWithTokens.isEmpty then page.getChildren else
67+
val maxDepthElem = packagePagesWithTokens.maxBy( (tokens, page) => tokens.size )
7068
(otherPages ++ buildPackageTree(maxDepthElem(0).size, packagePagesWithTokens, Seq.empty)).asJava
71-
)
72-
7369

70+
page.modified(page.getName, newPages)
7471
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
title: Adoc
3+
---
4+
# Header in Adoc
5+
6+
And a text!
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
title: A directory
3+
---
4+
# {{ page.title }}
5+
6+
And a text!
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
title: Nested in a directory
3+
---
4+
# {{ page.title }}
5+
6+
And a text!
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# {{ page.title }} in header
2+
3+
And a text!

scala3doc/test-documentations/basic/images/basic.svg

Lines changed: 30 additions & 0 deletions
Loading
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
title: Basic test
3+
---
4+
# Header
5+
6+
And a text!
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package dotty.dokka
2+
package site
3+
4+
import java.nio.file.Files
5+
import java.nio.file.Path
6+
import java.nio.file.Paths
7+
import org.junit.Test
8+
import org.junit.Assert._
9+
import org.jsoup.Jsoup
10+
import org.jsoup.nodes.Document
11+
import java.nio.charset.Charset
12+
13+
14+
class SiteGeneratationTest:
15+
val projectName = "Test Project Name"
16+
val projectTitle = "Test Project Title"
17+
val projectVersion = "1.0.1-M1"
18+
19+
private def withGeneratedSite(base: Path)(op: Path => Unit) =
20+
val dest = Files.createTempDirectory("test-doc")
21+
try
22+
val args = Args(
23+
name = projectName,
24+
tastyRoots = Nil ,
25+
classpath = System.getProperty("java.class.path"),
26+
output = dest.toFile,
27+
docsRoot = Some(base.toAbsolutePath.toString),
28+
projectVersion = Some(projectVersion),
29+
projectTitle = Some(projectTitle)
30+
)
31+
Main.main(args)
32+
op(dest)
33+
34+
finally println(dest.toFile) // IO.delete(dest.toFile)
35+
36+
val testDocPath = Paths.get(BuildInfo.testDocumentationRoot)
37+
38+
class DocumentContext(d: Document, path: Path):
39+
import collection.JavaConverters._
40+
41+
def niceMsg(msg: String) = s"$msg in $path (body):\n ${d.html()}:\n"
42+
43+
def assertTextsIn(selector: String, expected: String*) =
44+
assertFalse(niceMsg("Selector not found"), d.select(selector).isEmpty)
45+
val found = d.select(selector).eachText.asScala
46+
assertEquals(niceMsg(s"Context does not match for '$selector'"), expected.toList, found.toList)
47+
48+
def withHtmlFile(path: Path)(op: DocumentContext => Unit) = {
49+
assertTrue(s"File at $path does not exisits!", Files.exists(path))
50+
val content = new String(Files.readAllBytes(path), Charset.defaultCharset())
51+
val document = Jsoup.parse(content)
52+
op(DocumentContext(document, path))
53+
}
54+
55+
@Test
56+
def basicTest() = withGeneratedSite(testDocPath.resolve("basic")){ dest =>
57+
58+
def checkFile(path: String)(title: String, header: String, parents: Seq[String] = Nil) =
59+
withHtmlFile(dest.resolve(path)){ content =>
60+
content.assertTextsIn(".projectName", projectName)
61+
content.assertTextsIn(".projectVersion", projectVersion)
62+
content.assertTextsIn("h1", header)
63+
content.assertTextsIn("title", title)
64+
content.assertTextsIn(".breadcrumbs a", (parents :+ title):_*)
65+
}
66+
67+
checkFile("index.html")(title = "Basic test", header = "Header")
68+
checkFile("docs/Adoc.html")(title = "Adoc", header = "Header in Adoc", parents = Seq(projectTitle))
69+
checkFile("docs/Adoc.html")(title = "Adoc", header = "Header in Adoc", parents = Seq(projectTitle))
70+
checkFile("docs/dir/index.html")(title = "A directory", header = "A directory", parents = Seq(projectTitle))
71+
checkFile("docs/dir/nested.html")(
72+
title = "Nested in a directory", header = "Nested in a directory", parents = Seq(projectTitle, "A directory"))
73+
74+
checkFile("docs/index.html")(title = projectTitle, header = s"$projectTitle in header")
75+
}

0 commit comments

Comments
 (0)