-
Notifications
You must be signed in to change notification settings - Fork 2
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 #85 from menny/allow-test-only-deps
Fixing test-only marking for inner-deps
- Loading branch information
Showing
4 changed files
with
253 additions
and
14 deletions.
There are no files selected for viewing
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
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
55 changes: 55 additions & 0 deletions
55
resolver/src/main/java/net/evendanan/bazel/mvn/merger/TestOnlyMarker.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,55 @@ | ||
package net.evendanan.bazel.mvn.merger; | ||
|
||
import net.evendanan.bazel.mvn.api.model.Dependency; | ||
import net.evendanan.bazel.mvn.api.model.MavenCoordinate; | ||
import net.evendanan.bazel.mvn.api.model.Resolution; | ||
|
||
import java.util.Collection; | ||
import java.util.Set; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Collectors; | ||
|
||
public class TestOnlyMarker { | ||
public static Predicate<MavenCoordinate> mark(Collection<Resolution> resolutions, Set<MavenCoordinate> initialTestOnlyMap) { | ||
if (initialTestOnlyMap.isEmpty()) | ||
return c -> initialTestOnlyMap.contains(strippedDownCoord(c)); | ||
|
||
final Set<MavenCoordinate> marked = initialTestOnlyMap | ||
.stream() | ||
.map(TestOnlyMarker::strippedDownCoord) | ||
.collect(Collectors.toSet()); | ||
|
||
resolutions.forEach(resolution -> { | ||
if (isTestOnlySubTree(marked, resolution.allResolvedDependencies(), resolution.rootDependency()) && !initialTestOnlyMap.contains(resolution.rootDependency())) { | ||
throw new GraphVerifications.InvalidGraphException("TestOnlyMarker", resolution.rootDependency(), "Dependency has test-only dependencies but is not a test-only artifact!"); | ||
} | ||
}); | ||
return c -> marked.contains(strippedDownCoord(c)); | ||
} | ||
|
||
private static boolean isTestOnlySubTree(Set<MavenCoordinate> marked, Collection<Dependency> allResolvedDependencies, MavenCoordinate current) { | ||
final Dependency dependency = allResolvedDependencies | ||
.stream() | ||
.filter(d -> d.mavenCoordinate().equals(current)) | ||
.findFirst() | ||
.get(); | ||
/*NOTE: we have to go over the entire list, so the subtrees will be marked as wall*/ | ||
final AtomicBoolean foundTestOnly = new AtomicBoolean(false); | ||
dependency.dependencies().forEach(child -> { | ||
if (isTestOnlySubTree(marked, allResolvedDependencies, child)) | ||
foundTestOnly.set(true); | ||
}); | ||
|
||
if (foundTestOnly.get()) { | ||
marked.add(strippedDownCoord(current)); | ||
return true; | ||
} else { | ||
return marked.contains(strippedDownCoord(current)); | ||
} | ||
} | ||
|
||
private static MavenCoordinate strippedDownCoord(MavenCoordinate coordinate) { | ||
return MavenCoordinate.create(coordinate.groupId(), coordinate.artifactId(), "", ""); | ||
} | ||
} |
159 changes: 159 additions & 0 deletions
159
resolver/src/main/javatest/net/evendanan/bazel/mvn/merger/TestOnlyMarkerTest.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,159 @@ | ||
package net.evendanan.bazel.mvn.merger; | ||
|
||
import net.evendanan.bazel.mvn.api.model.Dependency; | ||
import net.evendanan.bazel.mvn.api.model.MavenCoordinate; | ||
import net.evendanan.bazel.mvn.api.model.Resolution; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.function.Predicate; | ||
|
||
public class TestOnlyMarkerTest { | ||
|
||
@Test | ||
public void testMarkingHappyPath() { | ||
final Dependency junitDep = Dependency.builder() | ||
.mavenCoordinate(MavenCoordinate.create("junit", "junit", "2", "jar")) | ||
.build(); | ||
final Resolution junit = Resolution.create( | ||
junitDep.mavenCoordinate(), | ||
Collections.singleton(junitDep)); | ||
|
||
final Resolution appTestUtil = Resolution.create( | ||
MavenCoordinate.create("util", "junit-helper", "1", "jar"), | ||
Arrays.asList( | ||
junitDep, | ||
Dependency.builder() | ||
.mavenCoordinate(MavenCoordinate.create("a", "b", "1", "jar")) | ||
.dependencies(Collections.singleton(junitDep.mavenCoordinate())) | ||
.build(), | ||
Dependency.builder() | ||
.mavenCoordinate(MavenCoordinate.create("util", "junit-helper", "1", "jar")) | ||
.dependencies(Collections.singleton(MavenCoordinate.create("a", "b", "1", "jar"))) | ||
.build()) | ||
); | ||
|
||
final HashSet<MavenCoordinate> initialTestOnlyMap = new HashSet<>(Arrays.asList(junitDep.mavenCoordinate(), appTestUtil.rootDependency())); | ||
final Predicate<MavenCoordinate> marked = TestOnlyMarker.mark( | ||
Arrays.asList(junit, appTestUtil), | ||
initialTestOnlyMap); | ||
|
||
Assert.assertTrue(marked.test(junit.rootDependency())); | ||
Assert.assertTrue(marked.test(appTestUtil.rootDependency())); | ||
Assert.assertTrue(marked.test(MavenCoordinate.create("a", "b", "1", "jar"))); | ||
//testing strip-down | ||
Assert.assertTrue(marked.test(MavenCoordinate.create("a", "b", "2", "jar"))); | ||
Assert.assertTrue(marked.test(MavenCoordinate.create("a", "b", "1", "aar"))); | ||
//random | ||
Assert.assertFalse(marked.test(MavenCoordinate.create("b", "b", "1", "jar"))); | ||
} | ||
|
||
@Test | ||
public void testMarkingHappyPathWithNonTestOnly() { | ||
final Dependency junitDep = Dependency.builder() | ||
.mavenCoordinate(MavenCoordinate.create("junit", "junit", "2", "jar")) | ||
.build(); | ||
final Resolution junit = Resolution.create( | ||
junitDep.mavenCoordinate(), | ||
Collections.singleton(junitDep)); | ||
|
||
final Resolution appUtil = Resolution.create( | ||
MavenCoordinate.create("util", "helper", "1", "jar"), | ||
Arrays.asList( | ||
junitDep, | ||
Dependency.builder() | ||
.mavenCoordinate(MavenCoordinate.create("a", "b", "1", "jar")) | ||
.dependencies(Collections.emptyList()) | ||
.build(), | ||
Dependency.builder() | ||
.mavenCoordinate(MavenCoordinate.create("util", "helper", "1", "jar")) | ||
.dependencies(Collections.singleton(MavenCoordinate.create("a", "b", "1", "jar"))) | ||
.build()) | ||
); | ||
|
||
final Set<MavenCoordinate> initialTestOnlyMap = Collections.singleton(junitDep.mavenCoordinate()); | ||
final Predicate<MavenCoordinate> marked = TestOnlyMarker.mark( | ||
Arrays.asList(junit, appUtil), | ||
initialTestOnlyMap); | ||
|
||
Assert.assertTrue(marked.test(junit.rootDependency())); | ||
Assert.assertFalse(marked.test(appUtil.rootDependency())); | ||
Assert.assertFalse(marked.test(MavenCoordinate.create("a", "b", "1", "jar"))); | ||
} | ||
|
||
@Test | ||
public void testMarkingWhenTestOnlyDepHasDifferentVersion() { | ||
final Dependency junitDep = Dependency.builder() | ||
.mavenCoordinate(MavenCoordinate.create("junit", "junit", "2", "jar")) | ||
.build(); | ||
final Resolution junit = Resolution.create( | ||
junitDep.mavenCoordinate(), | ||
Collections.singleton(junitDep)); | ||
|
||
final Dependency junitOld = Dependency.builder(junitDep) | ||
.mavenCoordinate(MavenCoordinate.create("junit", "junit", "1", "jar")) | ||
.build(); | ||
final Resolution appTestUtil = Resolution.create( | ||
MavenCoordinate.create("util", "junit-helper", "1", "jar"), | ||
Arrays.asList( | ||
junitOld, | ||
Dependency.builder() | ||
.mavenCoordinate(MavenCoordinate.create("a", "b", "1", "jar")) | ||
.dependencies(Collections.singleton(junitOld.mavenCoordinate())) | ||
.build(), | ||
Dependency.builder() | ||
.mavenCoordinate(MavenCoordinate.create("util", "junit-helper", "1", "jar")) | ||
.dependencies(Collections.singleton(MavenCoordinate.create("a", "b", "1", "jar"))) | ||
.build()) | ||
); | ||
|
||
final Predicate<MavenCoordinate> marked = TestOnlyMarker.mark( | ||
Arrays.asList(junit, appTestUtil), | ||
new HashSet<>(Arrays.asList(junitDep.mavenCoordinate(), appTestUtil.rootDependency()))); | ||
|
||
Assert.assertTrue(marked.test(junit.rootDependency())); | ||
Assert.assertTrue(marked.test(MavenCoordinate.create("junit", "junit", "1", "jar"))); | ||
Assert.assertTrue(marked.test(appTestUtil.rootDependency())); | ||
Assert.assertTrue(marked.test(MavenCoordinate.create("a", "b", "1", "jar"))); | ||
Assert.assertTrue(marked.test(MavenCoordinate.create("a", "b", "2", "jar"))); | ||
} | ||
|
||
@Test(expected = GraphVerifications.InvalidGraphException.class) | ||
public void testThrowsExceptionIfRootIsMarkedAsTestOnlyByMarker() { | ||
final Dependency junitDep = Dependency.builder() | ||
.mavenCoordinate(MavenCoordinate.create("junit", "junit", "1", "jar")) | ||
.build(); | ||
final Resolution junit = Resolution.create( | ||
junitDep.mavenCoordinate(), | ||
Collections.singleton(junitDep)); | ||
|
||
final Resolution appTestUtil = Resolution.create( | ||
MavenCoordinate.create("util", "junit-helper", "1", "jar"), | ||
Arrays.asList( | ||
junitDep, | ||
Dependency.builder() | ||
.mavenCoordinate(MavenCoordinate.create("util", "junit-helper", "1", "jar")) | ||
.dependencies(Collections.singleton(junitDep.mavenCoordinate())) | ||
.build()) | ||
); | ||
|
||
TestOnlyMarker.mark(Arrays.asList(junit, appTestUtil), Collections.singleton(junitDep.mavenCoordinate())); | ||
} | ||
|
||
@Test | ||
public void testReturnsSameIfInitialMapIsEmpty() { | ||
final Set<MavenCoordinate> emptySet = new HashSet<>(); | ||
final Predicate<MavenCoordinate> marked = TestOnlyMarker.mark( | ||
Collections.singleton(Resolution.create( | ||
MavenCoordinate.create("a", "b", "1", "jar"), | ||
Collections.emptyList())), | ||
emptySet); | ||
|
||
Assert.assertFalse(marked.test(MavenCoordinate.create("a", "b", "1", "jar"))); | ||
} | ||
} |