gradle-lint-plugin icon indicating copy to clipboard operation
gradle-lint-plugin copied to clipboard

Test for 'custom' rule

Open Lisanderl opened this issue 7 years ago • 3 comments

Hi! I have written simple rule for my project, and it works. But in test it didn't. Probably test runner can't find rule id. Maybe someone have had the same problem ?

It's my test: ........................................................................................................

def "should fall"() {
		given:
		createFiles()
		buildFile << """
		
		plugins {

		id 'java'
		id 'groovy'
		id 'meta-apps-lint'
		}
			
		task somwTask {
			while(true){
				println ' '
				break;
			}
		}
		
		repositories {
                  mavenCentral()
		}	

		dependencies{
			compile 'junit:junit:4.12'
		}
		
		gradleLint {
 
		criticalRules = ['wrong-script-rule'] 
			}

		""".trim()
		
		when:
		def result = gradle('build', '--stacktrace')
		
		then:
		assert result.task(":build").outcome == SUCCESS
		
	}

It's my rule: ............................................................................................

String description = 'Finds java code in script';
	@Override 
	void visitBuildscript (MethodCallExpression call) {
		Pattern pattern = Pattern.compile(badScriptRegex);
		Matcher matcher = pattern.matcher(buildFiles.getText());
		if ( matcher.find() ) {
			println 'Bad scrypt detected'
			addBuildLintViolation(message)
		}}}

Also i create src/main/resources/META-INF/lint-rules/wrong-script-rule.properties file . If i use 'default rule' like unused-dependency it works.

Lisanderl avatar Oct 05 '18 11:10 Lisanderl

Hi @Lisanderl, I don't see anything obvious at the first look. I assume that you apply our plugin through meta-apps-lint. I would look around parent class of your test. Here is one of our test which is kind of similar https://github.com/nebula-plugins/gradle-lint-plugin/blob/master/src/test/groovy/com/netflix/nebula/lint/rule/dependency/MinimumDependencyVersionRuleSpec.groovy Here is the parent class which we use https://github.com/nebula-plugins/gradle-lint-plugin/blob/master/src/test/groovy/com/netflix/nebula/lint/TestKitSpecification.groovy My guess is that Gradle which you run in the test doesn't see the project under test on its classpath. Our TestKitSpecification is providing that feature. I believe a similar class is also provided by nebula-test project (https://github.com/nebula-plugins/nebula-test/blob/master/src/main/groovy/nebula/test/IntegrationTestKitSpec.groovy) so you don't have to copy-paste this code. We haven't migrated this project to nebula-test yet.

chali avatar Oct 05 '18 12:10 chali

Complementing @chali comment, have you tried to look at your build/resources/main/META-INF/lint-rules to check that the properties file is available when you execute your test?

rpalcolea avatar Oct 05 '18 16:10 rpalcolea

Complementing @chali comment, have you tried to look at your build/resources/main/META-INF/lint-rules to check that the properties file is available when you execute your test?

Yeah, it's available, and default rules works if i add them to my src/main/resources/META-INF/lint-rules/wrong-script-rule.properties, like 'includes=unused-dependency,all-dependency'

Lisanderl avatar Oct 09 '18 11:10 Lisanderl