Skip to content

Commit

Permalink
Migrate existing Junit 4 usages to Junit 5 (#998)
Browse files Browse the repository at this point in the history
  • Loading branch information
Goooler authored Oct 30, 2024
1 parent 2c30155 commit e9aa551
Show file tree
Hide file tree
Showing 33 changed files with 212 additions and 596 deletions.
10 changes: 5 additions & 5 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ dependencies {

testImplementation("org.spockframework:spock-core:2.3-groovy-3.0") {
exclude(group = "org.codehaus.groovy")
exclude(group = "org.hamcrest")
}
testImplementation("org.spockframework:spock-junit4:2.3-groovy-3.0")
testImplementation("xmlunit:xmlunit:1.6")
testImplementation("org.xmlunit:xmlunit-legacy:2.10.0")
testImplementation("org.apache.commons:commons-lang3:3.17.0")
testImplementation("com.google.guava:guava:33.3.1-jre")
testImplementation("org.junit.jupiter:junit-jupiter-api:5.11.3")
testRuntimeOnly("org.junit.vintage:junit-vintage-engine:5.11.3")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
testImplementation(platform("org.junit:junit-bom:5.11.3"))
testImplementation("org.junit.jupiter:junit-jupiter")
testImplementation("org.junit.platform:junit-platform-suite-engine")
}

val isCI = providers.environmentVariable("CI").isPresent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import com.github.jengelman.gradle.plugins.shadow.util.PluginSpecification
import org.apache.commons.io.FileUtils
import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.TaskOutcome
import org.junit.Rule
import org.junit.rules.TemporaryFolder
import spock.lang.TempDir

import java.nio.file.Path

import static org.gradle.testkit.runner.TaskOutcome.FROM_CACHE
import static org.gradle.testkit.runner.TaskOutcome.SUCCESS

abstract class AbstractCachingSpec extends PluginSpecification {
@Rule TemporaryFolder alternateDir
@TempDir Path alternateDir

@Override
def setup() {
Expand Down Expand Up @@ -41,7 +42,7 @@ abstract class AbstractCachingSpec extends PluginSpecification {
List<String> cacheArguments = [ '--build-cache' ]
cacheArguments.addAll(arguments)
// TODO: Use PluginSpecification.run here to reuse flags, but cache tests failed for now, need to investigate.
return runner.withProjectDir(alternateDir.root).withArguments(cacheArguments).build()
return runner.withProjectDir(alternateDir.toFile()).withArguments(cacheArguments).build()
}

private String escapedPath(File file) {
Expand All @@ -59,9 +60,9 @@ abstract class AbstractCachingSpec extends PluginSpecification {
}

void copyToAlternateDir() {
FileUtils.deleteDirectory(alternateDir.root)
FileUtils.forceMkdir(alternateDir.root)
FileUtils.copyDirectory(dir.root, alternateDir.root)
FileUtils.deleteDirectory(alternateDir.toFile())
FileUtils.forceMkdir(alternateDir.toFile())
FileUtils.copyDirectory(dir.toFile(), alternateDir.toFile())
}

void assertShadowJarIsCachedAndRelocatable() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package com.github.jengelman.gradle.plugins.shadow.caching

import org.gradle.testkit.runner.BuildResult

import static org.gradle.testkit.runner.TaskOutcome.*

class MinimizationCachingSpec extends AbstractCachingSpec {
File output
String shadowJarTask = ":server:shadowJar"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,36 @@ import com.github.jengelman.gradle.plugins.shadow.docs.executer.GradleBuildExecu
import com.github.jengelman.gradle.plugins.shadow.docs.executer.NoopExecuter
import com.github.jengelman.gradle.plugins.shadow.docs.extractor.ManualSnippetExtractor
import com.github.jengelman.gradle.plugins.shadow.docs.fixture.GroovyDslFixture
import com.github.jengelman.gradle.plugins.shadow.docs.internal.snippets.CodeSnippetTestCase
import com.github.jengelman.gradle.plugins.shadow.docs.internal.snippets.CodeSnippetTests
import com.github.jengelman.gradle.plugins.shadow.docs.internal.snippets.TestCodeSnippet
import com.github.jengelman.gradle.plugins.shadow.docs.internal.snippets.executer.SnippetExecuter
import com.github.jengelman.gradle.plugins.shadow.docs.internal.snippets.fixture.GroovyScriptFixture
import com.google.common.base.StandardSystemProperty
import org.junit.jupiter.api.DynamicTest
import org.junit.jupiter.api.TestFactory
import org.junit.jupiter.api.io.TempDir

class ManualCodeSnippetTests extends CodeSnippetTestCase {
import java.nio.file.Path

class ManualCodeSnippetTests {
public static final LinkedHashMap<String, SnippetExecuter> FIXTURES = [
"groovy": new GradleBuildExecuter("build.gradle", new GroovyDslFixture(), new GroovyDslFixture.ImportsExtractor()),
"groovy no-plugins": new GradleBuildExecuter("build.gradle", new GroovyScriptFixture(), new GroovyDslFixture.ImportsExtractor()),
"groovy no-run": new NoopExecuter()
]

@Override
protected void addTests(CodeSnippetTests tests) {
@TestFactory
List<DynamicTest> provideDynamicTests(@TempDir Path tempDir) {
File cwd = new File(StandardSystemProperty.USER_DIR.value())

def content = new File(cwd, "src/docs")
List<TestCodeSnippet> snippets = []

FIXTURES.each { selector, executer ->
ManualSnippetExtractor.extract(content, selector, executer).each {
tests.add(it)
ManualSnippetExtractor.extract(tempDir, content, selector, executer).each {
snippets.add(it)
}
}
return snippets.collect {
DynamicTest.dynamicTest(it.testName, it)
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import com.github.jengelman.gradle.plugins.shadow.docs.internal.snippets.execute
import com.github.jengelman.gradle.plugins.shadow.docs.internal.snippets.fixture.SnippetFixture
import com.github.jengelman.gradle.plugins.shadow.util.PluginSpecification
import org.gradle.testkit.runner.GradleRunner
import org.junit.rules.TemporaryFolder

import java.util.function.Function

Expand Down Expand Up @@ -34,19 +33,15 @@ class GradleBuildExecuter implements SnippetExecuter {
}

@Override
void execute(TestCodeSnippet snippet) throws Exception {
TemporaryFolder tempDir = new TemporaryFolder()
tempDir.create()
File dir = tempDir.newFolder()

addSubProject(dir)
File settings = new File(dir, "settings.gradle")
void execute(File tempDir, TestCodeSnippet snippet) throws Exception {
addSubProject(tempDir)
File settings = new File(tempDir, "settings.gradle")
settings.text = """
rootProject.name = 'shadowTest'
include 'api', 'main'
"""

File mainDir = new File(dir, "main")
File mainDir = new File(tempDir, "main")
mainDir.mkdirs()
File buildFile = new File(mainDir, buildFile)

Expand All @@ -59,7 +54,7 @@ include 'api', 'main'

buildFile.text = replaceTokens(fullSnippet)

GradleRunner runner = GradleRunner.create().withProjectDir(dir).withPluginClasspath().forwardOutput()
GradleRunner runner = GradleRunner.create().withProjectDir(tempDir).withPluginClasspath().forwardOutput()

runner.withArguments(":main:build", "-m").build()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class NoopExecuter implements SnippetExecuter {
}

@Override
void execute(TestCodeSnippet snippet) throws Exception {

void execute(File tempDir, TestCodeSnippet snippet) throws Exception {
// noop
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,32 @@ import com.github.jengelman.gradle.plugins.shadow.docs.internal.snippets.execute
import com.github.jengelman.gradle.plugins.shadow.docs.internal.snippets.executer.SnippetExecuter
import groovy.ant.FileNameFinder

import java.nio.file.Path
import java.util.regex.Pattern

class ManualSnippetExtractor {

static List<TestCodeSnippet> extract(File root, String cssClass, SnippetExecuter executer) {
static List<TestCodeSnippet> extract(Path tempDir, File root, String cssClass, SnippetExecuter executer) {
List<TestCodeSnippet> snippets = []

def snippetBlockPattern = Pattern.compile(/(?ims)```$cssClass\n(.*?)\n```/)
def filenames = new FileNameFinder().getFileNames(root.absolutePath, "**/*.md")

filenames.each { filename ->
def file = new File(filename)
addSnippets(snippets, file, snippetBlockPattern, executer)
addSnippets(tempDir, snippets, file, snippetBlockPattern, executer)
}

snippets
}

private static void addSnippets(List<TestCodeSnippet> snippets, File file, Pattern snippetBlockPattern, SnippetExecuter executer) {
private static void addSnippets(Path tempDir, List<TestCodeSnippet> snippets, File file, Pattern snippetBlockPattern, SnippetExecuter executer) {
def source = file.text
String testName = file.parentFile.name + "/" +file.name
Map<Integer, String> snippetsByLine = findSnippetsByLine(source, snippetBlockPattern)

snippetsByLine.each { lineNumber, snippet ->
snippets << createSnippet(testName, file, lineNumber, snippet, executer)
snippets << createSnippet(tempDir, testName, file, lineNumber, snippet, executer)
}
}

Expand Down Expand Up @@ -60,8 +61,8 @@ class ManualSnippetExtractor {
tag.substring(tag.indexOf("\n") + 1, tag.lastIndexOf("\n"))
}

private static TestCodeSnippet createSnippet(String sourceClassName, File sourceFile, int lineNumber, String snippet, SnippetExecuter executer) {
new TestCodeSnippet(snippet, sourceClassName, sourceClassName + ":$lineNumber", executer, new ExceptionTransformer(sourceClassName, sourceFile.name, lineNumber))
private static TestCodeSnippet createSnippet(Path tempDir, String sourceClassName, File sourceFile, int lineNumber, String snippet, SnippetExecuter executer) {
new TestCodeSnippet(tempDir, snippet, sourceClassName + ":$lineNumber", executer, new ExceptionTransformer(sourceClassName, sourceFile.name, lineNumber))
}

}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,39 @@

import com.github.jengelman.gradle.plugins.shadow.docs.internal.snippets.executer.ExceptionTransformer;
import com.github.jengelman.gradle.plugins.shadow.docs.internal.snippets.executer.SnippetExecuter;

public class TestCodeSnippet {

private final String snippet;
private final String className;
private final String testName;
private final SnippetExecuter executer;
private final ExceptionTransformer exceptionTransformer;

public TestCodeSnippet(String snippet, String className, String testName, SnippetExecuter executer, ExceptionTransformer exceptionTransformer) {
this.snippet = snippet;
this.className = className;
this.testName = testName;
this.executer = executer;
this.exceptionTransformer = exceptionTransformer;
}

public String getSnippet() {
return snippet;
}

public String getClassName() {
return className;
}

public String getTestName() {
return testName;
}

public ExceptionTransformer getExceptionTransformer() {
return exceptionTransformer;
}

public SnippetExecuter getExecuter() {
return executer;
}
import org.junit.jupiter.api.function.Executable;

import java.nio.file.Path;

public class TestCodeSnippet implements Executable {

private final Path tempDir;
private final String snippet;
private final String testName;
private final SnippetExecuter executer;
private final ExceptionTransformer exceptionTransformer;

public TestCodeSnippet(Path tempDir, String snippet, String testName, SnippetExecuter executer, ExceptionTransformer exceptionTransformer) {
this.tempDir = tempDir;
this.snippet = snippet;
this.testName = testName;
this.executer = executer;
this.exceptionTransformer = exceptionTransformer;
}

public String getSnippet() {
return snippet;
}

public String getTestName() {
return testName;
}

public void execute() throws Throwable {
try {
executer.execute(tempDir.toFile(), this);
} catch (Throwable t) {
throw exceptionTransformer.transform(t, executer.getFixture().getOffset());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import com.github.jengelman.gradle.plugins.shadow.docs.internal.snippets.fixture.SnippetFixture;
import com.github.jengelman.gradle.plugins.shadow.docs.internal.snippets.TestCodeSnippet;

import java.io.File;

public interface SnippetExecuter {

SnippetFixture getFixture();

void execute(TestCodeSnippet snippet) throws Exception;
void execute(File tempDir, TestCodeSnippet snippet) throws Exception;

}
Loading

0 comments on commit e9aa551

Please sign in to comment.