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..6de233aba1db 100644 --- a/scaladoc/test/dotty/tools/scaladoc/BaseHtmlTest.scala +++ b/scaladoc/test/dotty/tools/scaladoc/BaseHtmlTest.scala @@ -24,23 +24,25 @@ class BaseHtmlTest: def withGeneratedDoc( pcks: Seq[String], - docsRoot: Option[String] = None)( + docsRoot: Option[String] = None, + customArgs: Option[Scaladoc.Args] = None, + )( op: ProjectContext ?=> Unit, - ): Unit = - val dest = Files.createTempDirectory("test-doc") - try - val args = Scaladoc.Args( - name = projectName, - tastyFiles = pcks.flatMap(tastyFiles(_)), - output = dest.toFile, - docsRoot = docsRoot, - projectVersion = Some(projectVersion) - ) - Scaladoc.run(args)(using testContext) - op(using ProjectContext(dest)) - - finally IO.delete(dest.toFile) + ): 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._ @@ -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..dc9e85a3974d --- /dev/null +++ b/scaladoc/test/dotty/tools/scaladoc/site/IndexPagesTest.scala @@ -0,0 +1,39 @@ +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)) { + 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)