github.com/nya3jp/tast@v0.0.0-20230601000426-85c8e4d83a9b/src/go.chromium.org/tast/core/cmd/tast-lint/internal/check/import_order_test.go (about) 1 // Copyright 2018 The ChromiumOS Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 package check 6 7 import ( 8 "go/ast" 9 "go/token" 10 "testing" 11 ) 12 13 func TestImportOrderGood(t *testing.T) { 14 const code = `package main 15 16 import ( 17 "fmt" 18 19 "github.com/godbus/dbus/v5" 20 21 "go.chromium.org/tast/core/errors" 22 "go.chromium.org/tast/errors" 23 ) 24 25 func Foo() { 26 fmt.Print("") 27 dbus.New() 28 errors.New() 29 } 30 ` 31 32 // The import order is good, so no issue. 33 var expects []string 34 issues := ImportOrder("testfile.go", []byte(code)) 35 verifyIssues(t, issues, expects) 36 } 37 38 func TestImportOrderGroup(t *testing.T) { 39 const ( 40 code = `package main 41 42 import ( 43 "fmt" 44 "github.com/godbus/dbus/v5" 45 "go.chromium.org/tast/core/errors" 46 "go.chromium.org/tast/errors" 47 ) 48 49 func Foo() { 50 fmt.Print("") 51 dbus.New() 52 errors.New() 53 } 54 ` 55 diff = `@@ -2,7 +2,9 @@ 56 57 import ( 58 "fmt" 59 + 60 "github.com/godbus/dbus/v5" 61 + 62 "go.chromium.org/tast/core/errors" 63 "go.chromium.org/tast/errors" 64 ) 65 ` 66 ) 67 68 expects := []string{ 69 "testfile.go: Import should be grouped into standard packages, third-party packages and chromiumos packages in this order separated by empty lines.\nApply the following patch to fix:\n" + diff, 70 } 71 issues := ImportOrder("testfile.go", []byte(code)) 72 verifyIssues(t, issues, expects) 73 } 74 75 func TestImportOrderGroupOrder(t *testing.T) { 76 const code = `package main 77 78 import ( 79 "fmt" 80 81 "go.chromium.org/tast/core/errors" 82 83 "github.com/godbus/dbus/v5" 84 ) 85 86 func Foo() { 87 fmt.Print("") 88 dbus.New() 89 errors.New() 90 } 91 ` 92 93 const diff = `@@ -3,9 +3,9 @@ 94 import ( 95 "fmt" 96 97 - "go.chromium.org/tast/core/errors" 98 - 99 "github.com/godbus/dbus/v5" 100 + 101 + "go.chromium.org/tast/core/errors" 102 ) 103 104 func Foo() { 105 ` 106 107 expects := []string{ 108 "testfile.go: Import should be grouped into standard packages, third-party packages and chromiumos packages in this order separated by empty lines.\nApply the following patch to fix:\n" + diff, 109 } 110 issues := ImportOrder("testfile.go", []byte(code)) 111 verifyIssues(t, issues, expects) 112 } 113 114 func TestImportOrderCommentInImportBlock(t *testing.T) { 115 const code = `package main 116 117 import ( 118 "fmt" 119 120 // some comment 121 "go.chromium.org/tast/core/errors" 122 ) 123 124 func Foo() { 125 fmt.Println(errors.New("foo")) 126 } 127 ` 128 129 // The import order is good, so no issue. 130 var expects []string 131 issues := ImportOrder("testfile.go", []byte(code)) 132 verifyIssues(t, issues, expects) 133 } 134 135 func TestAutoFixImportOrder(t *testing.T) { 136 const filename1, filename2 = "testfile1.go", "testfile2.go" 137 files := make(map[string]string) 138 files[filename1] = `// Package main 139 package main 140 141 import ( 142 "fmt" 143 "github.com/godbus/dbus/v5" 144 "go.chromium.org/tast/core/errors" 145 ) 146 147 // Foo does foo 148 func Foo() { 149 fmt.Print("") 150 dbus.New() 151 errors.New() 152 } 153 ` 154 files[filename2] = `// Package main 155 package main 156 157 import ( 158 "fmt" 159 160 "go.chromium.org/tast/core/errors" 161 162 "github.com/godbus/dbus/v5" 163 ) 164 165 // Foo does foo 166 func Foo() { 167 fmt.Print("") 168 dbus.New() 169 errors.New() 170 } 171 ` 172 expects := make(map[string]string) 173 expects[filename1] = `// Package main 174 package main 175 176 import ( 177 "fmt" 178 179 "github.com/godbus/dbus/v5" 180 181 "go.chromium.org/tast/core/errors" 182 ) 183 184 // Foo does foo 185 func Foo() { 186 fmt.Print("") 187 dbus.New() 188 errors.New() 189 } 190 ` 191 expects[filename2] = `// Package main 192 package main 193 194 import ( 195 "fmt" 196 197 "github.com/godbus/dbus/v5" 198 199 "go.chromium.org/tast/core/errors" 200 ) 201 202 // Foo does foo 203 func Foo() { 204 fmt.Print("") 205 dbus.New() 206 errors.New() 207 } 208 ` 209 verifyAutoFix(t, importOrderWrapper, files, expects) 210 } 211 212 func importOrderWrapper(fs *token.FileSet, f *ast.File, fix bool) []*Issue { 213 newf, err := ImportOrderAutoFix(fs, f) 214 if err != nil { 215 return nil 216 } 217 *f = *newf 218 return nil 219 }