1
1
# tenv
2
+
3
+ ![ tenv Gopher] ( ./tenv.png " Gopher ")
4
+
2
5
tenv is analyzer that detects using os.Setenv instead of t.Setenv since Go1.17
3
6
4
7
[ ![ 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
12
15
## Usage
13
16
14
17
``` go
15
- package sandbox_test
18
+ package main
16
19
17
20
import (
18
- " os"
19
- " testing"
21
+ " fmt"
22
+ " os"
23
+ " testing"
20
24
)
21
25
22
- var (
23
- e = os.Setenv ( " a " , " b " )
24
- _ = e
25
- )
26
+ func TestMain ( t * testing . T ) {
27
+ fmt. Println ( os.Getenv ( " GO " ) )
28
+ os. Setenv ( " GO " , " HACKING GOPHER " )
29
+ }
26
30
27
- func setup () {
28
- os.Setenv (" a" , " b" )
29
- err := os.Setenv (" a" , " b" )
30
- if err != nil {
31
- _ = err
32
- }
31
+ func TestMain2 (t *testing .T ) {
32
+ fmt.Println (os.Getenv (" GO" ))
33
33
}
34
34
35
- func TestF (t *testing .T ) {
36
- setup ()
37
- os.Setenv (" a" , " b" )
38
- if err := os.Setenv (" a" , " b" ); err != nil {
39
- _ = err
40
- }
35
+ func helper () {
36
+ os.Setenv (" GO" , " HACKING GOPHER" )
41
37
}
42
38
```
43
39
44
- ### fish
45
-
46
40
``` console
47
- go vet -vettool=(which tenv) sandbox_test.go
48
-
49
- # command-line-arguments
50
- ./sandbox_test.go:9:2: variable e is not using t.Setenv
51
- ./sandbox_test.go:14:2: func setup is not using t.Setenv
52
- ./sandbox_test.go:15:2: func setup is not using t.Setenv
53
- ./sandbox_test.go:23:2: func TestF is not using t.Setenv
54
- ./sandbox_test.go:24:2: func TestF is not using t.Setenv
55
- ```
56
-
57
- ### bash
41
+ go vet -vettool=(which tenv) ./...
58
42
59
- ``` console
60
- $ go vet -vettool=` which tenv` main.go
61
-
62
- # command-line-arguments
63
- ./sandbox_test.go:9:2: variable e is not using t.Setenv
64
- ./sandbox_test.go:14:2: func setup is not using t.Setenv
65
- ./sandbox_test.go:15:2: func setup is not using t.Setenv
66
- ./sandbox_test.go:23:2: func TestF is not using t.Setenv
67
- ./sandbox_test.go:24:2: func TestF is not using t.Setenv
43
+ # a
44
+ ./main_test.go:11:2: os.Setenv() can be replaced by `t.Setenv()` in TestMain
68
45
```
69
46
70
47
### option
71
48
72
- t.Setenv can use since Go1.17.
73
- This linter diagnostics, if Go version is since 1.17.
74
- But, if you wanna exec this linter in prior Go1.17, you can use it that you set ` -tenv.f ` flag.
49
+ The option ` all ` will run against whole test files (` _test.go ` ) regardless of method/function signatures.
75
50
76
- e.g .
51
+ By default, only methods that take ` *testing.T ` , ` *testing.B ` , and ` testing.TB ` as arguments are checked .
77
52
78
- ### fish
53
+ ``` go
54
+ package main
79
55
80
- ``` console
81
- go vet -vettool=(which tenv) -tenv.f sandbox_test.go
82
- ```
56
+ import (
57
+ " fmt"
58
+ " os"
59
+ " testing"
60
+ )
61
+
62
+ func TestMain (t *testing .T ) {
63
+ fmt.Println (os.Getenv (" GO" ))
64
+ os.Setenv (" GO" , " HACKING GOPHER" )
65
+ }
83
66
84
- ### bash
67
+ func TestMain2 (t *testing .T ) {
68
+ fmt.Println (os.Getenv (" GO" ))
69
+ }
70
+
71
+ func helper () {
72
+ os.Setenv (" GO" , " HACKING GOPHER" )
73
+ }
74
+ ```
85
75
86
76
``` console
87
- go vet -vettool=`which tenv` -tenv.f main.go
77
+ go vet -vettool=(which tenv) -tenv.all ./...
78
+
79
+ # a
80
+ ./main_test.go:11:2: os.Setenv() can be replaced by `t.Setenv()` in TestMain
81
+ ./main_test.go:19:2: os.Setenv() can be replaced by `testing.Setenv()` in helper
88
82
```
89
83
90
84
## CI
@@ -93,20 +87,20 @@ go vet -vettool=`which tenv` -tenv.f main.go
93
87
94
88
``` yaml
95
89
- run :
96
- name : Install tenv
90
+ name : install tenv
97
91
command : go install github.com/sivchari/tenv
98
92
99
93
- run :
100
- name : Run tenv
94
+ name : run tenv
101
95
command : go vet -vettool=`which tenv` ./...
102
96
` ` `
103
97
104
98
### GitHub Actions
105
99
106
100
` ` ` yaml
107
- - name : Install tenv
101
+ - name : install tenv
108
102
run : go install github.com/sivchari/tenv
109
103
110
- - name : Run tenv
104
+ - name : run tenv
111
105
run : go vet -vettool=`which tenv` ./...
112
106
` ` `
0 commit comments