From f2fd617b921f7a9bee826a271213e17586e6949c Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Fri, 13 Feb 2015 12:17:47 +0100 Subject: tests use the same implementation of DocumentationModule building logic as production code --- src/Analysis/AnalysisEnvironment.kt | 4 +- src/main.kt | 77 ++++++++++++++++++++----------------- test/src/TestAPI.kt | 14 +------ 3 files changed, 46 insertions(+), 49 deletions(-) diff --git a/src/Analysis/AnalysisEnvironment.kt b/src/Analysis/AnalysisEnvironment.kt index 1bf3575f..279489e7 100644 --- a/src/Analysis/AnalysisEnvironment.kt +++ b/src/Analysis/AnalysisEnvironment.kt @@ -36,7 +36,9 @@ public class AnalysisEnvironment(val messageCollector: MessageCollector, body: A public fun withContext(processor: (JetCoreEnvironment, ResolveSession) -> T): T { val environment = JetCoreEnvironment.createForProduction(this, configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES) val resolveSession = environment.analyze() - resolveSession.forceResolveAll() + if (environment.getSourceFiles().isNotEmpty()) { + resolveSession.forceResolveAll() + } return processor(environment, resolveSession) } diff --git a/src/main.kt b/src/main.kt index 521adb61..1c480f22 100644 --- a/src/main.kt +++ b/src/main.kt @@ -6,7 +6,7 @@ import org.jetbrains.kotlin.cli.common.messages.* import org.jetbrains.kotlin.cli.common.arguments.* import org.jetbrains.kotlin.utils.PathUtil import java.io.File -import org.jetbrains.kotlin.name.FqName +import com.intellij.psi.PsiFile class DokkaArguments { Argument(value = "src", description = "Source file or directory (allows many paths separated by the system path separator)") @@ -121,7 +121,8 @@ class DokkaGenerator(val logger: DokkaLogger, logger.info("Analysing sources and libraries... ") val startAnalyse = System.currentTimeMillis() - val documentation = buildDocumentationModule(environment) + val options = DocumentationOptions(false, sourceLinks) + val documentation = buildDocumentationModule(environment, moduleName, options, includes, { isSample(it) }, logger) val timeAnalyse = System.currentTimeMillis() - startAnalyse logger.info("done in ${timeAnalyse / 1000} secs") @@ -170,42 +171,48 @@ class DokkaGenerator(val logger: DokkaLogger, return environment } - fun buildDocumentationModule(environment: AnalysisEnvironment): DocumentationModule { - val documentation = environment.withContext { environment, session -> - val fragmentFiles = environment.getSourceFiles().filter { - val sourceFile = File(it.getVirtualFile()!!.getPath()) - samples.none { sample -> - val canonicalSample = File(sample).canonicalPath - val canonicalSource = sourceFile.canonicalPath - canonicalSource.startsWith(canonicalSample) + fun isSample(file: PsiFile): Boolean { + val sourceFile = File(file.getVirtualFile()!!.getPath()) + return samples.none { sample -> + val canonicalSample = File(sample).canonicalPath + val canonicalSource = sourceFile.canonicalPath + canonicalSource.startsWith(canonicalSample) + } + } +} + +fun buildDocumentationModule(environment: AnalysisEnvironment, + moduleName: String, + options: DocumentationOptions, + includes: List = listOf(), + filesToDocumentFilter: (PsiFile) -> Boolean = { file -> true }, + logger: DokkaLogger): DocumentationModule { + val documentation = environment.withContext { environment, session -> + val fragmentFiles = environment.getSourceFiles().filter(filesToDocumentFilter) + val fragments = fragmentFiles.map { session.getPackageFragment(it.getPackageFqName()) }.filterNotNull().distinct() + val documentationBuilder = DocumentationBuilder(session, options, logger) + + with(documentationBuilder) { + + val moduleContent = Content() + for (include in includes) { + val file = File(include) + if (file.exists()) { + val text = file.readText() + val tree = parseMarkdown(text) + val content = buildContent(tree) + moduleContent.children.addAll(content.children) + } else { + logger.warn("Include file $file was not found.") } } - val fragments = fragmentFiles.map { session.getPackageFragment(it.getPackageFqName()) }.filterNotNull().distinct() - val options = DocumentationOptions(false, sourceLinks) - val documentationBuilder = DocumentationBuilder(session, options, logger) - - with(documentationBuilder) { - - val moduleContent = Content() - for (include in includes) { - val file = File(include) - if (file.exists()) { - val text = file.readText() - val tree = parseMarkdown(text) - val content = buildContent(tree) - moduleContent.children.addAll(content.children) - } else { - logger.warn("Include file $file was not found.") - } - } - val documentationModule = DocumentationModule(moduleName, moduleContent) - documentationModule.appendFragments(fragments) - documentationBuilder.resolveReferences(documentationModule) - documentationModule - } + val documentationModule = DocumentationModule(moduleName, moduleContent) + documentationModule.appendFragments(fragments) + documentationBuilder.resolveReferences(documentationModule) + documentationModule } - - return documentation } + + return documentation } diff --git a/test/src/TestAPI.kt b/test/src/TestAPI.kt index 7db42980..5ba685f6 100644 --- a/test/src/TestAPI.kt +++ b/test/src/TestAPI.kt @@ -31,20 +31,8 @@ public fun verifyModel(vararg files: String, verifier: (DocumentationModule) -> addClasspath(File(stringRoot)) addSources(files.toList()) } - val options = DocumentationOptions(includeNonPublic = true, sourceLinks = listOf()) - - val documentation = environment.withContext { environment, session -> - val fragments = environment.getSourceFiles().map { session.getPackageFragment(it.getPackageFqName()) }.filterNotNull().distinct() - - val documentationModule = DocumentationModule("test") - val documentationBuilder = DocumentationBuilder(session, options, DokkaConsoleLogger) - with(documentationBuilder) { - documentationModule.appendFragments(fragments) - } - documentationBuilder.resolveReferences(documentationModule) - documentationModule - } + val documentation = buildDocumentationModule(environment, "test", options, logger = DokkaConsoleLogger) verifier(documentation) Disposer.dispose(environment) } -- cgit