https://github.com/sivchari/tenv
tenv detects environment variable not using t.Setenv
https://github.com/sivchari/tenv
go golang govet linter
Last synced: about 1 year ago
JSON representation
tenv detects environment variable not using t.Setenv
- Host: GitHub
- URL: https://github.com/sivchari/tenv
- Owner: sivchari
- License: mit
- Created: 2021-09-05T16:24:26.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2024-10-28T05:54:05.000Z (over 1 year ago)
- Last Synced: 2024-10-30T08:17:46.894Z (over 1 year ago)
- Topics: go, golang, govet, linter
- Language: Go
- Homepage:
- Size: 284 KB
- Stars: 23
- Watchers: 3
- Forks: 7
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# tenv

[](https://github.com/sivchari/tenv/actions/workflows/test.yaml)
tenv is analyzer that detects using os.Setenv instead of t.Setenv since Go1.17
## Instruction
```sh
go install github.com/sivchari/tenv/cmd/tenv@latest
```
## Usage
```go
package main
import (
"fmt"
"os"
"testing"
)
func TestMain(t *testing.T) {
fmt.Println(os.Getenv("GO"))
os.Setenv("GO", "HACKING GOPHER")
}
func TestMain2(t *testing.T) {
fmt.Println(os.Getenv("GO"))
}
func helper() {
os.Setenv("GO", "HACKING GOPHER")
}
```
```console
go vet -vettool=$(which tenv) ./...
# a
./main_test.go:11:2: os.Setenv() can be replaced by `t.Setenv()` in TestMain
```
### option
The option `all` will run against whole test files (`_test.go`) regardless of method/function signatures.
By default, only methods that take `*testing.T`, `*testing.B`, and `testing.TB` as arguments are checked.
```go
package main
import (
"fmt"
"os"
"testing"
)
func TestMain(t *testing.T) {
fmt.Println(os.Getenv("GO"))
os.Setenv("GO", "HACKING GOPHER")
}
func TestMain2(t *testing.T) {
fmt.Println(os.Getenv("GO"))
}
func helper() {
os.Setenv("GO", "HACKING GOPHER")
}
```
```console
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
### CircleCI
```yaml
- run:
name: install tenv
command: go install github.com/sivchari/tenv@latest
- run:
name: run tenv
command: go vet -vettool=`which tenv` ./...
```
### GitHub Actions
```yaml
- name: install tenv
run: go install github.com/sivchari/tenv@latest
- name: run tenv
run: go vet -vettool=`which tenv` ./...
```