From f5fd5104c730214ffb81253fcca2b44d155ef8ee Mon Sep 17 00:00:00 2001 From: Andrzej Ratajczak Date: Thu, 7 Oct 2021 09:59:21 +0200 Subject: [PATCH 1/2] Fix top level index. Add integration tests for indexes. --- .../tools/scaladoc/renderers/Locations.scala | 2 +- .../dotty/tools/scaladoc/BaseHtmlTest.scala | 33 ++++++++------- .../tools/scaladoc/site/IndexPagesTest.scala | 40 +++++++++++++++++++ .../tools/scaladoc/site/NavigationTest.scala | 2 +- 4 files changed, 61 insertions(+), 16 deletions(-) create mode 100644 scaladoc/test/dotty/tools/scaladoc/site/IndexPagesTest.scala diff --git a/scaladoc/src/dotty/tools/scaladoc/renderers/Locations.scala b/scaladoc/src/dotty/tools/scaladoc/renderers/Locations.scala index e65ae4260590..23b5c9d9479a 100644 --- a/scaladoc/src/dotty/tools/scaladoc/renderers/Locations.scala +++ b/scaladoc/src/dotty/tools/scaladoc/renderers/Locations.scala @@ -35,7 +35,7 @@ trait Locations(using ctx: DocContext): val path = dri match case `docsRootDRI` => List("docs", "index") case `apiPageDRI` => - if ctx.args.apiSubdirectory || ctx.staticSiteContext.fold(false)(_.hasIndexFile) + if ctx.staticSiteContext.fold(false)(_.hasIndexFile) then List("api", "index") else List("index") case dri if dri.isStaticFile => diff --git a/scaladoc/test/dotty/tools/scaladoc/BaseHtmlTest.scala b/scaladoc/test/dotty/tools/scaladoc/BaseHtmlTest.scala index 5285f996ec1d..96efa7bcd907 100644 --- a/scaladoc/test/dotty/tools/scaladoc/BaseHtmlTest.scala +++ b/scaladoc/test/dotty/tools/scaladoc/BaseHtmlTest.scala @@ -23,23 +23,25 @@ class BaseHtmlTest: withGeneratedDoc(Seq("site"), docsRoot = Some(base.toAbsolutePath.toString))(op) def withGeneratedDoc( - pcks: Seq[String], - docsRoot: Option[String] = None)( - op: ProjectContext ?=> Unit, + pcks: Seq[String], + docsRoot: Option[String] = None, + customArgs: Option[Scaladoc.Args] = None, + )( + op: ProjectContext ?=> Unit, ): Unit = - val dest = Files.createTempDirectory("test-doc") + val dest = customArgs.map(_.output).getOrElse(Files.createTempDirectory("test-doc").toFile) try - val args = Scaladoc.Args( - name = projectName, - tastyFiles = pcks.flatMap(tastyFiles(_)), - output = dest.toFile, - docsRoot = docsRoot, - projectVersion = Some(projectVersion) - ) + val args = customArgs.getOrElse(Scaladoc.Args( + name = projectName, + tastyFiles = pcks.flatMap(tastyFiles(_)), + output = dest, + docsRoot = docsRoot, + projectVersion = Some(projectVersion) + )) Scaladoc.run(args)(using testContext) - op(using ProjectContext(dest)) + op(using ProjectContext(args.output.toPath)) - finally IO.delete(dest.toFile) + finally IO.delete(dest) class DocumentContext(d: Document, path: Path): import collection.JavaConverters._ @@ -49,7 +51,7 @@ class BaseHtmlTest: def assertTextsIn(selector: String, expected: String*) = assertFalse(niceMsg(s"Selector not found for '$selector'"), d.select(selector).isEmpty) val found = d.select(selector).eachText.asScala - assertEquals(niceMsg(s"Context does not match for '$selector'"), expected.toList, found.toList) + assertEquals(niceMsg(s"Content does not match for '$selector'"), expected.toList, found.toList) def assertAttr(selector: String, attr: String, expected: String*) = assertFalse(niceMsg(s"Selector '$selector' not found"), d.select(selector).isEmpty) @@ -60,6 +62,9 @@ class BaseHtmlTest: val msg = niceMsg(s"Selector '$selector' exisits in document") assertTrue(msg, d.select(selector).isEmpty) + def fileExists = + assertTrue(path.toFile.exists) + def withHtmlFile(pathStr: String)(op: DocumentContext => Unit)(using ProjectContext) = val path = summon[ProjectContext].path.resolve(pathStr) assertTrue(s"File at $path does not exisits!", Files.exists(path)) diff --git a/scaladoc/test/dotty/tools/scaladoc/site/IndexPagesTest.scala b/scaladoc/test/dotty/tools/scaladoc/site/IndexPagesTest.scala new file mode 100644 index 000000000000..4ef0443ca97a --- /dev/null +++ b/scaladoc/test/dotty/tools/scaladoc/site/IndexPagesTest.scala @@ -0,0 +1,40 @@ +package dotty.tools.scaladoc +package site + +import org.junit.Test +import java.nio.file.Files + +class IndexPagesTest extends BaseHtmlTest: + + + private val baseArgs = Scaladoc.Args( + name = projectName, + tastyFiles = Seq("site").flatMap(tastyFiles(_)), + output = Files.createTempDirectory("test-doc").toFile, + projectVersion = Some(projectVersion) + ) + + @Test + def staticSiteAndApiSubdirectory = gridTest(baseArgs.copy(docsRoot = Some(testDocPath.resolve("noIndexes").toAbsolutePath.toString), apiSubdirectory = true)) + + @Test + def staticSiteAndNOApiSubdirectoryAndReadyToGoIndex = gridTest(baseArgs.copy(docsRoot = Some(testDocPath.resolve("basic").toAbsolutePath.toString), apiSubdirectory = false)) + + @Test + def staticSiteAndApiSubdirectoryAndReadyToGoIndex = gridTest(baseArgs.copy(docsRoot = Some(testDocPath.resolve("basic").toAbsolutePath.toString), apiSubdirectory = true)) + + @Test + def staticSiteAndNOApiSubdirectory = gridTest(baseArgs.copy(docsRoot = Some(testDocPath.resolve("noIndexes").toAbsolutePath.toString), apiSubdirectory = false)) + + @Test + def NOstaticSiteAndApSubdirectory = gridTest(baseArgs.copy(docsRoot = None, apiSubdirectory = true)) + + @Test + def NOstaticSiteAndNOApiSubdirectory = gridTest(baseArgs.copy(docsRoot = None, apiSubdirectory = false)) + + private def gridTest(args: Scaladoc.Args) = withGeneratedDoc(Seq.empty, None, customArgs = Some(args)) { + println(args.output) + withHtmlFile("index.html") { content => + content.fileExists + } + } diff --git a/scaladoc/test/dotty/tools/scaladoc/site/NavigationTest.scala b/scaladoc/test/dotty/tools/scaladoc/site/NavigationTest.scala index ebc74ef31561..c8172da4833f 100644 --- a/scaladoc/test/dotty/tools/scaladoc/site/NavigationTest.scala +++ b/scaladoc/test/dotty/tools/scaladoc/site/NavigationTest.scala @@ -22,7 +22,7 @@ class NavigationTest extends BaseHtmlTest: @Test - def testBasicNavigation() = withGeneratedSite(testDocPath.resolve("basic")){ + def testBasicNavigation() = withGeneratedSite(testDocPath.resolve("basic")) { val topLevelNav = NavMenuTestEntry(projectName, "index.html", Seq( NavMenuTestEntry("A directory", "dir/index.html", Seq( NavMenuTestEntry("Nested in a directory", "dir/nested.html", Nil) From 36d1798a347ed93cde4f40df2ddb28aeae6c4a77 Mon Sep 17 00:00:00 2001 From: Andrzej Ratajczak Date: Mon, 11 Oct 2021 12:23:36 +0200 Subject: [PATCH 2/2] Apply requested changes --- .../dotty/tools/scaladoc/BaseHtmlTest.scala | 38 +++++++++---------- .../tools/scaladoc/site/IndexPagesTest.scala | 1 - 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/scaladoc/test/dotty/tools/scaladoc/BaseHtmlTest.scala b/scaladoc/test/dotty/tools/scaladoc/BaseHtmlTest.scala index 96efa7bcd907..6de233aba1db 100644 --- a/scaladoc/test/dotty/tools/scaladoc/BaseHtmlTest.scala +++ b/scaladoc/test/dotty/tools/scaladoc/BaseHtmlTest.scala @@ -23,26 +23,26 @@ class BaseHtmlTest: withGeneratedDoc(Seq("site"), docsRoot = Some(base.toAbsolutePath.toString))(op) def withGeneratedDoc( - pcks: Seq[String], - docsRoot: Option[String] = None, - customArgs: Option[Scaladoc.Args] = None, - )( - op: ProjectContext ?=> Unit, - ): Unit = - val dest = customArgs.map(_.output).getOrElse(Files.createTempDirectory("test-doc").toFile) - try - val args = customArgs.getOrElse(Scaladoc.Args( - name = projectName, - tastyFiles = pcks.flatMap(tastyFiles(_)), - output = dest, - docsRoot = docsRoot, - projectVersion = Some(projectVersion) - )) - Scaladoc.run(args)(using testContext) - op(using ProjectContext(args.output.toPath)) - - finally IO.delete(dest) + pcks: Seq[String], + docsRoot: Option[String] = None, + customArgs: Option[Scaladoc.Args] = None, + )( + op: ProjectContext ?=> Unit, + ): Unit = + val dest = customArgs.fold(Files.createTempDirectory("test-doc").toFile)(_.output) + try + val args = customArgs.getOrElse(Scaladoc.Args( + name = projectName, + tastyFiles = pcks.flatMap(tastyFiles(_)), + output = dest, + docsRoot = docsRoot, + projectVersion = Some(projectVersion) + )) + Scaladoc.run(args)(using testContext) + op(using ProjectContext(args.output.toPath)) + finally IO.delete(dest) + end withGeneratedDoc class DocumentContext(d: Document, path: Path): import collection.JavaConverters._ diff --git a/scaladoc/test/dotty/tools/scaladoc/site/IndexPagesTest.scala b/scaladoc/test/dotty/tools/scaladoc/site/IndexPagesTest.scala index 4ef0443ca97a..dc9e85a3974d 100644 --- a/scaladoc/test/dotty/tools/scaladoc/site/IndexPagesTest.scala +++ b/scaladoc/test/dotty/tools/scaladoc/site/IndexPagesTest.scala @@ -33,7 +33,6 @@ class IndexPagesTest extends BaseHtmlTest: def NOstaticSiteAndNOApiSubdirectory = gridTest(baseArgs.copy(docsRoot = None, apiSubdirectory = false)) private def gridTest(args: Scaladoc.Args) = withGeneratedDoc(Seq.empty, None, customArgs = Some(args)) { - println(args.output) withHtmlFile("index.html") { content => content.fileExists }