Skip to content

Commit

Permalink
Fix TU and avoid workspace scan if not necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
Benoit Guerin committed Oct 3, 2017
1 parent 41ff6e8 commit 5f2178b
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 76 deletions.
Expand Up @@ -23,6 +23,8 @@
*/
package org.jenkinsci.plugins.pipeline.maven.publishers;

import static org.jenkinsci.plugins.pipeline.maven.publishers.DependenciesLister.listDependencies;

import hudson.Extension;
import hudson.FilePath;
import hudson.model.FingerprintMap;
Expand Down Expand Up @@ -99,7 +101,7 @@ public void process(@Nonnull StepContext context, @Nonnull Element mavenSpyLogsE

FilePath workspace = context.get(FilePath.class);

List<MavenSpyLogProcessor.MavenDependency> dependencies = listDependencies(mavenSpyLogsElt);
List<MavenSpyLogProcessor.MavenDependency> dependencies = listDependencies(mavenSpyLogsElt, LOGGER);

if (LOGGER.isLoggable(Level.FINE)) {
listener.getLogger().println("[withMaven] dependenciesFingerprintPublisher - filter: " +
Expand Down Expand Up @@ -201,38 +203,6 @@ public String toString() {
"versions={snapshot:" + isIncludeSnapshotVersions() + ", release:" + isIncludeReleaseVersions() + "}" +
']';
}
/**
* @param mavenSpyLogs Root XML element
* @return list of {@link MavenSpyLogProcessor.MavenArtifact}
*/
@Nonnull
public List<MavenSpyLogProcessor.MavenDependency> listDependencies(Element mavenSpyLogs) {

List<MavenSpyLogProcessor.MavenDependency> result = new ArrayList<>();

for (Element dependencyResolutionResult : XmlUtils.getChildrenElements(mavenSpyLogs, "DependencyResolutionResult")) {
Element resolvedDependenciesElt = XmlUtils.getUniqueChildElementOrNull(dependencyResolutionResult, "resolvedDependencies");

if (resolvedDependenciesElt == null) {
continue;
}

for (Element dependencyElt : XmlUtils.getChildrenElements(resolvedDependenciesElt, "dependency")) {
MavenSpyLogProcessor.MavenDependency dependencyArtifact = XmlUtils.newMavenDependency(dependencyElt);

Element fileElt = XmlUtils.getUniqueChildElementOrNull(dependencyElt, "file");
if (fileElt == null || fileElt.getTextContent() == null || fileElt.getTextContent().isEmpty()) {
LOGGER.log(Level.WARNING, "listDependencies: no associated file found for " + dependencyArtifact + " in " + XmlUtils.toString(dependencyElt));
} else {
dependencyArtifact.file = StringUtils.trim(fileElt.getTextContent());
}

result.add(dependencyArtifact);
}
}

return result;
}

public boolean isIncludeSnapshotVersions() {
return includeSnapshotVersions;
Expand Down
@@ -0,0 +1,61 @@
package org.jenkinsci.plugins.pipeline.maven.publishers;

import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.pipeline.maven.MavenSpyLogProcessor;
import org.jenkinsci.plugins.pipeline.maven.util.XmlUtils;
import org.w3c.dom.Element;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.annotation.Nonnull;

/**
* List dependencies from the spy log.
* @author <a href="mailto:cleclerc@cloudbees.com">Cyrille Le Clerc</a>
*/
public class DependenciesLister {

/**
* @param mavenSpyLogs Root XML element
* @return list of {@link MavenSpyLogProcessor.MavenArtifact}
*/
@Nonnull
public static List<MavenSpyLogProcessor.MavenDependency> listDependencies(final Element mavenSpyLogs,
final Logger logger) {

final List<MavenSpyLogProcessor.MavenDependency> result = new ArrayList<>();

for (final Element dependencyResolutionResult : XmlUtils.getChildrenElements(mavenSpyLogs,
"DependencyResolutionResult")) {
final Element resolvedDependenciesElt = XmlUtils.getUniqueChildElementOrNull(
dependencyResolutionResult, "resolvedDependencies");

if (resolvedDependenciesElt == null) {
continue;
}

for (final Element dependencyElt : XmlUtils.getChildrenElements(resolvedDependenciesElt,
"dependency")) {
final MavenSpyLogProcessor.MavenDependency dependencyArtifact = XmlUtils.newMavenDependency(
dependencyElt);

final Element fileElt = XmlUtils.getUniqueChildElementOrNull(dependencyElt, "file");
if (fileElt == null || fileElt.getTextContent() == null
|| fileElt.getTextContent().isEmpty()) {
logger.log(Level.WARNING, "listDependencies: no associated file found for "
+ dependencyArtifact + " in " + XmlUtils.toString(dependencyElt));
} else {
dependencyArtifact.file = StringUtils.trim(fileElt.getTextContent());
}

result.add(dependencyArtifact);
}
}

return result;
}

}
Expand Up @@ -18,17 +18,21 @@

package org.jenkinsci.plugins.pipeline.maven.publishers;

import static org.jenkinsci.plugins.pipeline.maven.publishers.DependenciesLister.listDependencies;

import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.jgiven.JgivenReportGenerator;
import org.jenkinsci.plugins.jgiven.JgivenReportGenerator.ReportConfig;
import org.jenkinsci.plugins.pipeline.maven.MavenPublisher;
import org.jenkinsci.plugins.pipeline.maven.MavenSpyLogProcessor;
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.kohsuke.stapler.DataBoundConstructor;
import org.w3c.dom.Element;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -71,12 +75,17 @@ public void process(@Nonnull final StepContext context, @Nonnull final Element m
final Run run = context.get(Run.class);
final Launcher launcher = context.get(Launcher.class);

final String pattern = "**/" + REPORTS_DIR + "/*";
final FilePath[] paths = workspace.list(pattern);
if (paths == null || paths.length == 0) {
boolean foundJGivenDependency = false;
List<MavenSpyLogProcessor.MavenDependency> dependencies = listDependencies(mavenSpyLogsElt, LOGGER);
for (MavenSpyLogProcessor.MavenDependency dependency : dependencies) {
if (dependency.artifactId.contains("jgiven")) {
foundJGivenDependency = true;
break;
}
}
if (!foundJGivenDependency) {
if (LOGGER.isLoggable(Level.FINE)) {
listener.getLogger().println("[withMaven] jgivenPublisher - Pattern \"" + pattern
+ "\" does not match any file on workspace, aborting.");
listener.getLogger().println("[withMaven] jgivenPublisher - JGiven not found within your project dependencies, aborting.");
}
return;
}
Expand All @@ -90,6 +99,16 @@ public void process(@Nonnull final StepContext context, @Nonnull final Element m
return;
}

final String pattern = "**/" + REPORTS_DIR + "/*";
final FilePath[] paths = workspace.list(pattern);
if (paths == null || paths.length == 0) {
if (LOGGER.isLoggable(Level.FINE)) {
listener.getLogger().println("[withMaven] jgivenPublisher - Pattern \"" + pattern
+ "\" does not match any file on workspace, aborting.");
}
return;
}

final JgivenReportGenerator generator = new JgivenReportGenerator(new ArrayList<ReportConfig>());

try {
Expand Down
@@ -1,5 +1,7 @@
package org.jenkinsci.plugins.pipeline.maven.publishers;

import static org.jenkinsci.plugins.pipeline.maven.publishers.DependenciesLister.listDependencies;

import hudson.Extension;
import hudson.model.Run;
import hudson.model.TaskListener;
Expand Down Expand Up @@ -88,7 +90,7 @@ public void process(@Nonnull StepContext context, @Nonnull Element mavenSpyLogsE
}

protected void recordDependencies(@Nonnull Element mavenSpyLogsElt, @Nonnull Run run, @Nonnull TaskListener listener, @Nonnull PipelineMavenPluginDao dao) {
List<MavenSpyLogProcessor.MavenDependency> dependencies = listDependencies(mavenSpyLogsElt);
List<MavenSpyLogProcessor.MavenDependency> dependencies = listDependencies(mavenSpyLogsElt, LOGGER);
recordDependencies(dependencies, run, listener, dao);
}

Expand Down Expand Up @@ -253,39 +255,6 @@ public String toString() {
']';
}

/**
* @param mavenSpyLogs Root XML element
* @return list of {@link MavenSpyLogProcessor.MavenArtifact}
*/
@Nonnull
public List<MavenSpyLogProcessor.MavenDependency> listDependencies(Element mavenSpyLogs) {

List<MavenSpyLogProcessor.MavenDependency> result = new ArrayList<>();

for (Element dependencyResolutionResult : XmlUtils.getChildrenElements(mavenSpyLogs, "DependencyResolutionResult")) {
Element resolvedDependenciesElt = XmlUtils.getUniqueChildElementOrNull(dependencyResolutionResult, "resolvedDependencies");

if (resolvedDependenciesElt == null) {
continue;
}

for (Element dependencyElt : XmlUtils.getChildrenElements(resolvedDependenciesElt, "dependency")) {
MavenSpyLogProcessor.MavenDependency dependencyArtifact = XmlUtils.newMavenDependency(dependencyElt);

Element fileElt = XmlUtils.getUniqueChildElementOrNull(dependencyElt, "file");
if (fileElt == null || fileElt.getTextContent() == null || fileElt.getTextContent().isEmpty()) {
LOGGER.log(Level.WARNING, "listDependencies: no associated file found for " + dependencyArtifact + " in " + XmlUtils.toString(dependencyElt));
} else {
dependencyArtifact.file = StringUtils.trim(fileElt.getTextContent());
}

result.add(dependencyArtifact);
}
}

return result;
}

public boolean isIncludeSnapshotVersions() {
return includeSnapshotVersions;
}
Expand Down
Expand Up @@ -9,6 +9,7 @@
import org.jenkinsci.plugins.pipeline.maven.publishers.FindbugsAnalysisPublisher;
import org.jenkinsci.plugins.pipeline.maven.publishers.GeneratedArtifactsPublisher;
import org.jenkinsci.plugins.pipeline.maven.publishers.InvokerRunsPublisher;
import org.jenkinsci.plugins.pipeline.maven.publishers.JGivenTestsPublisher;
import org.jenkinsci.plugins.pipeline.maven.publishers.JunitTestsPublisher;
import org.jenkinsci.plugins.pipeline.maven.publishers.PipelineGraphPublisher;
import org.jenkinsci.plugins.pipeline.maven.publishers.TasksScannerPublisher;
Expand Down Expand Up @@ -36,7 +37,7 @@ public void listMavenPublishers() throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();

List<MavenPublisher> mavenPublishers = MavenPublisher.buildPublishersList(Collections.<MavenPublisher>emptyList(), new StreamTaskListener(baos));
Assert.assertThat(mavenPublishers.size(), CoreMatchers.is(8));
Assert.assertThat(mavenPublishers.size(), CoreMatchers.is(9));

Map<String, MavenPublisher> reportersByDescriptorId = new HashMap<>();
for(MavenPublisher mavenPublisher : mavenPublishers) {
Expand All @@ -49,5 +50,6 @@ public void listMavenPublishers() throws Exception {
assertThat(reportersByDescriptorId.containsKey(new PipelineGraphPublisher.DescriptorImpl().getId()), is(true));
assertThat(reportersByDescriptorId.containsKey(new InvokerRunsPublisher.DescriptorImpl().getId()), is(true));
assertThat(reportersByDescriptorId.containsKey(new ConcordionTestsPublisher.DescriptorImpl().getId()), is(true));
assertThat(reportersByDescriptorId.containsKey(new JGivenTestsPublisher.DescriptorImpl().getId()), is(true));
}
}
Expand Up @@ -14,9 +14,8 @@
/**
* @author <a href="mailto:cleclerc@cloudbees.com">Cyrille Le Clerc</a>
*/
public class DependenciesFingerprintPublisherTest {
public class DependenciesListerTest {
Document doc;
DependenciesFingerprintPublisher dependenciesFingerprintPublisher = new DependenciesFingerprintPublisher();

@Before
public void before() throws Exception {
Expand All @@ -27,7 +26,7 @@ public void before() throws Exception {

@Test
public void listArtifactDependencies() throws Exception {
List<MavenSpyLogProcessor.MavenDependency> mavenArtifacts = dependenciesFingerprintPublisher.listDependencies(doc.getDocumentElement());
List<MavenSpyLogProcessor.MavenDependency> mavenArtifacts = DependenciesLister.listDependencies(doc.getDocumentElement(), null);
System.out.println(mavenArtifacts);
Assert.assertThat(mavenArtifacts.size(), CoreMatchers.is(2));

Expand Down

0 comments on commit 5f2178b

Please sign in to comment.