-
-
Notifications
You must be signed in to change notification settings - Fork 425
Migrate from TestFactory to ParameterizedTest #2188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a9acf2c
Use @ParameterizedTest in DocCodeSnippetTest
Goooler ff60a5e
Clean up tempDir and Executable from SnippetExecutable
Goooler 057887e
Refine documentation test arguments
Goooler 830c2da
Revert Arguments
Goooler e8d9a26
Simplify snippet failure locations
Goooler a7dde9a
Use interface for snippet executables
Goooler 42cde7d
Use top-level snippet extraction helpers
Goooler 1470762
Inline executeSnippet
Goooler abe4c40
Clean up snippet execution
Goooler bc6c5d0
Simplify code snippet extraction
Goooler 876a4aa
Share snippet plugins block
Goooler 9b60042
Prefer AssertionError over RuntimeException
Goooler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
44 changes: 24 additions & 20 deletions
44
src/documentTest/kotlin/com/github/jengelman/gradle/plugins/shadow/DocCodeSnippetTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,33 +1,37 @@ | ||
| package com.github.jengelman.gradle.plugins.shadow | ||
|
|
||
| import com.github.jengelman.gradle.plugins.shadow.snippet.CodeSnippetExtractor | ||
| import com.github.jengelman.gradle.plugins.shadow.snippet.DslLang | ||
| import com.github.jengelman.gradle.plugins.shadow.snippet.SnippetExecutable | ||
| import com.github.jengelman.gradle.plugins.shadow.snippet.extractCodeSnippets | ||
| import java.nio.file.Path | ||
| import kotlin.io.path.createDirectory | ||
| import org.junit.jupiter.api.DynamicTest | ||
| import org.junit.jupiter.api.TestFactory | ||
| import org.junit.jupiter.api.Named.named | ||
| import org.junit.jupiter.api.io.TempDir | ||
| import org.junit.jupiter.params.ParameterizedTest | ||
| import org.junit.jupiter.params.provider.Arguments | ||
| import org.junit.jupiter.params.provider.Arguments.arguments | ||
| import org.junit.jupiter.params.provider.MethodSource | ||
|
|
||
| class DocCodeSnippetTest { | ||
|
|
||
| @TestFactory | ||
| fun provideDynamicTests(@TempDir root: Path): List<DynamicTest> { | ||
| val langExecutables = DslLang.entries.map { executor -> CodeSnippetExtractor.extract(executor) } | ||
| @ParameterizedTest(name = "{0}") | ||
| @MethodSource("snippets") | ||
| fun test(executable: SnippetExecutable, @TempDir tempDir: Path) { | ||
| executable.execute(tempDir) | ||
| } | ||
|
|
||
| check(langExecutables.sumOf { it.size } > 0) { "No code snippets found." } | ||
| check(langExecutables.size == DslLang.entries.size) { | ||
| "We must provide build script snippets for all languages." | ||
| } | ||
| check(langExecutables.map { it.size }.distinct().size == 1) { | ||
| "All languages must have the same number of code snippets." | ||
| } | ||
| private companion object { | ||
| @JvmStatic | ||
| fun snippets(): List<Arguments> { | ||
| val langExecutables = DslLang.entries.map(DslLang::extractCodeSnippets) | ||
|
|
||
| return langExecutables.flatten().map { | ||
| val dirName = it.displayName.replace(nonAlphanumeric, "_") | ||
| it.tempDir = root.resolve(dirName).createDirectory() | ||
| DynamicTest.dynamicTest(it.displayName, it) | ||
| check(langExecutables.sumOf { it.size } > 0) { "No code snippets found." } | ||
| check(langExecutables.map { it.size }.distinct().size == 1) { | ||
| "All languages must have the same number of code snippets." | ||
| } | ||
|
|
||
| return langExecutables.flatten().map { executable -> | ||
| arguments(named(executable.displayName, executable)) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private val nonAlphanumeric = "[^a-zA-Z0-9]".toRegex() |
67 changes: 30 additions & 37 deletions
67
...entTest/kotlin/com/github/jengelman/gradle/plugins/shadow/snippet/CodeSnippetExtractor.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,52 +1,45 @@ | ||
| package com.github.jengelman.gradle.plugins.shadow.snippet | ||
|
|
||
| import com.github.jengelman.gradle.plugins.shadow.DocumentTestBuildConfig.DOCS_DIR | ||
| import java.nio.file.Path | ||
| import java.util.regex.Pattern | ||
| import kotlin.io.path.Path | ||
| import kotlin.io.path.name | ||
| import kotlin.io.path.readText | ||
| import kotlin.io.path.relativeTo | ||
| import kotlin.io.path.walk | ||
|
|
||
| object CodeSnippetExtractor { | ||
| private val docRoot = Path(DOCS_DIR) | ||
|
|
||
| private val markdownPaths = | ||
| docRoot.walk().filter { it.name.endsWith(".md", ignoreCase = true) }.toList() | ||
|
|
||
| fun extract(lang: DslLang): List<SnippetExecutable> { | ||
| return markdownPaths.flatMap { path -> createExecutables(lang, path) } | ||
| } | ||
|
|
||
| private fun createExecutables(lang: DslLang, markdownPath: Path): List<SnippetExecutable> { | ||
| val relativeDocPath = markdownPath.relativeTo(docRoot).toString() | ||
| return createSnippets(markdownPath.readText(), lang).map { (lineNumber, snippet) -> | ||
| SnippetExecutable.create(lang, snippet, "$relativeDocPath:$lineNumber") { cause -> | ||
| RuntimeException( | ||
| "The error line in the doc is near ${markdownPath.toUri()}:$lineNumber\n\n${cause.message}", | ||
| cause, | ||
| ) | ||
| private val docRoot = Path(DOCS_DIR) | ||
|
|
||
| fun DslLang.extractCodeSnippets(): List<SnippetExecutable> { | ||
| val lang = this | ||
| return docRoot | ||
| .walk() | ||
| .filter { it.name.endsWith(".md", ignoreCase = true) } | ||
| .flatMap { path -> | ||
| val source = path.readText() | ||
| val matcher = Pattern.compile("(?ims) {4}```${lang}\n(.*?)\n {4}```").matcher(source) | ||
|
|
||
| buildList { | ||
| while (matcher.find()) { | ||
| val lineNumber = source.lineNumberAt(matcher.start()) | ||
| add( | ||
| SnippetExecutable.create( | ||
| lang = lang, | ||
| snippet = matcher.group(1), | ||
| testName = "${path.relativeTo(docRoot)}:$lineNumber", | ||
| sourceLocation = "${path.toUri()}:$lineNumber", | ||
| ) | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun createSnippets(source: String, lang: DslLang) = buildMap { | ||
| val pattern = Pattern.compile("(?ims) {4}```${lang}\n(.*?)\n {4}```") | ||
| val matcher = pattern.matcher(source) | ||
|
|
||
| while (matcher.find()) { | ||
| val line = source.lineNumberAt(matcher.start()) | ||
| val code = matcher.group(1) | ||
| put(line, code) | ||
| } | ||
| } | ||
| .toList() | ||
| } | ||
|
|
||
| private fun String.lineNumberAt(index: Int): Int { | ||
| var line = 1 | ||
| for (i in 0 until index.coerceAtMost(length)) { | ||
| if (this[i] == '\n') line++ | ||
| } | ||
| return line | ||
| private fun String.lineNumberAt(index: Int): Int { | ||
| var line = 1 | ||
| for (i in 0 until index.coerceAtMost(length)) { | ||
| if (this[i] == '\n') line++ | ||
| } | ||
| return line | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.