Skip to content

Commit

Permalink
Added tests for StandardFilesMergeTransformer
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Diederich committed Sep 18, 2024
1 parent e331631 commit 95a4326
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.github.jengelman.gradle.plugins.shadow.relocation.Relocator
import com.github.jengelman.gradle.plugins.shadow.transformers.StandardFilesMergeTransformer
import com.github.jengelman.gradle.plugins.shadow.transformers.Transformer
import com.github.jengelman.gradle.plugins.shadow.transformers.TransformerContext
import groovy.util.logging.Slf4j
import org.apache.commons.io.FilenameUtils
import org.apache.commons.io.IOUtils
import org.apache.tools.zip.UnixStat
Expand Down Expand Up @@ -39,17 +40,14 @@ import org.objectweb.asm.ClassReader
import org.objectweb.asm.ClassVisitor
import org.objectweb.asm.ClassWriter
import org.objectweb.asm.commons.ClassRemapper
import org.slf4j.Logger
import org.slf4j.LoggerFactory

import javax.annotation.Nullable
import java.util.zip.ZipException

@Slf4j
class ShadowCopyAction implements CopyAction {
static final long CONSTANT_TIME_FOR_ZIP_ENTRIES = (new GregorianCalendar(1980, 1, 1, 0, 0, 0)).getTimeInMillis()

final static Logger log = LoggerFactory.getLogger(ShadowCopyAction.class);

private final File zipFile
private final ZipCompressor compressor
private final DocumentationRegistry documentationRegistry
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
package com.github.jengelman.gradle.plugins.shadow.tasks;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;

import javax.annotation.Nonnull;

import com.github.jengelman.gradle.plugins.shadow.ShadowStats;
import com.github.jengelman.gradle.plugins.shadow.internal.DefaultDependencyFilter;
import com.github.jengelman.gradle.plugins.shadow.internal.DependencyFilter;
Expand All @@ -27,14 +18,12 @@
import com.github.jengelman.gradle.plugins.shadow.transformers.StandardFilesMergeTransformer;
import com.github.jengelman.gradle.plugins.shadow.transformers.Transformer;
import org.gradle.api.Action;
import org.gradle.api.Task;
import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.file.DuplicatesStrategy;
import org.gradle.api.file.FileCollection;
import org.gradle.api.internal.DocumentationRegistry;
import org.gradle.api.internal.file.FileResolver;
import org.gradle.api.internal.file.copy.CopyAction;
import org.gradle.api.specs.Spec;
import org.gradle.api.tasks.CacheableTask;
import org.gradle.api.tasks.Classpath;
import org.gradle.api.tasks.Input;
Expand All @@ -47,9 +36,16 @@
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.SourceSetContainer;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.*;
import org.gradle.api.tasks.bundling.Jar;
import org.gradle.api.tasks.util.PatternSet;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;

@CacheableTask
public class ShadowJar extends Jar implements ShadowSpec {
Expand Down Expand Up @@ -139,7 +135,7 @@ public InheritManifest getManifest() {
}

@Override
@Nonnull
@NotNull
protected CopyAction createCopyAction() {
DocumentationRegistry documentationRegistry = getServices().get(DocumentationRegistry.class);
final UnusedTracker unusedTracker = minimizeJar ? UnusedTracker.forProject(getApiJars(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,38 @@ class ShadowPluginSpec extends PluginSpecification {
BuildResult result = run('shadow')
then:
assert result.output =~ /\s*IGNORING Weird-File\.StrangeFormat from test-project-1\.0-SNAPSHOT\.jar, size is different \([0-9]{4} vs [0-9]{2}\)\s+--> origin JAR was Weird-File.StrangeFormat/
assert result.output =~ /\s*IGNORING Weird-File\.StrangeFormat from test-project-1\.0-SNAPSHOT\.jar,/
/ size is different \([0-9]{4} vs [0-9]{2}\)\s+--> origin JAR was Weird-File.StrangeFormat/
/* Shouldn't appear, because the default StandardFileTransformer should've merged it,
instead of just dropping all following licenses. */
assert !result.output.contains('license.txt')
}
def 'Tests the removal of the default transformer'() {
given:
URL artifact = this.class.classLoader.getResource('test-artifact-1.0-SNAPSHOT.jar')
URL project = this.class.classLoader.getResource('test-project-1.0-SNAPSHOT.jar')
buildFile << """
|task shadow(type: ${ShadowJar.name}) {
| destinationDirectory = buildDir
| archiveBaseName = 'shadow'
| removeDefaultTransformers()
| from('${artifact.path}')
| from('${project.path}')
|}
""".stripMargin()
when:
BuildResult result = run('shadow')
then:
assert result.output =~ /\s*IGNORING Weird-File\.StrangeFormat from test-project-1\.0-SNAPSHOT\.jar,/
/ size is different \([0-9]{4} vs [0-9]{2}\)\s+--> origin JAR was Weird-File.StrangeFormat/
/\s+IGNORING test\.json from test-project-1\.0-SNAPSHOT.jar, size is different/
// Without the StandardFileTransformer there should be a warning about multiple license files with the same name.
assert result.output.contains('license.txt')
}
def 'include project sources'() {
Expand Down Expand Up @@ -214,7 +245,6 @@ class ShadowPluginSpec extends PluginSpecification {
repositories { maven { url "${repo.uri}" } }
dependencies { implementation project(':client') }
""".stripIndent()
File serverOutput = getFile('server/build/libs/server-all.jar')
Expand Down
Binary file modified src/test/resources/test-artifact-1.0-SNAPSHOT.jar
Binary file not shown.
Binary file modified src/test/resources/test-project-1.0-SNAPSHOT.jar
Binary file not shown.

0 comments on commit 95a4326

Please sign in to comment.