github.com/yoheimuta/protolint@v0.49.8-0.20240515023657-4ecaebb7575d/internal/addon/rules/importsSortedRule_test.go (about) 1 package rules_test 2 3 import ( 4 "reflect" 5 "testing" 6 7 "github.com/yoheimuta/go-protoparser/v4/parser/meta" 8 9 "github.com/yoheimuta/protolint/internal/setting_test" 10 "github.com/yoheimuta/protolint/internal/util_test" 11 12 "github.com/yoheimuta/protolint/internal/linter/file" 13 14 "github.com/yoheimuta/protolint/internal/addon/rules" 15 "github.com/yoheimuta/protolint/linter/report" 16 "github.com/yoheimuta/protolint/linter/rule" 17 ) 18 19 func testImportSortedProtoPath(name string) string { 20 return setting_test.TestDataPath("rules", "importsSorted", name) 21 } 22 23 func TestImportsSortedRule_Apply(t *testing.T) { 24 tests := []struct { 25 name string 26 inputFilename string 27 wantFailures []report.Failure 28 wantExistErr bool 29 }{ 30 { 31 name: "no failures for proto with sorted imports", 32 inputFilename: "sorted.proto", 33 }, 34 { 35 name: "no failures for proto with sorted imports separated by a newline", 36 inputFilename: "sortedWithNewline.proto", 37 }, 38 { 39 name: "failures for proto with not sorted imports", 40 inputFilename: "notSorted.proto", 41 wantFailures: []report.Failure{ 42 report.Failuref( 43 meta.Position{ 44 Filename: testImportSortedProtoPath("notSorted.proto"), 45 Offset: 20, 46 Line: 3, 47 Column: 1, 48 }, 49 "IMPORTS_SORTED", 50 `Imports are not sorted.`, 51 ), 52 report.Failuref( 53 meta.Position{ 54 Filename: testImportSortedProtoPath("notSorted.proto"), 55 Offset: 47, 56 Line: 4, 57 Column: 1, 58 }, 59 "IMPORTS_SORTED", 60 `Imports are not sorted.`, 61 ), 62 }, 63 }, 64 { 65 name: "failures for proto with not sorted imports separated by a newline", 66 inputFilename: "notSortedWithNewline.proto", 67 wantFailures: []report.Failure{ 68 report.Failuref( 69 meta.Position{ 70 Filename: testImportSortedProtoPath("notSortedWithNewline.proto"), 71 Offset: 20, 72 Line: 3, 73 Column: 1, 74 }, 75 "IMPORTS_SORTED", 76 `Imports are not sorted.`, 77 ), 78 report.Failuref( 79 meta.Position{ 80 Filename: testImportSortedProtoPath("notSortedWithNewline.proto"), 81 Offset: 42, 82 Line: 4, 83 Column: 1, 84 }, 85 "IMPORTS_SORTED", 86 `Imports are not sorted.`, 87 ), 88 report.Failuref( 89 meta.Position{ 90 Filename: testImportSortedProtoPath("notSortedWithNewline.proto"), 91 Offset: 151, 92 Line: 9, 93 Column: 1, 94 }, 95 "IMPORTS_SORTED", 96 `Imports are not sorted.`, 97 ), 98 report.Failuref( 99 meta.Position{ 100 Filename: testImportSortedProtoPath("notSortedWithNewline.proto"), 101 Offset: 190, 102 Line: 10, 103 Column: 1, 104 }, 105 "IMPORTS_SORTED", 106 `Imports are not sorted.`, 107 ), 108 }, 109 }, 110 } 111 112 for _, test := range tests { 113 test := test 114 t.Run(test.name, func(t *testing.T) { 115 rule := rules.NewImportsSortedRule( 116 rule.SeverityError, 117 false, 118 ) 119 120 protoPath := testImportSortedProtoPath(test.inputFilename) 121 proto, err := file.NewProtoFile(protoPath, protoPath).Parse(false) 122 if err != nil { 123 t.Errorf(err.Error()) 124 return 125 } 126 127 got, err := rule.Apply(proto) 128 if test.wantExistErr { 129 if err == nil { 130 t.Errorf("got err nil, but want err") 131 } 132 return 133 } 134 if err != nil { 135 t.Errorf("got err %v, but want nil", err) 136 return 137 } 138 139 if !reflect.DeepEqual(got, test.wantFailures) { 140 t.Errorf("got %v, but want %v", got, test.wantFailures) 141 } 142 }) 143 } 144 } 145 146 func newTestImportsSortedData( 147 fileName string, 148 ) (util_test.TestData, error) { 149 return util_test.NewTestData(testImportSortedProtoPath(fileName)) 150 } 151 152 func TestImportsSortedRule_Apply_fix(t *testing.T) { 153 tests := []struct { 154 name string 155 inputFilename string 156 wantFilename string 157 }{ 158 { 159 name: "no fix for proto with sorted imports", 160 inputFilename: "sorted.proto", 161 wantFilename: "sorted.proto", 162 }, 163 { 164 name: "no fix for proto with sorted imports separated by a newline", 165 inputFilename: "sortedWithNewline.proto", 166 wantFilename: "sortedWithNewline.proto", 167 }, 168 { 169 name: "fix for proto with not sorted imports", 170 inputFilename: "notSorted.proto", 171 wantFilename: "sorted.proto", 172 }, 173 { 174 name: "fix for proto with sorted imports separated by a newline", 175 inputFilename: "notSortedWithNewline.proto", 176 wantFilename: "sortedWithNewline.proto", 177 }, 178 } 179 180 for _, test := range tests { 181 test := test 182 t.Run(test.name, func(t *testing.T) { 183 rule := rules.NewImportsSortedRule( 184 rule.SeverityError, 185 true, 186 ) 187 188 input, err := newTestImportsSortedData(test.inputFilename) 189 if err != nil { 190 t.Errorf("got err %v", err) 191 return 192 } 193 194 want, err := newTestImportsSortedData(test.wantFilename) 195 if err != nil { 196 t.Errorf("got err %v", err) 197 return 198 } 199 200 proto, err := file.NewProtoFile(input.FilePath, input.FilePath).Parse(false) 201 if err != nil { 202 t.Errorf(err.Error()) 203 return 204 } 205 206 _, err = rule.Apply(proto) 207 if err != nil { 208 t.Errorf("got err %v, but want nil", err) 209 return 210 } 211 212 got, err := input.Data() 213 if !reflect.DeepEqual(got, want.OriginData) { 214 t.Errorf( 215 "got %s(%v), but want %s(%v)", 216 string(got), got, 217 string(want.OriginData), want.OriginData, 218 ) 219 } 220 221 err = input.Restore() 222 if err != nil { 223 t.Errorf("got err %v", err) 224 } 225 }) 226 } 227 }