Skip to content

Commit

Permalink
Merge pull request #13710 from dotty-staging/scaladoc/index-tests
Browse files Browse the repository at this point in the history
Fix top level index. Add integration tests for indexes.
  • Loading branch information
BarkingBad authored Oct 11, 2021
2 parents f8b869b + 36d1798 commit a771c86
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand Down
37 changes: 21 additions & 16 deletions scaladoc/test/dotty/tools/scaladoc/BaseHtmlTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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._

Expand All @@ -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)
Expand All @@ -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))
Expand Down
39 changes: 39 additions & 0 deletions scaladoc/test/dotty/tools/scaladoc/site/IndexPagesTest.scala
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a771c86

Please sign in to comment.