Check Whether particular file present in the project or application directory

  • versions used (SonarQube 6.7.6)

  • Description: I am trying to check whether file present in the directory or not.

Eg:- when i run the custom rule i need to scan all the files present in the project and check whether settings.gradle is there or not.

  • What i am looking, is there any way to scan the project location and get all the files ?

hello @AnuragSanagapalli,

I assume you are developing custom plugin. Please have a look at SensorContext#fileSystem() method and FilePredicates class

Thanks @saberduck. I have tried using that but I will explore more on it.

I am writing custom rules to check the required file. So, When i write Junit to test, how to pass current project directory as input to Junit without Hard-coding ?

For Example,

I am wiriting Junit as below,

JavaCheckVerifier.verify("src/main/resources/sonar-project.properties", new PomFileIsMandatoryRule());

The highlighted one was the input, I am testing against one custom rule.

For my scenario to check file exits or not, How can I pass current directory or folder as input to test against custom rule(developed to check file existence).

@AnuragSanagapalli you will probably want to raise issue on project level then, you can use this method to test your rule org.sonar.java.checks.verifier.JavaCheckVerifier#verifyIssueOnProject

You can see the way it is used in implementation of PackageInfoCheck https://github.com/SonarSource/sonar-java/blob/782eb3d8b812e90bba5d7cc4f7073489c1c6c2c6/java-checks/src/test/java/org/sonar/java/checks/PackageInfoCheckTest.java#L43

A post was split to a new topic: Exporting custom rule setting