Skip to content

Commit

Permalink
Fix io.grpc dependency inconsistencies (#428)
Browse files Browse the repository at this point in the history
`io.grpc` dependencies should no longer cause `versions.lock` generation inconsistencies between different machines.
  • Loading branch information
CRogers authored Feb 19, 2020
1 parent f99b128 commit f3588db
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 2 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ dependencies {

testCompile platform('org.junit:junit-bom')
testImplementation 'com.netflix.nebula:nebula-test'
testImplementation 'org.mockito:mockito-core'
testImplementation 'com.fasterxml.jackson.core:jackson-databind'
testImplementation "org.junit.jupiter:junit-jupiter-api"
testImplementation 'org.assertj:assertj-core'
Expand Down
6 changes: 6 additions & 0 deletions changelog/1.17.3-rc2/pr-428.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: fix
fix:
description: '`io.grpc` dependencies should no longer cause `versions.lock` generation
inconsistencies between different machines.'
links:
- https://github.com/palantir/gradle-consistent-versions/pull/428
7 changes: 7 additions & 0 deletions changelog/@unreleased/pr-428.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type: fix
fix:
description: Dependencies depended on with single version range constraints (like
`[1.27.1]`, used extensively by `io.grpc` dependencies) should no longer cause
`versions.lock` generation inconsistencies between different machines.
links:
- https://github.com/palantir/gradle-consistent-versions/pull/428
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.gradle.api.artifacts.ModuleVersionIdentifier;
Expand All @@ -42,6 +43,8 @@
public final class LockStates {
private static final Logger log = LoggerFactory.getLogger(LockStates.class);

private static final Pattern SINGLE_VERSION_RANGE = Pattern.compile("\\[[^,]+\\]");

private LockStates() {}

/**
Expand Down Expand Up @@ -80,7 +83,7 @@ public static List<String> prettyPrintConstraints(Dependents dependents) {
return constraintEntries
.map(e -> {
List<String> constraintsStr = e.getValue().stream()
.map(VersionConstraint::toString)
.map(LockStates::versionConstraintToString)
.filter(string -> !string.isEmpty()) // toString is empty if the constraint is a no-op
.collect(toList());

Expand All @@ -99,6 +102,16 @@ public static List<String> prettyPrintConstraints(Dependents dependents) {
.collect(toList());
}

private static String versionConstraintToString(VersionConstraint versionConstraint) {
String constraintString = versionConstraint.toString();

if (SINGLE_VERSION_RANGE.matcher(constraintString).matches()) {
return constraintString.substring(1, constraintString.length() - 1);
}

return constraintString;
}

private static String formatComponentIdentifier(ComponentIdentifier id) {
if (id instanceof ModuleComponentIdentifier) {
// We don't include the version, as conflicts in the version would show up on the line for that version.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* (c) Copyright 2020 Palantir Technologies Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.palantir.gradle.versions.lockstate;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import com.google.common.collect.ImmutableSet;
import com.palantir.gradle.versions.GradleComparators;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import org.gradle.api.artifacts.VersionConstraint;
import org.gradle.api.artifacts.component.ComponentIdentifier;
import org.junit.jupiter.api.Test;

class LockStatesTest {
@Test
void modules_like_grpc_should_have_its_square_bracket_version_stripped() {
ComponentIdentifier grpcApi = componentIdentifier("io.grpc:grpc-api");
ComponentIdentifier grpcCore = componentIdentifier("io.grpc:grpc-core");
ComponentIdentifier grpcNetty = componentIdentifier("io.grpc:grpc-netty");
ComponentIdentifier somethingElse = componentIdentifier("something:else");

VersionConstraint squareBracketConstraint = versionConstraint("[1.27.1]");
VersionConstraint normalConstraint = versionConstraint("1.27.1");

SortedMap<ComponentIdentifier, Set<VersionConstraint>> dependents =
new TreeMap<>(GradleComparators.COMPONENT_IDENTIFIER_COMPARATOR);

dependents.put(grpcApi, ImmutableSet.of(squareBracketConstraint));
dependents.put(grpcCore, ImmutableSet.of(normalConstraint));
dependents.put(grpcNetty, ImmutableSet.of(squareBracketConstraint, normalConstraint));
dependents.put(somethingElse, ImmutableSet.of(squareBracketConstraint));

assertThat(LockStates.prettyPrintConstraints(Dependents.of(dependents)))
.containsExactly(
"io.grpc:grpc-api -> 1.27.1",
"io.grpc:grpc-core -> 1.27.1",
"io.grpc:grpc-netty -> {1.27.1, 1.27.1}",
"something:else -> 1.27.1");
}

private ComponentIdentifier componentIdentifier(String componentIdentifier) {
ComponentIdentifier grpcApi = mock(ComponentIdentifier.class);
when(grpcApi.getDisplayName()).thenReturn(componentIdentifier);
return grpcApi;
}

private VersionConstraint versionConstraint(String version) {
VersionConstraint constraint = mock(VersionConstraint.class);
when(constraint.toString()).thenReturn(version);
return constraint;
}
}
5 changes: 4 additions & 1 deletion versions.lock
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ com.fasterxml.jackson.core:jackson-databind:2.10.2 (1 constraints: 3705313b)
com.netflix.nebula:nebula-test:7.8.1 (1 constraints: 12052736)
commons-io:commons-io:2.5 (1 constraints: eb0c8d0a)
junit:junit:4.13 (2 constraints: 541d390d)
net.bytebuddy:byte-buddy:1.10.5 (1 constraints: 410b37de)
net.bytebuddy:byte-buddy-agent:1.10.5 (1 constraints: 410b37de)
org.apiguardian:apiguardian-api:1.1.0 (6 constraints: 7d64a5c6)
org.assertj:assertj-core:3.15.0 (1 constraints: 3b05443b)
org.hamcrest:hamcrest-core:1.3 (1 constraints: cc05fe3f)
Expand All @@ -42,6 +44,7 @@ org.junit.jupiter:junit-jupiter-params:5.6.0 (2 constraints: 1917223c)
org.junit.platform:junit-platform-commons:1.6.0 (3 constraints: e2299d29)
org.junit.platform:junit-platform-engine:1.6.0 (3 constraints: 302a795b)
org.junit.vintage:junit-vintage-engine:5.6.0 (1 constraints: 12098595)
org.objenesis:objenesis:2.4 (1 constraints: ea0c8c0a)
org.mockito:mockito-core:3.2.4 (1 constraints: 0b050436)
org.objenesis:objenesis:2.6 (2 constraints: 9d17f557)
org.opentest4j:opentest4j:1.2.0 (2 constraints: cd205b49)
org.spockframework:spock-core:1.3-groovy-2.4 (1 constraints: 7c10f3af)
2 changes: 2 additions & 0 deletions versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ org.immutables:value = 2.8.3
org.junit:junit-bom = 5.6.0
com.fasterxml.jackson.*:jackson-* = 2.10.2
org.assertj:* = 3.15.0
org.mockito:mockito-core = 3.2.4


# Unnecessary once we have lock files
com.google.code.findbugs:jsr305 = 3.0.2
Expand Down

0 comments on commit f3588db

Please sign in to comment.