From 63839c92a1283bfa0b578f23d541f0f255c22cdb Mon Sep 17 00:00:00 2001 From: nitschSB <39341455+nitschSB@users.noreply.github.com> Date: Thu, 23 May 2024 15:16:04 +0200 Subject: [PATCH] remove shared variables #170 --- .../layerfilter/JibLayerFilterExtension.java | 24 +++++++++++-------- .../JibLayerFilterExtensionTest.java | 3 --- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/first-party/jib-layer-filter-extension-maven/src/main/java/com/google/cloud/tools/jib/maven/extension/layerfilter/JibLayerFilterExtension.java b/first-party/jib-layer-filter-extension-maven/src/main/java/com/google/cloud/tools/jib/maven/extension/layerfilter/JibLayerFilterExtension.java index 14fc7f0..c56224c 100644 --- a/first-party/jib-layer-filter-extension-maven/src/main/java/com/google/cloud/tools/jib/maven/extension/layerfilter/JibLayerFilterExtension.java +++ b/first-party/jib-layer-filter-extension-maven/src/main/java/com/google/cloud/tools/jib/maven/extension/layerfilter/JibLayerFilterExtension.java @@ -54,13 +54,8 @@ @Singleton public class JibLayerFilterExtension implements JibMavenPluginExtension { - private Map pathMatchers = new LinkedHashMap<>(); - @VisibleForTesting @Inject ProjectDependenciesResolver dependencyResolver; - // (layer name, layer builder) map for new layers of configured - @VisibleForTesting Map newToLayers = new LinkedHashMap<>(); - @Override public Optional> getExtraConfigType() { return Optional.of(Configuration.class); @@ -80,7 +75,12 @@ public ContainerBuildPlan extendContainerBuildPlan( return buildPlan; } - preparePathMatchersAndLayerBuilders(buildPlan, config.get()); + Map pathMatchers = new LinkedHashMap<>(); + + // (layer name, layer builder) map for new layers of configured + Map newToLayers = new LinkedHashMap<>(); + + preparePathMatchersAndLayerBuilders(buildPlan, config.get(), pathMatchers); ContainerBuildPlan.Builder newPlanBuilder = buildPlan.toBuilder(); newPlanBuilder.setLayers(Collections.emptyList()); @@ -92,7 +92,7 @@ public ContainerBuildPlan extendContainerBuildPlan( List filesToKeep = new ArrayList<>(); for (FileEntry entry : layer.getEntries()) { - Optional finalLayerName = determineFinalLayerName(entry, layer.getName()); + Optional finalLayerName = determineFinalLayerName(entry, layer.getName(), pathMatchers); // Either keep, move, or delete this FileEntry. if (finalLayerName.isPresent()) { if (finalLayerName.get().equals(layer.getName())) { @@ -235,11 +235,14 @@ private void logMissingParentDependencies( } private void preparePathMatchersAndLayerBuilders( - ContainerBuildPlan buildPlan, Configuration config) throws JibPluginExtensionException { + ContainerBuildPlan buildPlan, + Configuration config, + Map pathMatchers, + Map newToLayers) + throws JibPluginExtensionException { List originalLayerNames = buildPlan.getLayers().stream().map(LayerObject::getName).collect(Collectors.toList()); - newToLayers.clear(); // ensure empty (in case previously built module already populated it) for (Configuration.Filter filter : config.getFilters()) { String toLayerName = filter.getToLayer(); if (!toLayerName.isEmpty() && originalLayerNames.contains(toLayerName)) { @@ -271,7 +274,8 @@ private void preparePathMatchersAndLayerBuilders( * @return final layer name into which {@code fileEntry} should move. May be same as {@code * originalLayerName}. {@link Optional#empty()} indicates deletion. */ - private Optional determineFinalLayerName(FileEntry fileEntry, String originalLayerName) { + private Optional determineFinalLayerName( + FileEntry fileEntry, String originalLayerName, Map pathMatchers) { Optional finalLayerName = Optional.of(originalLayerName); for (Map.Entry mapEntry : pathMatchers.entrySet()) { diff --git a/first-party/jib-layer-filter-extension-maven/src/test/java/com/google/cloud/tools/jib/maven/extension/layerfilter/JibLayerFilterExtensionTest.java b/first-party/jib-layer-filter-extension-maven/src/test/java/com/google/cloud/tools/jib/maven/extension/layerfilter/JibLayerFilterExtensionTest.java index 4c16e88..0357e48 100644 --- a/first-party/jib-layer-filter-extension-maven/src/test/java/com/google/cloud/tools/jib/maven/extension/layerfilter/JibLayerFilterExtensionTest.java +++ b/first-party/jib-layer-filter-extension-maven/src/test/java/com/google/cloud/tools/jib/maven/extension/layerfilter/JibLayerFilterExtensionTest.java @@ -236,9 +236,6 @@ public void testExtendContainerBuildPlan_sameLayerNameInMultipleFilters() ContainerBuildPlan newPlan = extension.extendContainerBuildPlan(buildPlan, null, Optional.of(config), null, logger); - ArrayList layerNames = new ArrayList<>(extension.newToLayers.keySet()); - assertEquals(Arrays.asList("foo", "same layer name", "bar", "baz"), layerNames); - assertEquals(4, newPlan.getLayers().size()); FileEntriesLayer newLayer1 = (FileEntriesLayer) newPlan.getLayers().get(0); FileEntriesLayer newLayer2 = (FileEntriesLayer) newPlan.getLayers().get(1);