Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
JENKINS-32847: add warning for sub directory filter
  • Loading branch information
mheinzerling committed Mar 14, 2017
1 parent d771e0b commit 4c5ac73
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/main/java/hudson/plugins/jacoco/JacocoPublisher.java
Expand Up @@ -459,6 +459,11 @@ public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath filePath, @Nonnull
logger.print(" " + Util.join(matchedExecFiles," "));
FilePath[] matchedClassDirs = resolveDirPaths(filePath, taskListener, classPattern);
logger.print("\n[JaCoCo plugin] Saving matched class directories for class-pattern: " + classPattern + ": ");
final String warning = "\n[JaCoCo plugin] WARNING: You are using directory patterns with trailing /, /* or /** . This will most likely" +
" multiply the copied files in your build directory. Check the list below and ignore this warning if you know what you are doing.";
if (hasSubDirectories(classPattern)) {
logger.print(warning);
}
for (FilePath dir : matchedClassDirs) {
int copied = reportDir.saveClassesFrom(dir, "**/*.class");
logger.print("\n[JaCoCo plugin] - " + dir + " " + copied + " files");
Expand All @@ -468,6 +473,7 @@ public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath filePath, @Nonnull
if(!this.skipCopyOfSrcFiles) {
FilePath[] matchedSrcDirs = resolveDirPaths(filePath, taskListener, sourcePattern);
logger.print("\n[JaCoCo plugin] Saving matched source directories for source-pattern: " + sourcePattern + ": ");
if (hasSubDirectories(sourcePattern)) logger.print(warning);
for (FilePath dir : matchedSrcDirs) {
int copied = reportDir.saveSourcesFrom(dir, "**/*.java");
logger.print("\n[JaCoCo plugin] - " + dir + " " + copied + " files");
Expand Down Expand Up @@ -515,6 +521,18 @@ public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath filePath, @Nonnull
}
}

private boolean hasSubDirectories(String pattern) {
for (String dir : pattern.split(DIR_SEP)) {
if (dir.endsWith("\\") || dir.endsWith("/") ||
dir.endsWith("\\*") || dir.endsWith("/*") ||
dir.endsWith("\\**") || dir.endsWith("/**")
) {
return true;
}
}
return false;
}

private JacocoHealthReportThresholds createJacocoHealthReportThresholds(EnvVars env) {
try {
return healthReports = new JacocoHealthReportThresholds(
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/hudson/plugins/jacoco/JacocoPublisherTest.java
Expand Up @@ -414,4 +414,29 @@ public void testCopyClassAndSource() throws IOException, InterruptedException {
assertFalse(new File(run.getRootDir(), "jacoco/sources/test.png").exists());
verify(taskListener, run);
}

@Test
public void testCopyClass_Wrong() throws IOException, InterruptedException {

final Run run = new RunBuilder().taskListener(taskListener).build();
FilePath workspace = new WorkspaceBuilder().name("workspace", ".tst")
.file("classes/Test.class")
.file("classes/Test.jar")
.file("classes/sub/Test2.class")
.build();

JacocoPublisher publisher = new JacocoPublisher();
publisher.setClassPattern("**/classes/");
publisher.perform(run, workspace, launcher, taskListener);

assertTrue(new File(run.getRootDir(), "jacoco/classes/Test.class").exists());
assertFalse(new File(run.getRootDir(), "jacoco/classes/Test.jar").exists());
assertTrue(new File(run.getRootDir(), "jacoco/classes/sub/Test2.class").exists());
assertTrue(new File(run.getRootDir(), "jacoco/classes/Test2.class").exists()); // will be copied accidentally

assertTrue(logContent.toString().contains("WARNING: You are using directory patterns with trailing /, /* or /**"));
assertTrue(logContent.toString().replace("\\","/").contains("tst/classes 2 files"));
assertTrue(logContent.toString().replace("\\","/").contains("tst/classes/sub 1 files"));
verify(taskListener, run);
}
}

0 comments on commit 4c5ac73

Please sign in to comment.