From 20bd82d30881f8b8439ea49baab923bc04ff1f2e Mon Sep 17 00:00:00 2001 From: Robert Stoll Date: Thu, 21 Jun 2018 22:35:31 +0200 Subject: Use canonicalPath instead of absolutePath for srcLink This way a user can define "./" instead of an absolute path to the root of the project dir (or a user can use ../ etc.). Thus: - use canonicalPath in: - SourceLinkDefinitionImpl::parseSourceLinkDefinition - and DocumentationNode.appendSourceLink => here because if the config is deserialized we bypass parseSourceLinkDefinition - also use canonicalPath for the path of PsiElement Moreover: - make sure the comparison works for unix and windows paths - fixes #289 --- core/src/test/kotlin/model/SourceLinksTest.kt | 76 +++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 core/src/test/kotlin/model/SourceLinksTest.kt (limited to 'core/src/test/kotlin/model/SourceLinksTest.kt') diff --git a/core/src/test/kotlin/model/SourceLinksTest.kt b/core/src/test/kotlin/model/SourceLinksTest.kt new file mode 100644 index 00000000..1a74506f --- /dev/null +++ b/core/src/test/kotlin/model/SourceLinksTest.kt @@ -0,0 +1,76 @@ +package org.jetbrains.dokka.tests.model + +import org.jetbrains.dokka.NodeKind +import org.jetbrains.dokka.SourceLinkDefinitionImpl +import org.jetbrains.dokka.tests.verifyModel +import org.junit.Assert +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.Parameterized +import java.io.File + +@RunWith(Parameterized::class) +class SourceLinksTest( + private val srcLink: String, + private val url: String, + private val lineSuffix: String?, + private val expectedUrl: String +) { + + @Test + fun test() { + val link = if(srcLink.contains(sourceLinks)){ + srcLink.substringBeforeLast(sourceLinks) + sourceLinks + } else { + srcLink.substringBeforeLast(testdata) + testdata + } + val sourceLink = SourceLinkDefinitionImpl(link, url, lineSuffix) + + verifyModel(filePath, sourceLinks = listOf(sourceLink)) { model -> + with(model.members.single().members.single()) { + Assert.assertEquals("foo", name) + Assert.assertEquals(NodeKind.Function, kind) + Assert.assertEquals(expectedUrl, details(NodeKind.SourceUrl).single().name) + } + } + } + + companion object { + private const val testdata = "testdata" + private const val sourceLinks = "sourceLinks" + private const val dummy = "dummy.kt" + private const val pathSuffix = "$sourceLinks/$dummy" + private const val filePath = "$testdata/$pathSuffix/../dummy.kt" + private const val url = "https://example.com" + + @Parameterized.Parameters(name = "{index}: {0}, {1}, {2} = {3}") + @JvmStatic + fun data(): Collection> { + val longestPath = File(testdata).absolutePath.removeSuffix("/") + "/../$testdata/" + val maxLength = longestPath.length + val list = listOf( + arrayOf(File(testdata).absolutePath.removeSuffix("/"), "$url/$pathSuffix"), + arrayOf(File("$testdata/$sourceLinks").absolutePath.removeSuffix("/") + "/", "$url/$dummy"), + arrayOf(longestPath, "$url/$pathSuffix"), + + arrayOf(testdata, "$url/$pathSuffix"), + arrayOf("./$testdata", "$url/$pathSuffix"), + arrayOf("../core/$testdata", "$url/$pathSuffix"), + arrayOf("$testdata/$sourceLinks", "$url/$dummy"), + arrayOf("./$testdata/../$testdata/$sourceLinks", "$url/$dummy") + ) + val allPaths = list + + // we want to be sure Windows paths work as well + list.map { arrayOf(it[0].replace('/', '\\'), it[1]) } + return allPaths.map { arrayOf(it[0].padEnd(maxLength, '_'), url, null, it[1]) } + + listOf( + // check that it also works if url ends with / + arrayOf((File(testdata).absolutePath.removeSuffix("/") + "/").padEnd(maxLength, '_'), "$url/", null, "$url/$pathSuffix"), + // check if line suffix work + arrayOf("../core/../core/./$testdata/$sourceLinks/".padEnd(maxLength, '_'), "$url/", "#L", "$url/$dummy#L4") + ) + } + } + +} + -- cgit From 7d7e2012e35722c6c0315b2205d708ef5d4341d9 Mon Sep 17 00:00:00 2001 From: KrystianUjma Date: Wed, 27 Feb 2019 17:41:04 +0100 Subject: Fix test, prohibit non-unix path style for dir property Update readme.md, allow only unix based path (dir property) --- README.md | 4 ++-- core/src/test/kotlin/model/SourceLinksTest.kt | 8 +++----- runners/ant/src/main/kotlin/ant/dokka.kt | 4 ++++ runners/gradle-plugin/src/main/kotlin/main.kt | 4 +++- runners/maven-plugin/src/main/kotlin/DokkaMojo.kt | 7 +++++++ 5 files changed, 19 insertions(+), 8 deletions(-) (limited to 'core/src/test/kotlin/model/SourceLinksTest.kt') diff --git a/README.md b/README.md index f0cca532..92ea9e2a 100644 --- a/README.md +++ b/README.md @@ -100,7 +100,7 @@ dokka { // If provided, Dokka generates "source" links for each declaration. // Repeat for multiple mappings linkMapping { - // Directory relative to the root of the project (where you execute gradle respectively). + // Unix based directory relative path to the root of the project (where you execute gradle respectively). dir = "src/main/kotlin" // or simply "./" // URL showing where the source code can be accessed through the web browser @@ -297,7 +297,7 @@ The available configuration options are shown below: ${project.basedir}/src/main/kotlin - https://github.com/me/myrepo + https://github.com/cy6erGn0m/vertx3-lang-kotlin/blob/master/src/main/kotlin #L diff --git a/core/src/test/kotlin/model/SourceLinksTest.kt b/core/src/test/kotlin/model/SourceLinksTest.kt index 1a74506f..0e9b666c 100644 --- a/core/src/test/kotlin/model/SourceLinksTest.kt +++ b/core/src/test/kotlin/model/SourceLinksTest.kt @@ -40,7 +40,7 @@ class SourceLinksTest( private const val sourceLinks = "sourceLinks" private const val dummy = "dummy.kt" private const val pathSuffix = "$sourceLinks/$dummy" - private const val filePath = "$testdata/$pathSuffix/../dummy.kt" + private const val filePath = "$testdata/$pathSuffix" private const val url = "https://example.com" @Parameterized.Parameters(name = "{index}: {0}, {1}, {2} = {3}") @@ -59,10 +59,8 @@ class SourceLinksTest( arrayOf("$testdata/$sourceLinks", "$url/$dummy"), arrayOf("./$testdata/../$testdata/$sourceLinks", "$url/$dummy") ) - val allPaths = list + - // we want to be sure Windows paths work as well - list.map { arrayOf(it[0].replace('/', '\\'), it[1]) } - return allPaths.map { arrayOf(it[0].padEnd(maxLength, '_'), url, null, it[1]) } + + + return list.map { arrayOf(it[0].padEnd(maxLength, '_'), url, null, it[1]) } + listOf( // check that it also works if url ends with / arrayOf((File(testdata).absolutePath.removeSuffix("/") + "/").padEnd(maxLength, '_'), "$url/", null, "$url/$pathSuffix"), diff --git a/runners/ant/src/main/kotlin/ant/dokka.kt b/runners/ant/src/main/kotlin/ant/dokka.kt index 79583a1f..b23328e0 100644 --- a/runners/ant/src/main/kotlin/ant/dokka.kt +++ b/runners/ant/src/main/kotlin/ant/dokka.kt @@ -111,6 +111,10 @@ class DokkaAntTask: Task() { } val sourceLinks = antSourceLinks.map { val path = it.path ?: throw BuildException("'path' attribute of a element is required") + if (path.contains("\\")) { + throw BuildException("'dir' attribute of a - incorrect value, only Unix based path allowed.") + } + val url = it.url ?: throw BuildException("'url' attribute of a element is required") SourceLinkDefinitionImpl(File(path).canonicalFile.absolutePath, url, it.lineSuffix) } diff --git a/runners/gradle-plugin/src/main/kotlin/main.kt b/runners/gradle-plugin/src/main/kotlin/main.kt index c0b74695..f4adc1c3 100644 --- a/runners/gradle-plugin/src/main/kotlin/main.kt +++ b/runners/gradle-plugin/src/main/kotlin/main.kt @@ -448,7 +448,9 @@ open class LinkMapping : Serializable, DokkaConfiguration.SourceLinkDefinition { var dir: String get() = path set(value) { - path = value + if (value.contains("\\")) + throw java.lang.IllegalArgumentException("Incorrect dir property, only Unix based path allowed.") + else path = value } override var path: String = "" diff --git a/runners/maven-plugin/src/main/kotlin/DokkaMojo.kt b/runners/maven-plugin/src/main/kotlin/DokkaMojo.kt index 41521388..324703a0 100644 --- a/runners/maven-plugin/src/main/kotlin/DokkaMojo.kt +++ b/runners/maven-plugin/src/main/kotlin/DokkaMojo.kt @@ -4,6 +4,7 @@ import org.apache.maven.archiver.MavenArchiveConfiguration import org.apache.maven.archiver.MavenArchiver import org.apache.maven.execution.MavenSession import org.apache.maven.plugin.AbstractMojo +import org.apache.maven.plugin.MojoExecutionException import org.apache.maven.plugins.annotations.* import org.apache.maven.project.MavenProject import org.apache.maven.project.MavenProjectHelper @@ -125,6 +126,12 @@ abstract class AbstractDokkaMojo : AbstractMojo() { return } + sourceLinks.forEach { + if (it.dir.contains("\\")) { + throw MojoExecutionException("Incorrect dir property, only Unix based path allowed.") + } + } + val gen = DokkaGenerator( MavenDokkaLogger(log), classpath, -- cgit