Add custom decompiler tests. Includes test utils for creating buildSrc plugins.
This commit is contained in:
		
							parent
							
								
									a712954be2
								
							
						
					
					
						commit
						e4330a11dc
					
				
					 6 changed files with 218 additions and 2 deletions
				
			
		|  | @ -28,7 +28,7 @@ import org.gradle.util.GradleVersion | |||
| 
 | ||||
| class LoomTestConstants { | ||||
|     public final static String DEFAULT_GRADLE = GradleVersion.current().getVersion() | ||||
|     public final static String PRE_RELEASE_GRADLE = "7.5-20220116010338+0000" | ||||
|     public final static String PRE_RELEASE_GRADLE = "7.5-20220122232041+0000" | ||||
| 
 | ||||
|     public final static String[] STANDARD_TEST_VERSIONS = [DEFAULT_GRADLE, PRE_RELEASE_GRADLE] | ||||
| } | ||||
|  |  | |||
|  | @ -50,4 +50,28 @@ class DecompileTest extends Specification implements GradleProjectTestTrait { | |||
| 			'cfr' 			| "genSourcesWithCfr"				| DEFAULT_GRADLE | ||||
| 			'cfr' 			| "genSourcesWithCfr"				| PRE_RELEASE_GRADLE | ||||
| 	} | ||||
| 
 | ||||
| 	@Unroll | ||||
| 	def "custom decompiler (gradle #version)"() { | ||||
| 		setup: | ||||
| 			def gradle = gradleProject(project: "minimalBase", version: version) | ||||
| 			gradle.buildSrc("decompile") | ||||
| 			gradle.buildGradle << ''' | ||||
|                 dependencies { | ||||
|                     minecraft "com.mojang:minecraft:1.18.1" | ||||
|                     mappings "net.fabricmc:yarn:1.18.1+build.18:v2" | ||||
|                 } | ||||
|             ''' | ||||
| 		when: | ||||
| 			def result = gradle.run(task: "genSourcesWithCustom") | ||||
| 
 | ||||
| 		then: | ||||
| 			result.task(":genSourcesWithCustom").outcome == SUCCESS | ||||
| 			result.task(":preDecompile").outcome == SUCCESS | ||||
| 			result.output.contains("Writing test file") | ||||
| 			result.output.contains("Running custom decompiler") | ||||
| 
 | ||||
| 		where: | ||||
| 			version << STANDARD_TEST_VERSIONS | ||||
| 	} | ||||
| } | ||||
|  |  | |||
|  | @ -0,0 +1,37 @@ | |||
| /* | ||||
|  * This file is part of fabric-loom, licensed under the MIT License (MIT). | ||||
|  * | ||||
|  * Copyright (c) 2022 FabricMC | ||||
|  * | ||||
|  * Permission is hereby granted, free of charge, to any person obtaining a copy | ||||
|  * of this software and associated documentation files (the "Software"), to deal | ||||
|  * in the Software without restriction, including without limitation the rights | ||||
|  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||
|  * copies of the Software, and to permit persons to whom the Software is | ||||
|  * furnished to do so, subject to the following conditions: | ||||
|  * | ||||
|  * The above copyright notice and this permission notice shall be included in all | ||||
|  * copies or substantial portions of the Software. | ||||
|  * | ||||
|  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||
|  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||
|  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||
|  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||
|  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||
|  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||||
|  * SOFTWARE. | ||||
|  */ | ||||
| 
 | ||||
| package net.fabricmc.loom.test.integration.buildSrc.decompile | ||||
| 
 | ||||
| import net.fabricmc.loom.api.decompilers.DecompilationMetadata | ||||
| import net.fabricmc.loom.api.decompilers.LoomDecompiler | ||||
| 
 | ||||
| import java.nio.file.Path | ||||
| 
 | ||||
| class CustomDecompiler implements LoomDecompiler { | ||||
|     @Override | ||||
|     void decompile(Path compiledJar, Path sourcesDestination, Path linemapDestination, DecompilationMetadata metaData) { | ||||
|         println("Running custom decompiler") | ||||
|     } | ||||
| } | ||||
|  | @ -0,0 +1,41 @@ | |||
| /* | ||||
|  * This file is part of fabric-loom, licensed under the MIT License (MIT). | ||||
|  * | ||||
|  * Copyright (c) 2022 FabricMC | ||||
|  * | ||||
|  * Permission is hereby granted, free of charge, to any person obtaining a copy | ||||
|  * of this software and associated documentation files (the "Software"), to deal | ||||
|  * in the Software without restriction, including without limitation the rights | ||||
|  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||
|  * copies of the Software, and to permit persons to whom the Software is | ||||
|  * furnished to do so, subject to the following conditions: | ||||
|  * | ||||
|  * The above copyright notice and this permission notice shall be included in all | ||||
|  * copies or substantial portions of the Software. | ||||
|  * | ||||
|  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||
|  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||
|  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||
|  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||
|  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||
|  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||||
|  * SOFTWARE. | ||||
|  */ | ||||
| 
 | ||||
| package net.fabricmc.loom.test.integration.buildSrc.decompile | ||||
| 
 | ||||
| import org.gradle.api.DefaultTask | ||||
| import org.gradle.api.file.RegularFileProperty | ||||
| import org.gradle.api.tasks.OutputFile | ||||
| import org.gradle.api.tasks.TaskAction | ||||
| 
 | ||||
| abstract class PreDecompileTask extends DefaultTask { | ||||
|     @OutputFile | ||||
|     abstract RegularFileProperty getOutputFile() | ||||
| 
 | ||||
|     @TaskAction | ||||
|     def run() { | ||||
|         println("Writing test file") | ||||
|         getOutputFile().asFile.get().text = "Test" | ||||
|     } | ||||
| } | ||||
|  | @ -0,0 +1,49 @@ | |||
| /* | ||||
|  * This file is part of fabric-loom, licensed under the MIT License (MIT). | ||||
|  * | ||||
|  * Copyright (c) 2022 FabricMC | ||||
|  * | ||||
|  * Permission is hereby granted, free of charge, to any person obtaining a copy | ||||
|  * of this software and associated documentation files (the "Software"), to deal | ||||
|  * in the Software without restriction, including without limitation the rights | ||||
|  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||
|  * copies of the Software, and to permit persons to whom the Software is | ||||
|  * furnished to do so, subject to the following conditions: | ||||
|  * | ||||
|  * The above copyright notice and this permission notice shall be included in all | ||||
|  * copies or substantial portions of the Software. | ||||
|  * | ||||
|  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||
|  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||
|  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||
|  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||
|  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||
|  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||||
|  * SOFTWARE. | ||||
|  */ | ||||
| 
 | ||||
| package net.fabricmc.loom.test.integration.buildSrc.decompile | ||||
| 
 | ||||
| import net.fabricmc.loom.LoomGradleExtension | ||||
| import org.gradle.api.Plugin | ||||
| import org.gradle.api.Project | ||||
| 
 | ||||
| class TestPlugin implements Plugin<Project> { | ||||
|     @Override | ||||
|     void apply(Project project) { | ||||
|         println("Test plugin") | ||||
|         def extension = LoomGradleExtension.get(project) | ||||
| 
 | ||||
|         def preDecompileTask = project.tasks.register("preDecompile", PreDecompileTask.class) { | ||||
|             outputFile = project.file("output.txt") | ||||
|         } | ||||
| 
 | ||||
|         extension.decompilerOptions.register("custom") { | ||||
|             decompilerClassName.set(CustomDecompiler.class.name) | ||||
| 
 | ||||
|             // BuiltBy shouldn't be required, but for some reason it is? Any ideas? | ||||
|             classpath.from(preDecompileTask) | ||||
|             classpath.builtBy(preDecompileTask) | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | @ -27,6 +27,7 @@ package net.fabricmc.loom.test.util | |||
| import groovy.transform.Immutable | ||||
| import net.fabricmc.loom.test.LoomTestConstants | ||||
| import net.fabricmc.loom.util.ZipUtils | ||||
| import org.apache.commons.io.FileUtils | ||||
| import org.gradle.testkit.runner.BuildResult | ||||
| import org.gradle.testkit.runner.GradleRunner | ||||
| import spock.lang.Shared | ||||
|  | @ -141,8 +142,8 @@ trait GradleProjectTestTrait { | |||
|         private String gradleVersion | ||||
|         private String projectDir | ||||
|         private String gradleHomeDir | ||||
| 
 | ||||
|         private String warningMode | ||||
|         private boolean useBuildSrc | ||||
| 
 | ||||
|         BuildResult run(Map options) { | ||||
|             // Setup the system props to tell loom that its running in a test env | ||||
|  | @ -167,6 +168,10 @@ trait GradleProjectTestTrait { | |||
| 
 | ||||
|             runner.withArguments(args as String[]) | ||||
| 
 | ||||
|             if (useBuildSrc) { | ||||
|                 writeBuildSrcDeps(runner) | ||||
|             } | ||||
| 
 | ||||
|             return options.expectFailure ? runner.buildAndFail() : runner.build() | ||||
|         } | ||||
| 
 | ||||
|  | @ -228,5 +233,65 @@ trait GradleProjectTestTrait { | |||
|         File getGeneratedSources(String mappings) { | ||||
|             return new File(getGradleHomeDir(), "caches/fabric-loom/${mappings}/minecraft-merged-named-sources.jar") | ||||
|         } | ||||
| 
 | ||||
|         void buildSrc(String name) { | ||||
|             useBuildSrc = true | ||||
| 
 | ||||
|             def buildSrcDir = new File(projectDir, "buildSrc") | ||||
|             buildSrcDir.mkdirs() | ||||
| 
 | ||||
|             def pluginClass = "net.fabricmc.loom.test.integration.buildSrc.${name}.TestPlugin" | ||||
|             new File(buildSrcDir, "build.gradle") << """ | ||||
|                 plugins { | ||||
|                     id 'groovy-gradle-plugin' | ||||
|                     id 'groovy' | ||||
|                 } | ||||
| 
 | ||||
|                 gradlePlugin { | ||||
|                     plugins { | ||||
|                         simplePlugin { | ||||
|                             id = 'loom-test-plugin' | ||||
|                             implementationClass = '${pluginClass}' | ||||
|                         } | ||||
|                     } | ||||
|                 } | ||||
|             """ | ||||
| 
 | ||||
|             new File(buildSrcDir, "settings.gradle") << ''' | ||||
|                 rootProject.name='loom-test-plugin' | ||||
|             ''' | ||||
| 
 | ||||
|             // Patch the new plugin into the end of the plugins block | ||||
|             def matcher = buildGradle.text =~ /(?s)plugins \{(?<ids>.*)}/ | ||||
|             assert matcher.find() | ||||
|             def ids = matcher.group("ids") | ||||
| 
 | ||||
|             def pluginBlock = """ | ||||
|                 plugins { | ||||
|                     ${ids} | ||||
|                     id 'loom-test-plugin' | ||||
|                 } | ||||
|             """ | ||||
| 
 | ||||
|             buildGradle.text = buildGradle.text.replaceAll("(?s)(plugins \\{.*})", pluginBlock) | ||||
| 
 | ||||
|             def sourceSrc = new File("src/test/groovy/net/fabricmc/loom/test/integration/buildSrc/" + name) | ||||
|             def targetSrc = new File(buildSrcDir, "src/main/groovy/net/fabricmc/loom/test/integration/buildSrc/" + name) | ||||
| 
 | ||||
|             FileUtils.copyDirectory(sourceSrc, targetSrc) | ||||
|         } | ||||
| 
 | ||||
|         void writeBuildSrcDeps(GradleRunner runner) { | ||||
|             def dependencies = "" | ||||
|             runner.pluginClasspath.forEach { File file -> | ||||
|                 dependencies += "implementation files('${file.absolutePath.replace("\\", "\\\\")}')\n" | ||||
|             } | ||||
| 
 | ||||
|             new File(projectDir, "buildSrc/build.gradle") << """ | ||||
|                 dependencies { | ||||
|                     ${dependencies} | ||||
|                 } | ||||
|             """ | ||||
|         } | ||||
|     } | ||||
| } | ||||
		Loading…
	
		Reference in a new issue