Skip to content

Commit

Permalink
bump version and update readme/tests (#1)
Browse files Browse the repository at this point in the history
* bump Go version and dependencies
* better test names, making them easier to understand
* improve readme
* remove deprecated golint
* bump golangci-lint version
  • Loading branch information
aziule authored Nov 14, 2024
1 parent a66cb42 commit a1b08cf
Show file tree
Hide file tree
Showing 179 changed files with 41,783 additions and 10,324 deletions.
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ issues:

linters:
enable:
- golint
- gofumpt
- gosec
- unparam
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Variables
GOLANGCI-LINT-VERSION=v1.28.2
GOLANGCI-LINT-VERSION=v1.62.0
GOLANGCI-LINT=docker run --env=GOFLAGS=-mod=vendor --rm -v $(CURDIR):/app -w /app golangci/golangci-lint:$(GOLANGCI-LINT-VERSION) golangci-lint

# Targets
Expand Down
54 changes: 34 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# filebuildtag
Linter that matches Go file naming patterns to their expected build tags.
Linter enforcing files to contain expected build tags (`// +build` instruction), based on the file name.

---

Expand All @@ -8,13 +8,20 @@ Linter that matches Go file naming patterns to their expected build tags.

[Jump to Installation and usage](#installation-and-usage)

## Benefits
## Real world use case

Let's say you put integration tests in files named `*_integration_test.go` and you run them as part of your CI pipeline.

You might forget to add the `// +build integration` instruction on a newly created file, or you might remove it inadvertently,
and it can have some consequences, such as never running during the CI pipeline.

Match file naming patterns to build tags and make sure these files always have the expected build tags in the `// +build` instruction.
As a consequence, you think your code works when it doesn't, because it is not tested, but you believe it is.

This linter can help with such issues and let you know when you forgot to add the expected build tags.

## Features

### One-To-One match
### Exact match

Example: files named `foo.go` must include the `bar` build tag.

Expand All @@ -25,7 +32,7 @@ File: `foo.go`
package foo
```

### Many-To-One match
### Wildcard match

Example: files ending with `_suffix.go` must include the `bar` build tag.

Expand All @@ -45,30 +52,19 @@ package foo

### Go's `buildtag` linter support

Built on top of the `buildtag` linter, it supports its Go files features.
`filebuildtag` is built on top of the `buildtag` linter, hence it supports its features.

### And also

* Run it as a standalone command
* Integrate it as a part of a runner using the provided `analysis.Analyzer`

## Real world use case

Let's say you name your integration tests `*_integration_test.go` and you run them as part of your CI pipeline.

You might forget to add the `// +build integration` instruction on a newly created file, or you might remove it inadvertently,
and it can have some consequences, such as never running during your pipeline.

As a consequence, you think your code works when it doesn't, because it is not tested (but you believe it is).

This linter can help with such issues and let you know when you forgot to add the expected build tags.

## Installation and usage

**Install with Go install**

```shell
GO111MODULE=on go get github.com/aziule/filebuildtag/cmd/filebuildtag
go get github.com/aziule/filebuildtag/cmd/filebuildtag
```

**Install and build from source**
Expand All @@ -85,10 +81,10 @@ make build
filebuildtag --filetags foo.go:bar ./...

// All files ending with "_integration_test.go" must have the "integration" tag
filebuildtag --filetags *_integration_test.go:integration ./...
filebuildtag --filetags "*_integration_test.go:integration" ./...

// Both of the above
filebuildtag --filetags foo.go:bar,*_integration_test.go:integration ./...
filebuildtag --filetags "foo.go:bar,*_integration_test.go:integration" ./...

// Only check that the `// +build` instructions are correct (no args to pass)
filebuildtag ./...
Expand All @@ -97,6 +93,8 @@ filebuildtag ./...
*Note: files naming patterns are matched using Go's `filepath.Match` method. Therefore, you can use any of its supported patterns.
See [File patterns](#file-patterns) for more information and examples.*

Head to the [test scenarios](./filebuildtag_test.go) for more examples.

## Using with linters runners

This linter exposes an `Analyzer` (accessible via `filebuildtag.Analyzer`), which is defined as
Expand Down Expand Up @@ -138,6 +136,22 @@ character-range:
| a_test.go | 🚫 || 🚫 ||
| something | 🚫 | 🚫 | 🚫 | 🚫 |

## Development



**Run tests**

```shell
make test
```

**Lint**

```shell
make lint
```

## Roadmap

* Support for folder name matching (`/pkg/**/foo.go`, `/pkg/foo/*.go`, etc.).
Expand Down
14 changes: 7 additions & 7 deletions filebuildtag.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ import (

const (
// Doc of the linter.
Doc = `check that Go files have the expected build tags in the "// +build" instruction
Doc = `ensure Go files have the expected "// +build <tag>" instruction based on the file name
Bind file patterns to build tags, for instance:
File named "bar.go" must have the "baz" build tag
Files matching "*_integration_test.go" must have the "integration" build tag`
Bind file names to their expected build tags, such as:
Files named "foo.go" must have the "foo" build tag
Files with the suffix "*_integration_test.go" must have the "integration" build tag`
// FlagFiletagsName is the name of the default filetags flag. It is exported to be reused from linters runners.
FlagFiletagsName = "filetags"
// FlagFiletagsDoc is the usage doc of the default filetags flag. It is exported to be reused from linters runners.
FlagFiletagsDoc = `Comma separated list of file names and build tags using the form "pattern:tag". For example:
- Single file: "*foo.go:tag1"
- Multiple files: "*foo.go:tag1,*foo2.go:tag2"`
FlagFiletagsDoc = `Comma-separated list of file names and build tags using the form "pattern:tag". For example:
- Single pattern: "*foo.go:tag1"
- Multiple patterns: "*foo.go:tag1,*foo2.go:tag2"`
)

var Analyzer = &analysis.Analyzer{
Expand Down
14 changes: 7 additions & 7 deletions filebuildtag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ func Test_Lint(t *testing.T) {
pattern string
flags string
}{
"buildtag - std lib linter's original test file": {
pattern: "buildtag",
flags: "*:foo",
},
"filebuildtag - wildcard match": {
"match files with a wildcard": {
pattern: "filebuildtag_wildcard",
flags: "*tag1_suff.go:tag1,*tag2_suff.go:tag2",
},
"filebuildtag - exact match": {
"match exact file names": {
pattern: "filebuildtag_exact",
flags: "pref_tag1_suff.go:tag1,pref_tag2_suff.go:tag2",
},
"filebuildtag - no tags": {
"match exact file name without tags": {
pattern: "filebuildtag_exact",
flags: "",
},
"the std lib linter's original test file must have the foo tag": {
pattern: "buildtag",
flags: "*:foo",
},
}
for name, tt := range testCases {
t.Run(name, func(t *testing.T) {
Expand Down
17 changes: 14 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
module github.com/aziule/filebuildtag

go 1.13
go 1.22.0

toolchain go1.23.2

require (
github.com/google/go-licenses v0.0.0-20200602185517-f29a4c695c3d // indirect
github.com/stretchr/testify v1.6.1
golang.org/x/tools v0.0.0-20200710042808-f1c4188a97a1
golang.org/x/tools v0.27.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/mod v0.22.0 // indirect
golang.org/x/sync v0.9.0 // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
Loading

0 comments on commit a1b08cf

Please sign in to comment.