Skip to content

Commit

Permalink
Merge pull request #16 from nebula-plugins/npe-on-error
Browse files Browse the repository at this point in the history
Fix NPE on unresolved dependencies due to untested branch in align rules
  • Loading branch information
DanielThomas committed Apr 26, 2016
2 parents a98ee7c + d8bae34 commit 059134b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
Expand Up @@ -660,12 +660,43 @@ class AlignRulesSpec extends IntegrationSpec {
"""

when:
def results = runTasks('dependencies', '--configuration', 'compile')
runTasksSuccessfully('dependencies', '--configuration', 'compile')

then:
noExceptionThrown()
}

def 'unresolvable dependencies cause warnings to be output'() {
rulesJsonFile << '''\
{
"deny": [], "reject": [], "substitute": [], "replace": [],
"align": [
{
"name": "testNebula",
"group": "com.google.guava",
"reason": "Align guava",
"author": "Example Person <person@example.org>",
"date": "2016-03-17T20:21:20.368Z"
}
]
}
'''.stripIndent()

buildFile << """\
repositories { jcenter() }
dependencies {
compile 'org.slf4j:slf4j-api:1.7.21'
compile 'com.google.guava:guava:oops'
}
"""

when:
def result = runTasksSuccessfully('dependencies', '--configuration', 'compile')

then:
result.standardOutput.contains("Cannot resolve all dependencies to align, configuration 'compile' should also fail to resolve")
}

def 'allow skipping an align rule'() {
def graph = new DependencyGraphBuilder()
.addModule('test.nebula:a:1.0.0')
Expand Down
18 changes: 11 additions & 7 deletions src/main/groovy/nebula/plugin/resolutionrules/Rules.groovy
Expand Up @@ -20,6 +20,7 @@ import org.gradle.api.artifacts.*
import org.gradle.api.artifacts.component.ComponentSelector
import org.gradle.api.artifacts.component.ModuleComponentIdentifier
import org.gradle.api.internal.artifacts.ivyservice.ivyresolve.strategy.DefaultVersionComparator
import org.gradle.api.specs.Specs
import org.joda.time.DateTime
import org.joda.time.DateTimeZone

Expand Down Expand Up @@ -209,20 +210,23 @@ class AlignRules implements ProjectConfigurationRule {
return
}

def detached = configuration.copyRecursive()
detached.exclude group: project.group, module: project.name
def copy = configuration.copyRecursive()
copy.exclude group: project.group, module: project.name
def artifacts
if (detached.resolvedConfiguration.hasError()) {
project.logger.info('Cannot resolve all dependencies to align')
artifacts = detached.resolvedConfiguration.lenientConfiguration.getArtifacts()
def resolvedConfiguration = copy.resolvedConfiguration
if (resolvedConfiguration.hasError()) {
def lenientConfiguration = resolvedConfiguration.lenientConfiguration
project.logger.info("Cannot resolve all dependencies to align, configuration '${configuration.name}' should also fail to resolve")
artifacts = lenientConfiguration.getArtifacts(Specs.SATISFIES_ALL)
} else {
artifacts = detached.resolvedConfiguration.resolvedArtifacts.collect { it.moduleVersion }
artifacts = resolvedConfiguration.resolvedArtifacts
}

def moduleVersions = artifacts.collect { it.moduleVersion }
def selectedVersion = [:]
aligns.each { AlignRule align ->
if (align.shouldNotBeSkipped(extension)) {
def matches = artifacts.findAll { ResolvedModuleVersion dep -> align.resolvedMatches(dep) }
def matches = moduleVersions.findAll { ResolvedModuleVersion dep -> align.resolvedMatches(dep) }
def versions = matches.collect { ResolvedModuleVersion dep -> dep.id.version }.toUnique()
def comparator = new DefaultVersionComparator().asStringComparator()
def alignedVersion = versions.max { a, b -> comparator.compare(a, b) }
Expand Down

0 comments on commit 059134b

Please sign in to comment.