Skip to content

Commit 6b90cf2

Browse files
committed
Merge pull request #13 from rspieldenner/opt_out
Tests for cycles
2 parents 79e3240 + 88baeec commit 6b90cf2

File tree

4 files changed

+181
-21
lines changed

4 files changed

+181
-21
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1.2.0 / 2016/04/06
1+
1.2.0 / 2016/04/11
22
==================
33
- Allow opt out of rules for shared company wide rules that apply to your project, e.g. there is a common align rule for a:foo and a:bar and you produce them
44
- Performance improvement if there are multiple align rules

src/functionalTest/groovy/nebula/plugin/resolutionrules/AlignRulesMultiprojectSpec.groovy

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class AlignRulesMultiprojectSpec extends IntegrationSpec {
6666
// project b depends on a
6767
new File(bDir, 'build.gradle') << '''\
6868
dependencies {
69-
project(':a')
69+
compile project(':a')
7070
}
7171
'''.stripIndent()
7272

@@ -96,4 +96,32 @@ class AlignRulesMultiprojectSpec extends IntegrationSpec {
9696
then:
9797
noExceptionThrown()
9898
}
99+
100+
def 'cycle like behavior'() {
101+
rulesJsonFile << '''\
102+
{
103+
"deny": [], "reject": [], "substitute": [], "replace": [],
104+
"align": [
105+
]
106+
}
107+
'''.stripIndent()
108+
109+
new File(aDir, 'build.gradle') << '''\
110+
dependencies {
111+
testCompile project(':b')
112+
}
113+
'''.stripIndent()
114+
115+
new File(bDir, 'build.gradle') << '''\
116+
dependencies {
117+
compile project(':a')
118+
}
119+
'''.stripIndent()
120+
121+
when:
122+
def results = runTasksSuccessfully(':a:dependencies', ':b:dependencies', 'assemble')
123+
124+
then:
125+
noExceptionThrown()
126+
}
99127
}

src/functionalTest/groovy/nebula/plugin/resolutionrules/AlignRulesPluginInteractionSpec.groovy

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,136 @@ class AlignRulesPluginInteractionSpec extends IntegrationSpec {
234234
then:
235235
result.standardOutput.contains '\\--- test.a:a:1.+ -> 1.42.2\n'
236236
}
237+
238+
def 'publishing, provided, and dependency-recommender interacting with resolution-rules'() {
239+
def graph = new DependencyGraphBuilder()
240+
.addModule('test.a:a:1.42.2')
241+
.addModule('test.a:b:1.2.1')
242+
.build()
243+
def mavenrepo = new GradleDependencyGenerator(graph, "${projectDir}/testrepogen")
244+
mavenrepo.generateTestMavenRepo()
245+
246+
def rulesJsonFile = new File(projectDir, 'rules.json')
247+
248+
rulesJsonFile << '''\
249+
{
250+
"deny": [], "reject": [], "substitute": [], "replace": [],
251+
"align": [
252+
{
253+
"name": "testNebula",
254+
"group": "test.nebula",
255+
"reason": "Align test.nebula dependencies",
256+
"author": "Example Person <person@example.org>",
257+
"date": "2016-03-17T20:21:20.368Z"
258+
}
259+
]
260+
}
261+
'''.stripIndent()
262+
263+
buildFile << """\
264+
buildscript {
265+
repositories { jcenter() }
266+
267+
dependencies {
268+
classpath 'com.netflix.nebula:nebula-dependency-recommender:3.1.0'
269+
classpath 'com.netflix.nebula:nebula-publishing-plugin:4.4.4'
270+
classpath 'com.netflix.nebula:gradle-extra-configurations-plugin:3.0.3'
271+
}
272+
}
273+
274+
apply plugin: 'nebula.dependency-recommender'
275+
apply plugin: 'nebula.maven-publish'
276+
${applyPlugin(ResolutionRulesPlugin)}
277+
apply plugin: 'java'
278+
apply plugin: 'nebula.provided-base'
279+
280+
281+
repositories {
282+
${mavenrepo.mavenRepositoryBlock}
283+
}
284+
285+
dependencyRecommendations {
286+
map recommendations: ['test.a:a': '1.42.2', 'test.a:b': '1.2.1']
287+
}
288+
289+
dependencies {
290+
resolutionRules files('$rulesJsonFile')
291+
compile 'test.a:a'
292+
provided 'test.a:b'
293+
}
294+
""".stripIndent()
295+
296+
when:
297+
def result = runTasksSuccessfully('dependencies', '--configuration', 'compile')
298+
299+
then:
300+
result.standardOutput.contains '+--- test.a:a: -> 1.42.2\n'
301+
result.standardOutput.contains '\\--- test.a:b: -> 1.2.1\n'
302+
}
303+
304+
@spock.lang.Ignore
305+
def 'cycle like behavior'() {
306+
def graph = new DependencyGraphBuilder()
307+
.addModule('test.nebula:c:1.42.2')
308+
.addModule('test.nebula:d:1.2.1')
309+
.build()
310+
def mavenrepo = new GradleDependencyGenerator(graph, "${projectDir}/testrepogen")
311+
mavenrepo.generateTestMavenRepo()
312+
313+
def rulesJsonFile = new File(projectDir, 'rules.json')
314+
315+
rulesJsonFile << '''\
316+
{
317+
"deny": [], "reject": [], "substitute": [], "replace": [],
318+
"align": [
319+
]
320+
}
321+
'''.stripIndent()
322+
323+
buildFile << """\
324+
buildscript {
325+
repositories { jcenter() }
326+
327+
dependencies {
328+
classpath 'com.netflix.nebula:nebula-publishing-plugin:4.4.4'
329+
classpath 'com.netflix.nebula:gradle-extra-configurations-plugin:3.0.3'
330+
}
331+
}
332+
subprojects {
333+
apply plugin: 'nebula.maven-publish'
334+
apply plugin: 'nebula.provided-base'
335+
${applyPlugin(ResolutionRulesPlugin)}
336+
apply plugin: 'java'
337+
338+
339+
340+
repositories {
341+
${mavenrepo.mavenRepositoryBlock}
342+
}
343+
344+
dependencies {
345+
resolutionRules files('$rulesJsonFile')
346+
}
347+
}
348+
""".stripIndent()
349+
350+
def aDir = addSubproject('a', '''\
351+
dependencies {
352+
compile 'test.nebula:c:1.+'
353+
testCompile project(':b')
354+
}
355+
'''.stripIndent())
356+
def bDir = addSubproject('b', '''\
357+
dependencies {
358+
compile 'test.nebula:d:[1.0.0, 2.0.0)'
359+
compile project(':a')
360+
}
361+
'''.stripIndent())
362+
363+
when:
364+
def results = runTasksSuccessfully(':a:dependencies', ':b:dependencies', 'assemble')
365+
366+
then:
367+
noExceptionThrown()
368+
}
237369
}

src/main/groovy/nebula/plugin/resolutionrules/Rules.groovy

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -200,29 +200,29 @@ class AlignRules implements ProjectConfigurationRule {
200200
return
201201
}
202202

203-
def detached = configuration.copyRecursive()
204-
def artifacts
205-
if (detached.resolvedConfiguration.hasError()) {
206-
project.logger.info('Cannot resolve all dependencies to align')
207-
artifacts = detached.resolvedConfiguration.lenientConfiguration.getArtifacts()
208-
} else {
209-
artifacts = detached.resolvedConfiguration.resolvedArtifacts.collect { it.moduleVersion }
210-
}
203+
configuration.resolutionStrategy { ResolutionStrategy rs ->
204+
def detached = configuration.copyRecursive()
205+
def artifacts
206+
if (detached.resolvedConfiguration.hasError()) {
207+
project.logger.info('Cannot resolve all dependencies to align')
208+
artifacts = detached.resolvedConfiguration.lenientConfiguration.getArtifacts()
209+
} else {
210+
artifacts = detached.resolvedConfiguration.resolvedArtifacts.collect { it.moduleVersion }
211+
}
211212

212-
def selectedVersion = [:]
213-
aligns.each { AlignRule align ->
214-
if (align.shouldNotBeSkipped(extension)) {
215-
def matches = artifacts.findAll { ResolvedModuleVersion dep -> align.resolvedMatches(dep) }
216-
def versions = matches.collect { ResolvedModuleVersion dep -> dep.id.version }.toUnique()
217-
def comparator = new DefaultVersionComparator().asStringComparator()
218-
def alignedVersion = versions.max { a, b -> comparator.compare(a, b) }
219-
if (alignedVersion) {
220-
selectedVersion[align] = alignedVersion
213+
def selectedVersion = [:]
214+
aligns.each { AlignRule align ->
215+
if (align.shouldNotBeSkipped(extension)) {
216+
def matches = artifacts.findAll { ResolvedModuleVersion dep -> align.resolvedMatches(dep) }
217+
def versions = matches.collect { ResolvedModuleVersion dep -> dep.id.version }.toUnique()
218+
def comparator = new DefaultVersionComparator().asStringComparator()
219+
def alignedVersion = versions.max { a, b -> comparator.compare(a, b) }
220+
if (alignedVersion) {
221+
selectedVersion[align] = alignedVersion
222+
}
221223
}
222224
}
223-
}
224225

225-
configuration.resolutionStrategy { ResolutionStrategy rs ->
226226
rs.eachDependency { DependencyResolveDetails details ->
227227
def foundMatch = selectedVersion.find { AlignRule rule, String version -> rule.dependencyMatches(details) }
228228
if (foundMatch) {

0 commit comments

Comments
 (0)