ginkgolinter

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 11, 2022 License: MIT Imports: 8 Imported by: 11

README

ginkgo-linter

This is a golang linter to check usage of the ginkgo and gomega packages.

ginkgo is a testing framework and gomega is its assertion package.

usage

ginkgo-linter [-fix] .

Use the -fix flag to apply the fix suggestions to the source code.

Linter Checks

Wrong Length checks

The linter finds assertion of the golang built-in len function, with all kind of matchers, while there are already gomega matchers for these usecases; We want to assert the item, rather than its length.

There are several wrong patterns:

Expect(len(x)).To(Equal(0)) // should be Expect(x).To(BeEmpty())
Expect(len(x)).To(BeZero()) // should be Expect(x).To(BeEmpty())
Expect(len(x)).To(BeNumeric(">", 0)) // should be Expect(x).ToNot(BeEmpty())
Expect(len(x)).To(BeNumeric(">=", 1)) // should be Expect(x).ToNot(BeEmpty())
Expect(len(x)).To(BeNumeric("==", 0)) // should be Expect(x).To(BeEmpty())
Expect(len(x)).To(BeNumeric("!=", 0)) // should be Expect(x).ToNot(BeEmpty())

Expect(len(x)).To(Equal(1)) // should be Expect(x).To(HaveLen(1))
Expect(len(x)).To(BeNumeric("==", 2)) // should be Expect(x).To(HaveLen(2))
Expect(len(x)).To(BeNumeric("!=", 3)) // should be Expect(x).ToNot(HaveLen(3))

The linter supports the Expect, ExpectWithOffset and the Ω "actual" functions, and the Should, ShouldNot, To, ToNot and NotTo assertion functions.

It also supports the embedded Not() matcher; e.g.

Ω(len(x)).Should(Not(Equal(4))) => Ω(x).ShouldNot(HaveLen(4))

Or even (double negative):

Ω(len(x)).To(Not(BeNumeric(">", 0))) => Ω(x).To(BeEmpty())

The output of the linter,when finding issues, looks like this:

./testdata/src/a/a.go:14:5: ginkgo-linter: wrong length assertion; consider using `Expect("abcd").Should(HaveLen(4))` instead
./testdata/src/a/a.go:18:5: ginkgo-linter: wrong length assertion; consider using `Expect("").Should(BeEmpty())` instead
./testdata/src/a/a.go:22:5: ginkgo-linter: wrong length assertion; consider using `Expect("").Should(BeEmpty())` instead

Suppress the linter

To suppress the wrong length assertion warning, add a comment with (only) ginkgo-linter:ignore-len-assert-warning. There are two options to use this comment:

  1. If the comment is at the top of the file, supress the warning for the whole file; e.g.:
    package mypackage
    
    // ginkgo-linter:ignore-len-assert-warning
    
    import (
        . "github.com/onsi/ginkgo/v2"
        . "github.com/onsi/gomega"
    )
    
    var _ = Describe("my test", func() {
         It("should do something", func() {
             Expect(len("abc")).Should(Equal(3)) // nothing in this file will trigger the warning
         })
    })
    
  2. If the comment is before a wrong length check expression, the warning is suppressed for this expression only; for example:
    It("should test something", func() {
        // ginkgo-linter:ignore-len-assert-warning
        Expect(len("abc")).Should(Equal(3)) // this line will not trigger the warning
        Expect(len("abc")).Should(Equal(3)) // this line will trigger the warning
    }
    

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Analyzer = &analysis.Analyzer{
	Name: "ginkgolinter",
	Doc: `enforces standards of using ginkgo and gomega
currently, the linter searches for wrong length assertions. We want to assert the item rather than its length.
For example:
	Expect(len(x).Should(Equal(1))
This should be replaced with:
	Expect(x).Should(HavelLen(1)`,
	Run:              run,
	RunDespiteErrors: true,
}

Analyzer is the package interface with nogo

Functions

This section is empty.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL