Skip to content

Commit

Permalink
fix readme
Browse files Browse the repository at this point in the history
  • Loading branch information
sivchari committed Oct 1, 2021
1 parent d8ea6ba commit f121264
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 55 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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/
104 changes: 49 additions & 55 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
Expand All @@ -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` ./...
```
Binary file added tenv.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f121264

Please sign in to comment.