diff --git a/.gitignore b/.gitignore index 66fd13c..8347010 100644 --- a/.gitignore +++ b/.gitignore @@ -11,5 +11,7 @@ # Output of the go coverage tool, specifically when used with LiteIDE *.out +.idea + # Dependency directories (remove the comment below to include it) # vendor/ diff --git a/README.md b/README.md index 1204089..f8d2723 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,7 @@ # tenv + +![tenv Gopher](./tenv.png "Gopher") + tenv is analyzer that detects using os.Setenv instead of t.Setenv since Go1.17 [![test_and_lint](https://github.com/sivchari/tenv/actions/workflows/workflows.yml/badge.svg?branch=main)](https://github.com/sivchari/tenv/actions/workflows/workflows.yml) @@ -12,79 +15,70 @@ go install github.com/sivchari/tenv/cmd/tenv ## Usage ```go -package sandbox_test +package main import ( - "os" - "testing" + "fmt" + "os" + "testing" ) -var ( - e = os.Setenv("a", "b") - _ = e -) +func TestMain(t *testing.T) { + fmt.Println(os.Getenv("GO")) + os.Setenv("GO", "HACKING GOPHER") +} -func setup() { - os.Setenv("a", "b") - err := os.Setenv("a", "b") - if err != nil { - _ = err - } +func TestMain2(t *testing.T) { + fmt.Println(os.Getenv("GO")) } -func TestF(t *testing.T) { - setup() - os.Setenv("a", "b") - if err := os.Setenv("a", "b"); err != nil { - _ = err - } +func helper() { + os.Setenv("GO", "HACKING GOPHER") } ``` -### fish - ```console -go vet -vettool=(which tenv) sandbox_test.go - -# command-line-arguments -./sandbox_test.go:9:2: variable e is not using t.Setenv -./sandbox_test.go:14:2: func setup is not using t.Setenv -./sandbox_test.go:15:2: func setup is not using t.Setenv -./sandbox_test.go:23:2: func TestF is not using t.Setenv -./sandbox_test.go:24:2: func TestF is not using t.Setenv -``` - -### bash +go vet -vettool=(which tenv) ./... -```console -$ go vet -vettool=`which tenv` main.go - -# command-line-arguments -./sandbox_test.go:9:2: variable e is not using t.Setenv -./sandbox_test.go:14:2: func setup is not using t.Setenv -./sandbox_test.go:15:2: func setup is not using t.Setenv -./sandbox_test.go:23:2: func TestF is not using t.Setenv -./sandbox_test.go:24:2: func TestF is not using t.Setenv +# a +./main_test.go:11:2: os.Setenv() can be replaced by `t.Setenv()` in TestMain ``` ### option -t.Setenv can use since Go1.17. -This linter diagnostics, if Go version is since 1.17. -But, if you wanna exec this linter in prior Go1.17, you can use it that you set `-tenv.f` flag. +The option `all` will run against whole test files (`_test.go`) regardless of method/function signatures. -e.g. +By default, only methods that take `*testing.T`, `*testing.B`, and `testing.TB` as arguments are checked. -### fish +```go +package main -```console -go vet -vettool=(which tenv) -tenv.f sandbox_test.go -``` +import ( + "fmt" + "os" + "testing" +) + +func TestMain(t *testing.T) { + fmt.Println(os.Getenv("GO")) + os.Setenv("GO", "HACKING GOPHER") +} -### bash +func TestMain2(t *testing.T) { + fmt.Println(os.Getenv("GO")) +} + +func helper() { + os.Setenv("GO", "HACKING GOPHER") +} +``` ```console -go vet -vettool=`which tenv` -tenv.f main.go +go vet -vettool=(which tenv) -tenv.all ./... + +# a +./main_test.go:11:2: os.Setenv() can be replaced by `t.Setenv()` in TestMain +./main_test.go:19:2: os.Setenv() can be replaced by `testing.Setenv()` in helper ``` ## CI @@ -93,20 +87,20 @@ go vet -vettool=`which tenv` -tenv.f main.go ```yaml - run: - name: Install tenv + name: install tenv command: go install github.com/sivchari/tenv - run: - name: Run tenv + name: run tenv command: go vet -vettool=`which tenv` ./... ``` ### GitHub Actions ```yaml -- name: Install tenv +- name: install tenv run: go install github.com/sivchari/tenv -- name: Run tenv +- name: run tenv run: go vet -vettool=`which tenv` ./... ``` diff --git a/tenv.png b/tenv.png new file mode 100644 index 0000000..96dc967 Binary files /dev/null and b/tenv.png differ