com.netflix.nebula.lint.rule.test.AbstractRuleSpec.groovy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gradle-lint-plugin Show documentation
Show all versions of gradle-lint-plugin Show documentation
Pluggable and configurable linter tool for identifying and reporting on patterns of misuse or deprecations in Gradle scripts
/*
* Copyright 2015-2019 Netflix, Inc.
*
* 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.netflix.nebula.lint.rule.test
import com.netflix.nebula.lint.GradleLintFix
import com.netflix.nebula.lint.GradleLintPatchAction
import com.netflix.nebula.lint.plugin.NotNecessarilyGitRepository
import com.netflix.nebula.lint.rule.BuildFiles
import com.netflix.nebula.lint.rule.GradleLintRule
import com.netflix.nebula.lint.rule.GradleModelAware
import groovy.transform.CompileDynamic
import groovy.transform.CompileStatic
import nebula.test.ProjectSpec
import org.codenarc.analyzer.StringSourceAnalyzer
import org.codenarc.results.Results
import org.codenarc.ruleset.CompositeRuleSet
import org.codenarc.ruleset.RuleSet
import org.eclipse.jgit.api.ApplyCommand
import org.gradle.api.Project
@CompileStatic
abstract class AbstractRuleSpec extends ProjectSpec {
def setupSpec() {
Results.mixin ResultsAssert
}
def setup() {
project.configurations.create('compile')
}
protected RuleSet configureRuleSet(Project project, GradleLintRule... rules) {
def ruleSet = new CompositeRuleSet()
rules.each {
ruleSet.addRule(it)
if (it instanceof GradleModelAware) {
it.project = project
}
}
ruleSet
}
protected GradleLintRule[] configureBuildFile(Project project, GradleLintRule... rules) {
rules.each {
it.buildFiles = new BuildFiles([project.buildFile])
}
rules
}
Results runRulesAgainst(GradleLintRule... rules) {
new StringSourceAnalyzer(project.buildFile.text).analyze(configureRuleSet(project, configureBuildFile(project, rules)))
}
@CompileDynamic
String correct(GradleLintRule... rules) {
def analyzer = new StringSourceAnalyzer(project.buildFile.text)
def rulesWithBuildFile = configureBuildFile(project, rules)
def violations = analyzer
.analyze(configureRuleSet(project, *rulesWithBuildFile.collect { it.buildFiles.original(null).file = project.buildFile; it }))
.violations
def patchFile = new File(projectDir, 'lint.patch')
patchFile.text = new GradleLintPatchAction(project).patch(
violations*.fixes.flatten() as List)
new ApplyCommand(new NotNecessarilyGitRepository(projectDir)).setPatch(patchFile.newInputStream()).call()
return project.buildFile.text
}
@Override
boolean deleteProjectDir() {
return false
}
}
class ResultsAssert {
boolean violates(Class extends GradleLintRule> ruleClass) {
this.violations.find { v ->
ruleClass.newInstance().name == v.rule.name
}
}
boolean violates() {
!this.violations.isEmpty()
}
boolean doesNotViolate(Class extends GradleLintRule> ruleClass) {
!violates(ruleClass)
}
boolean doesNotViolate() {
!violates()
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy