5
5
package loopclosure_test
6
6
7
7
import (
8
+ "os"
9
+ "path/filepath"
8
10
"testing"
9
11
12
+ "golang.org/x/tools/go/analysis"
10
13
"golang.org/x/tools/go/analysis/analysistest"
11
14
"golang.org/x/tools/go/analysis/passes/loopclosure"
15
+ "golang.org/x/tools/internal/testenv"
12
16
"golang.org/x/tools/internal/typeparams"
17
+ "golang.org/x/tools/txtar"
13
18
)
14
19
15
20
func Test (t * testing.T ) {
@@ -20,3 +25,43 @@ func Test(t *testing.T) {
20
25
}
21
26
analysistest .Run (t , testdata , loopclosure .Analyzer , tests ... )
22
27
}
28
+
29
+ func TestVersions22 (t * testing.T ) {
30
+ testenv .NeedsGo1Point (t , 22 )
31
+
32
+ testfile := filepath .Join (analysistest .TestData (), "src" , "versions" , "go22.txtar" )
33
+ runTxtarFile (t , testfile , loopclosure .Analyzer , "golang.org/fake/versions" )
34
+ }
35
+
36
+ func TestVersions18 (t * testing.T ) {
37
+ testenv .NeedsGo1Point (t , 18 )
38
+
39
+ testfile := filepath .Join (analysistest .TestData (), "src" , "versions" , "go18.txtar" )
40
+ runTxtarFile (t , testfile , loopclosure .Analyzer , "golang.org/fake/versions" )
41
+ }
42
+
43
+ // runTxtarFile unpacks a txtar archive to a directory, and runs
44
+ // analyzer on the given patterns.
45
+ //
46
+ // This is compatible with a go.mod file.
47
+ //
48
+ // TODO(taking): Consider unifying with analysistest.
49
+ func runTxtarFile (t * testing.T , path string , analyzer * analysis.Analyzer , patterns ... string ) {
50
+ ar , err := txtar .ParseFile (path )
51
+ if err != nil {
52
+ t .Fatal (err )
53
+ }
54
+
55
+ dir := t .TempDir ()
56
+ for _ , file := range ar .Files {
57
+ name , content := file .Name , file .Data
58
+
59
+ filename := filepath .Join (dir , name )
60
+ os .MkdirAll (filepath .Dir (filename ), 0777 ) // ignore error
61
+ if err := os .WriteFile (filename , content , 0666 ); err != nil {
62
+ t .Fatal (err )
63
+ }
64
+ }
65
+
66
+ analysistest .Run (t , dir , analyzer , patterns ... )
67
+ }
0 commit comments