Skip to content

Commit

Permalink
fail check if no signatires declared (to catch configuration errors e…
Browse files Browse the repository at this point in the history
…arly)
  • Loading branch information
xvik committed Jan 20, 2025
1 parent 5d71c61 commit f3df944
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 61 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
* (breaking) Drop gradle 5 and 6 support
* (breaking) Fail check when no signatures declared (required to catch early configuration errors)
* Add android projects support (activates for android library and application plugins
and use variant components (debug, release and test) instead of source sets).
* Add kotlin multiplatform projects support (tasks created for each platform compilation, except metadata (common))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ru.vyarus.gradle.plugin.animalsniffer

import groovy.transform.CompileStatic
import groovy.transform.TypeCheckingMode
import org.gradle.api.Action
import org.gradle.api.GradleException
import org.gradle.api.file.FileCollection
Expand Down Expand Up @@ -63,7 +62,6 @@ abstract class AnimalSniffer extends SourceTask implements VerificationTask, Rep
/**
* Signature files used for checks.
*/
@SkipWhenEmpty
@Classpath
@InputFiles
FileCollection animalsnifferSignatures
Expand Down Expand Up @@ -136,42 +134,8 @@ abstract class AnimalSniffer extends SourceTask implements VerificationTask, Rep
abstract WorkerExecutor getWorkerExecutor()

@TaskAction
@CompileStatic(TypeCheckingMode.SKIP)
void run() {
List<File> sortedPath = preparePath(getSource())
if (getDebug()) {
printTaskConfig(sortedPath)
}
Set<File> sourceDirs = getSourcesDirs().getFiles()
String annotation = getAnnotation()
FileCollection signatures = getAnimalsnifferSignatures()
Set<File> classpathFiles = getClasspath().asFileTree.files
List<String> ignoreClasses = getIgnoreClasses()
boolean quiet = isIgnoreFailures()

WorkQueue workQueue = workerExecutor.classLoaderIsolation {
it.classpath.from(getAnimalsnifferClasspath())
}

// report file used for errors list exchange between task and worker
File errors = reports.text.outputLocation.get().asFile
workQueue.submit(CheckWorker) { params ->
params.signatures.addAll(signatures)
params.classpath.addAll(classpathFiles)
params.classes.addAll(sortedPath)
params.sourceDirs.addAll(sourceDirs)
params.annotations.add('org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement')
if (annotation) {
params.annotations.add(annotation)
}
params.ignored.addAll(ignoreClasses)
params.ignoreErrors.set(quiet)

// always create report file because its the only way to know if errors were found
params.reportOutput.set(errors)
}

workQueue.await()
File errors = runAnimalsniffer()

// if file exists then violations were found (or check process failed)
if (errors.exists()) {
Expand Down Expand Up @@ -235,7 +199,49 @@ abstract class AnimalSniffer extends SourceTask implements VerificationTask, Rep
return new TreeSet<File>(objectFactory.fileCollection().from(classesDirs).files)
}

@CompileStatic(TypeCheckingMode.SKIP)
File runAnimalsniffer() {
List<File> sortedPath = preparePath(getSource())
if (getDebug()) {
printTaskConfig(sortedPath)
}
Set<File> sourceDirs = getSourcesDirs().getFiles()
String annotation = getAnnotation()
FileCollection signatures = getAnimalsnifferSignatures()
Set<File> classpathFiles = getClasspath().asFileTree.files
Iterable<String> ignoreClasses = getIgnoreClasses()
boolean quiet = isIgnoreFailures()

if (signatures.empty) {
throw new GradleException('No signatures declared for animalsniffer. If you declared signatures, ' +
'make sure "@signature" qualifier used')
}

WorkQueue workQueue = workerExecutor.classLoaderIsolation {
it.classpath.from(getAnimalsnifferClasspath())
}

// report file used for errors list exchange between task and worker
File errors = reports.text.outputLocation.get().asFile
workQueue.submit(CheckWorker) { params ->
params.signatures.addAll(signatures)
params.classpath.addAll(classpathFiles)
params.classes.addAll(sortedPath)
params.sourceDirs.addAll(sourceDirs)
params.annotations.add('org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement')
if (annotation) {
params.annotations.add(annotation)
}
params.ignored.addAll(ignoreClasses)
params.ignoreErrors.set(quiet)

// always create report file because its the only way to know if errors were found
params.reportOutput.set(errors)
}

workQueue.await()
return errors
}

void processErrors(ReportBuilder collector) {
if (collector.errorsCnt() > 0) {
String message = "${collector.errorsCnt()} AnimalSniffer violations were found " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,8 @@ class AnimalSnifferPlugin implements Plugin<Project> {
}
}
checkTask.configure {
// skip if no signatures configured or no sources to check
onlyIf { !it.getAnimalsnifferSignatures().empty && it.getSource().size() > 0 }
// skip if no sources to check
onlyIf { it.getSource().size() > 0 }
conventionMapping.with {
classpath = {
extension.cache.enabled ?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,17 +169,11 @@ class FailKitTest extends AbstractKitTest {
// debug()

when: "run task"
BuildResult result = run('check')
BuildResult result = runFailed('check')

then: "task successful"
result.task(':check').outcome == TaskOutcome.UP_TO_DATE
result.task(':animalsnifferMain').outcome == TaskOutcome.SKIPPED

then: "animalsniffer skipepd"
!result.output.contains("AnimalSniffer violations were found")

then: "no report"
!file('/build/reports/animalsniffer/main.text').exists()
result.task(':animalsnifferMain').outcome == TaskOutcome.FAILED
result.output.contains("No signatures declared for animalsniffer")
}

def "Check only tests configured"() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ class IncorrectSignaturesConfigKitTest extends AbstractKitTest {
// debug()

when: "run task"
BuildResult result = run('check')
BuildResult result = runFailed('check')

then: "task successful"
result.task(':check').outcome == TaskOutcome.UP_TO_DATE
result.task(':animalsnifferMain').outcome == TaskOutcome.SKIPPED
result.task(':animalsnifferMain').outcome == TaskOutcome.FAILED
result.output.contains("No signatures declared for animalsniffer")

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,11 @@ class CacheCasesKitTest extends AbstractKitTest {
// debug()

when: "run task"
BuildResult result = run('check')
BuildResult result = runFailed('check')

then: "task successful"
result.task(':check').outcome == TaskOutcome.UP_TO_DATE
result.task(':animalsnifferMain').outcome == TaskOutcome.SKIPPED

then: "animalsniffer skipepd"
!result.output.contains("AnimalSniffer violations were found")

then: "no report"
!file('/build/reports/animalsniffer/main.text').exists()
result.task(':animalsnifferMain').outcome == TaskOutcome.FAILED
result.output.contains("No signatures declared for animalsniffer")
}

def "Check only tests configured"() {
Expand Down

0 comments on commit f3df944

Please sign in to comment.