Giter Site home page Giter Site logo

racodond / sonar-gherkin-plugin Goto Github PK

View Code? Open in Web Editor NEW
36.0 8.0 20.0 562 KB

SonarQube Cucumber Gherkin Analyzer

License: GNU Lesser General Public License v3.0

Java 80.73% Shell 0.11% HTML 3.12% Gherkin 16.04%
code-quality gherkin cucumber guidelines linter sonarqube sonarqube-plugin static-analysis static-analyzer styleguide

sonar-gherkin-plugin's Introduction

Release Build Status AppVeyor Build Status

Quality Gate status Lines of Code Coverage

SonarQube Cucumber Gherkin Analyzer

Disclaimer

I don't want to keep maintaining this plugin. Feel free to ping me if you want to take over.

Description

This SonarQube plugin analyzes Cucumber Gherkin feature files and:

  • Computes metrics: lines of code, number of scenarios, etc.
  • Checks various guidelines to find out potential bugs and code smells through more than 40 checks
  • Provides the ability to write your own checks

Usage

  1. Download and install SonarQube
  2. Install the Cucumber Gherkin plugin by a direct download. The latest version is compatible with SonarQube 6.7+.
  3. Install your favorite scanner (SonarQube Scanner, Maven, Ant, etc.)
  4. Analyze your code

Maven

It is likely that your feature files are not located in source code directories but in test directories. By default, SonarQube doesn't analyze those test directories. Thus, you have to explicitly tell SonarQube to also analyze the test directories containing your feature files.

Let's say that the structure of your project is:

pom.xml
src
  |-- main
        |-- java
        |-- resources
  |-- test
        |-- java
        |-- resources
              |-- features
                    |-- my-feature.feature
                    |-- my-other-feature.feature
                                   
                

In you POM file, you would need to add:

<properties>
  <sonar.sources>pom.xml,src/main/java,src/main/resources,src/test/resources/features</sonar.sources>
</properties>  

Custom Checks

You're thinking of new valuable rules? Version 1.0 or greater provides an API to write your own custom checks. A sample plugin with detailed explanations is available here. If your custom rules may benefit the community, feel free to create a pull request in order to make the rule available in the Cucumber Gherkin analyzer.

You're thinking of new rules that may benefit the community but don't have the time or the skills to write them? Feel free to create an issue for your rules to be taken under consideration.

Metrics

Statements

Number of steps.

Functions

Number of scenarios and scenario outlines.

Classes

Number of features.

Available Rules

  • "FIXME" tags should be handled
  • "TODO" tags should be handled
  • All comments should be formatted consistently
  • And and But prefixes should be used instead of redundant Given/When/Then prefixes
  • Byte Order Mark (BOM) should not be used for UTF-8 files
  • Common Given steps should be added to Background
  • Duplicated steps should be removed
  • End-line characters should be consistent
  • Examples data tables should contain data at least two data rows
  • Features should be written in the same language
  • Features should have a description
  • Features should have a name
  • Features should have a unique name
  • Features should not contain too many scenarios
  • Features that do not define any scenario should be removed
  • File names should comply with a naming convention
  • Files should contain an empty new line at the end
  • Files that do not define any feature should be removed
  • Given steps should follow a regular expression
  • Given/When/Then steps should be defined in the right order
  • Lines should not end with trailing whitespaces
  • Missing data table columns should be added
  • Non-Given steps should be moved out of Background
  • Only tags from the whitelist should be used
  • Regular expression on comment
  • Scenarios should define at least one of each Given/When/Then step type
  • Scenarios should have a name
  • Scenarios should have a unique name
  • Scenarios should not contain too many steps
  • Scenarios that do not define any step should be removed
  • Source code should be properly indented
  • Spelling mistakes should be fixed
  • Star (*) step prefixes should not be used
  • Step sentences should not be too long
  • Steps of unknown type should not be used
  • Tabulation characters should not be used
  • Tags should be defined at the right level
  • Tags should comply with a naming convention
  • Tags should not be set on Examples
  • Then steps should follow a regular expression
  • There should be one single When step per scenario
  • Unused variables should be removed
  • Useless tags should be removed
  • When steps should follow a regular expression
  • Wording should remain at business level

sonar-gherkin-plugin's People

Contributors

racodond avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sonar-gherkin-plugin's Issues

Support scenarios with two 'Examples' sections

`CRITICAL SonarQube violation: Parse error at line 97 column 5:

@mytag
Examples:
| unitName | unitCode |
| myUnit | myCode |

Examples:
^
| unitName | unitCode |
| myUnit1 | myCode1 |
`

Because we use tag for some examples, we need to provide two sections of Example. It works pretty nice with Cucumber - at least this is what we verified using cucumber report so I think that the problem is with the parser that does not support such case.

False positive when a variable is used inside a table

This gherkin code

    Scenario Outline: Create banana with 2 different dates, valid combinations of rateDifference
        Given I have a valid "postBananas.json" json body
        And I have 2 identical elements of "interestRateChanges"
        And I set these elements to:
            | "interestRateChanges[0].date"           | "2025-06-10" |
            | "interestRateChanges[1].date"           | "2025-06-15" |
            | "interestRateChanges[0].rateDifference" | <value0>     |
            | "interestRateChanges[1].rateDifference" | <value1>     |
        When a POST is performed on the endpoint "bananas"
        Then the response code is "201"
        And I get the created banana ID
        And I perform a GET on bananas endpoint with the created banana ID
        And the response code is "200"
        And the response body has these elements:
            | "interestRateChanges[0].date"           | "2025-06-10" |
            | "interestRateChanges[1].date"           | "2025-06-15" |
            | "interestRateChanges[0].rateDifference" | <value0>     |
            | "interestRateChanges[1].rateDifference" | <value1>     |
        Examples:
            | value0 | value1 |
            | 492.0  | -0.01  |
            | -0.01  | -0.012 |

gives the error message "Remove the following unused variables: value0, value1" even though the two variables are indeed used inside other tables.

Feature files not picked up

Hi,
the plugin is not picking up the feature files regardless of me forcefully setting both the language and the folders to include. I'm using maven 3.3.9, maven-sonar plugin 3.2, sonar 6.2 and 1.3 version of your plugin.

Any ideas what I'm doing wrong?

Thanks

Check that .feature files exist in the project

As a Test Manager I am interesting in 1. checking the quality of the tests being written which this helps with and 2. I want to insure my teams are using cucumber in the first place so a rule that checks that feature files exist would be great

Make rule "Spelling mistakes should be fixed" support i18n

Not only on english we write feature

it will be usefull to add to this class support of all language

https://github.com/racodond/sonar-gherkin-plugin/blob/master/gherkin-checks/src/main/java/org/sonar/gherkin/checks/SpellingCheck.java

in our proof of concept we use method getLanguage in Feature file
and after that get JLanguageTool class by this lang-code

 String featureLanguage = feature.getLanguage();
    if ("en".equals(featureLanguage)) {
      featureLanguage = "en-US";
    }

    Language language = Languages.getLanguageForShortName(featureLanguage);
    JLanguageTool langTool = new JLanguageTool(language);

New rule: Unused variable should be removed

Scenario Outline: my scenario
  Given I am a customer
  When I add <number> products to my cart
  Then I should see <number> products in my cart

  Examples:
    | number | type  |
    | 0      | type1 |
    | 1      | type2 |

'type' is not used

Metrics in SonarQube 6.6

Thanks for making this plugin available.
Probably more a question based on my inexperience (and outdated docs?):

I was wondering, how to use the metrics mentioned in the Readme, I'm not seeing them in sonarqube 6.6, JS project.
Only the defaults in /admin/custom_metrics.

FP on rule unused-variable: Variable in doc string not recognized

A variable in a doc string (multiline string) is not recognized and the bug "Remove the following unused variable" is issued.

Example code for reproducing this bug:

Feature: CRUD operations on mocked banana prices

    Background:
        Given that i have a banana WS in "local" environmnet

    Scenario Outline: 4. Verify error messages for updates with invalid values
        When I perform a PUT request for "mock/bananaprices/12345"
            """
            {
                "code":"79365",
                "price":<value>
            }
            """
        Then I receive the response code 400
        Examples:
            | value |
            |       |
            | aaa   |
            | "aaa" |
            | false |

Plugin not working when used with SonarCloud

Hello,

So I wanted to use the plugin with SonarCloud, so I added the following in the pom.xml file of my Java project.

<dependency> <groupId>com.racodond.sonarqube.plugin.gherkin</groupId> <artifactId>sonar-gherkin-plugin</artifactId> <version>1.7</version> </dependency>

<sonar.sources>pom.xml,src/main/java,src/test/resources/features</sonar.sources> (properties section).

I ran the command, "mvn clean install sonar:sonar". The command ran successfully, but the Gherkin plugin did not run or find any issues.

Is there anything that I'm missing?

New rule: Given/When/Then steps should be defined in the right order

Compliant:

Given I am a customer
When I add a product to my cart
Then I should see the product in my cart

Noncompliant:

Given I am a customer
When I add a product to my cart
Then I should see the product in my cart
When I proceed to the order                            # Noncompliant
Then I should see the payment mode choice
...

Change 'allowed-tags' rule parameter from a comma-separated list to a regular expression

@racodond We use SpecFlow and want to use references in feature tags to synchronize TFS work items or test cases (https://www.youtube.com/watch?v=uhZH81sMMfs).
I implemented a plugin already with the rule "allowed-composite-tag" (see https://github.com/Bertk/sonar-gherkin-custom-rules-plugin/).
Can this rule be added to the SonarQube Gherkin plugin?
The current solution of a separate plugin with only one single rule is somehow overdone.

see #58

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.