Skip to content

Commit

Permalink
Merge pull request #529 from jenkinsci/local-checkout-dir-is-exclusive
Browse files Browse the repository at this point in the history
Testing local or embedded plugins is exclusive
  • Loading branch information
jtnord committed Apr 19, 2023
2 parents 76d3c83 + 7837515 commit 57c72c3
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 21 deletions.
31 changes: 15 additions & 16 deletions src/main/java/org/jenkins/tools/test/PluginCompatTester.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@
import java.util.List;
import java.util.Map;
import java.util.NavigableMap;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.io.FileUtils;
import org.jenkins.tools.test.exception.MetadataExtractionException;
import org.jenkins.tools.test.exception.PluginCompatibilityTesterException;
import org.jenkins.tools.test.exception.PluginSourcesUnavailableException;
import org.jenkins.tools.test.maven.ExpressionEvaluator;
Expand Down Expand Up @@ -91,28 +91,27 @@ public void testPlugins() throws PluginCompatibilityTesterException {
WarExtractor warExtractor = new WarExtractor(
config.getWar(), serviceHelper, config.getIncludePlugins(), config.getExcludePlugins());
String coreVersion = warExtractor.extractCoreVersion();
List<Plugin> plugins = warExtractor.extractPlugins();
NavigableMap<String, List<Plugin>> pluginsByRepository = WarExtractor.byRepository(plugins);

/*
* Run the before checkout hooks on everything that we are about to check out (as opposed to an existing local
* checkout).
*/
for (Plugin plugin : plugins) {
BeforeCheckoutContext c = new BeforeCheckoutContext(coreVersion, plugin, config);
pcth.runBeforeCheckout(c);
}
NavigableMap<String, List<Plugin>> pluginsByRepository;

if (localCheckoutProvided()) {
// if a user provides a local checkout we do not also check anything in the way.
LocalCheckoutPluginMetadataExtractor localCheckoutPluginMetadataExtractor =
new LocalCheckoutPluginMetadataExtractor(config, runner);
// Do not perform the before checkout hooks on a local checkout
List<Plugin> localCheckout = localCheckoutPluginMetadataExtractor.extractMetadata();
pluginsByRepository.put(LOCAL_CHECKOUT, localCheckout);
}

if (pluginsByRepository.keySet().isEmpty()) {
throw new MetadataExtractionException("List of plugins to check is empty");
pluginsByRepository = new TreeMap<>(Map.of(LOCAL_CHECKOUT, localCheckout));
} else {
List<Plugin> plugins = warExtractor.extractPlugins();
pluginsByRepository = WarExtractor.byRepository(plugins);
/*
* Run the before checkout hooks on everything that we are about to check out (as opposed to an existing local
* checkout).
*/
for (Plugin plugin : plugins) {
BeforeCheckoutContext c = new BeforeCheckoutContext(coreVersion, plugin, config);
pcth.runBeforeCheckout(c);
}
}

PluginCompatibilityTesterException lastException = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,9 @@ public class PluginCompatTesterCli implements Callable<Integer> {
@CheckForNull
@CommandLine.Option(
names = "--local-checkout-dir",
description =
"Folder containing either a local (possibly modified) clone of a plugin repository or a set of local clones of different plugins.",
description = "Folder containing either a local (possibly modified) clone of a single plugin repository,"
+ " or a set of local clones of different plugins with a local aggregator project in the root. "
+ "If specified then any plugins bundled in the war will not be tested.",
converter = ExistingFileTypeConverter.class)
private File localCheckoutDir;

Expand Down
3 changes: 0 additions & 3 deletions src/main/java/org/jenkins/tools/test/PluginListerCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,6 @@ public Integer call() throws MetadataExtractionException {
ServiceHelper serviceHelper = new ServiceHelper(externalHooksJars);
WarExtractor warExtractor = new WarExtractor(warFile, serviceHelper, includePlugins, excludePlugins);
List<Plugin> plugins = warExtractor.extractPlugins();
if (plugins.isEmpty()) {
throw new MetadataExtractionException("Found no plugins in " + warFile);
}

if (output != null) {
NavigableMap<String, List<Plugin>> pluginsByRepository = WarExtractor.byRepository(plugins);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/jenkins/tools/test/util/WarExtractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public String extractCoreVersion() throws MetadataExtractionException {
* Extract the list of plugins to be tested from the given WAR.
*
* @return An unmodifiable list of plugins to be tested, sorted by plugin ID.
* @throws MetadataExtractionException if a non-I/O related issue occurs when the list of plugins is extracted or, if after applying filters, no plugins are located.
*/
public List<Plugin> extractPlugins() throws MetadataExtractionException {
List<Plugin> plugins = new ArrayList<>();
Expand All @@ -88,6 +89,9 @@ public List<Plugin> extractPlugins() throws MetadataExtractionException {
} catch (IOException e) {
throw new UncheckedIOException("I/O error occurred whilst extracting plugin metadata from WAR", e);
}
if (plugins.isEmpty()) {
throw new MetadataExtractionException("Found no plugins in " + warFile);
}
plugins.sort(Comparator.comparing(Plugin::getPluginId));
return List.copyOf(plugins);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.io.File;
import java.util.List;
import java.util.Set;
import org.jenkins.tools.test.exception.MetadataExtractionException;
import org.jenkins.tools.test.model.plugin_metadata.Plugin;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -41,4 +43,11 @@ void testExtractPlugins() throws Exception {
hasProperty("name", is("Text Finder")),
hasProperty("version", startsWith("1."))));
}

@Test
void testExtractPluginsWithNoMatches() throws Exception {
WarExtractor warExtractor = new WarExtractor(
new File("target", "megawar.war"), new ServiceHelper(Set.of()), Set.of("bogus-plugin-id"), Set.of());
assertThrows(MetadataExtractionException.class, () -> warExtractor.extractPlugins());
}
}

0 comments on commit 57c72c3

Please sign in to comment.