-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #244 from fcojfernandez/jacoco-hook
Jacoco hook
- Loading branch information
Showing
2 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
plugins-compat-tester/src/main/java/org/jenkins/tools/test/hook/JacocoHook.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package org.jenkins.tools.test.hook; | ||
|
||
import org.jenkins.tools.test.model.PomData; | ||
import org.jenkins.tools.test.model.hook.PluginCompatTesterHookBeforeExecution; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.IntStream; | ||
|
||
/** | ||
* Workaround for JaCoCo plugin since it needs execute the jacoco:prepare-agent goal before execution. | ||
*/ | ||
public class JacocoHook extends PluginCompatTesterHookBeforeExecution { | ||
|
||
@Override | ||
public boolean check(Map<String, Object> info) { | ||
PomData data = (PomData) info.get("pomData"); | ||
return "jacoco".equals(data.artifactId); | ||
} | ||
|
||
@Override | ||
public Map<String, Object> action(Map<String, Object> info) throws Exception { | ||
List<String> args = (List<String>) info.get("args"); | ||
|
||
if (args != null) { | ||
int index = IntStream.range(0, args.size()).filter(i -> args.get(i).startsWith("hpi:")).findFirst().orElse(-1); | ||
if (index == -1) { | ||
index = IntStream.range(0, args.size()).filter(i -> args.get(i).equals("surefire:test")).findFirst().orElse(-1); | ||
} | ||
if (index != -1) { | ||
args.add(index, "jacoco:prepare-agent"); | ||
} | ||
} | ||
|
||
return info; | ||
} | ||
|
||
} |
71 changes: 71 additions & 0 deletions
71
plugins-compat-tester/src/test/java/org/jenkins/tools/test/hook/JacocoHookTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package org.jenkins.tools.test.hook; | ||
|
||
import com.google.common.collect.Lists; | ||
import org.jenkins.tools.test.model.MavenCoordinates; | ||
import org.jenkins.tools.test.model.PomData; | ||
import org.junit.Test; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class JacocoHookTest { | ||
|
||
@Test | ||
public void testCheckMethod() { | ||
final JacocoHook hook = new JacocoHook(); | ||
final MavenCoordinates parent = new MavenCoordinates("org.jenkins-ci.plugins", "plugin", "3.57"); | ||
|
||
PomData pomData = new PomData("jacoco", "hpi", "it-does-not-matter", "whatever", parent, "org.jenkins-ci.plugins"); | ||
Map<String, Object> info = new HashMap<>(); | ||
info.put("pomData", pomData); | ||
assertTrue(hook.check(info)); | ||
|
||
pomData = new PomData("other-plugin", "hpi", "it-does-not-matter", "whatever", parent, "org.jenkins-ci.plugins"); | ||
info = new HashMap<>(); | ||
info.put("pomData", pomData); | ||
assertFalse(hook.check(info)); | ||
} | ||
|
||
@Test | ||
public void testAction() throws Exception { | ||
final JacocoHook hook = new JacocoHook(); | ||
|
||
Map<String, Object> info = new HashMap<>(); | ||
info.put("args", Lists.newArrayList( | ||
"--define=forkCount=1", | ||
"hpi:resolve-test-dependencies", | ||
"hpi:test-hpl", | ||
"surefire:test")); | ||
Map<String, Object> afterAction = hook.action(info); | ||
List<String> args = (List<String>) afterAction.get("args"); | ||
assertThat(args.size(), is(5)); | ||
assertThat(args.get(1), is("jacoco:prepare-agent")); | ||
|
||
info = new HashMap<>(); | ||
info.put("args", Lists.newArrayList( | ||
"--define=forkCount=1", | ||
"other-plugin:other-goal", | ||
"surefire:test")); | ||
afterAction = hook.action(info); | ||
args = (List<String>) afterAction.get("args"); | ||
assertThat(args.size(), is(4)); | ||
assertThat(args.get(2), is("jacoco:prepare-agent")); | ||
|
||
info = new HashMap<>(); | ||
info.put("args", Lists.newArrayList( | ||
"element1", | ||
"element2", | ||
"element3", | ||
"element4")); | ||
afterAction = hook.action(info); | ||
args = (List<String>) afterAction.get("args"); | ||
assertThat(args.size(), is(4)); | ||
assertFalse(args.contains("jacoco:prepare-agent")); | ||
} | ||
} |