github.com/bgpat/reviewdog@v0.0.0-20230909064023-077e44ca1f66/parser/sarif_test.go (about) 1 package parser 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "fmt" 7 "reflect" 8 "strings" 9 "testing" 10 11 "github.com/bgpat/reviewdog/service/serviceutil" 12 "google.golang.org/protobuf/encoding/protojson" 13 ) 14 15 func TestExampleSarifParser(t *testing.T) { 16 p := NewSarifParser() 17 for i, fixture := range fixtures { 18 diagnostics, err := p.Parse(strings.NewReader(fixture[0])) 19 if err != nil { 20 panic(err) 21 } 22 for _, d := range diagnostics { 23 rdjson, _ := protojson.MarshalOptions{Indent: " "}.Marshal(d) 24 var actualJson interface{} 25 var expectJson interface{} 26 json.Unmarshal([]byte(rdjson), &actualJson) 27 json.Unmarshal([]byte(fixture[1]), &expectJson) 28 if !reflect.DeepEqual(actualJson, expectJson) { 29 var out bytes.Buffer 30 json.Indent(&out, rdjson, "", "\t") 31 actual := out.String() 32 expect := fixture[1] 33 t.Errorf("actual(%v):\n%v\n---\nexpect(%v):\n%v", i, actual, i, expect) 34 } 35 } 36 } 37 } 38 39 func basedir() string { 40 root, err := serviceutil.GetGitRoot() 41 if err != nil { 42 panic(err) 43 } 44 return root 45 } 46 47 var fixtures = [][]string{{ 48 fmt.Sprintf(`{ 49 "$schema": "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json", 50 "version": "2.1.0", 51 "runs": [ 52 { 53 "originalUriBaseIds": { 54 "SRCROOT": { 55 "uri": "file://%s" 56 } 57 }, 58 "results": [ 59 { 60 "level": "warning", 61 "locations": [ 62 { 63 "physicalLocation": { 64 "artifactLocation": { 65 "uri": "src/MyClass.kt", 66 "uriBaseId": "SRCROOT" 67 }, 68 "region": { 69 "startLine": 10, 70 "startColumn": 5, 71 "endLine": 12, 72 "endColumn": 15 73 } 74 } 75 } 76 ], 77 "message": { 78 "text": "result message" 79 }, 80 "ruleId": "rule_id" 81 } 82 ], 83 "tool": { 84 "driver": { 85 "informationUri": "https://example.com", 86 "name": "driver_name", 87 "rules": [ 88 { 89 "helpUri": "https://example.com", 90 "id": "rule_id", 91 "name": "My Rule", 92 "shortDescription": { 93 "text": "Rule description" 94 } 95 } 96 ] 97 } 98 } 99 } 100 ] 101 }`, basedir()), `{ 102 "message": "result message", 103 "location": { 104 "path": "src/MyClass.kt", 105 "range": { 106 "start": { 107 "line": 10, 108 "column": 5 109 }, 110 "end": { 111 "line": 12, 112 "column": 15 113 } 114 } 115 }, 116 "severity": "WARNING", 117 "source": { 118 "name": "driver_name", 119 "url": "https://example.com" 120 }, 121 "code": { 122 "value": "rule_id", 123 "url": "https://example.com" 124 }, 125 "originalOutput": "src/MyClass.kt:10:5: warning: result message (rule_id)" 126 }`}, 127 {`{ 128 "runs": [ 129 { 130 "originalUriBaseIds": { 131 "SRCROOT": { 132 "description": "uri deleted root" 133 } 134 }, 135 "results": [ 136 { 137 "locations": [ 138 { 139 "physicalLocation": { 140 "artifactLocation": { 141 "uri": "src/MyClass.kt", 142 "uriBaseId": "SRCROOT" 143 }, 144 "region": { 145 "startLine": 10 146 } 147 } 148 } 149 ], 150 "message": { 151 "text": "message" 152 }, 153 "ruleId": "rule_id" 154 } 155 ], 156 "tool": { 157 "driver": { 158 "name": "driver_name", 159 "rules": [ 160 { 161 "id": "rule_id", 162 "defaultConfiguration": { 163 "level": "error" 164 } 165 } 166 ] 167 } 168 } 169 } 170 ] 171 }`, `{ 172 "message": "message", 173 "location": { 174 "path": "src/MyClass.kt", 175 "range": { 176 "start": { 177 "line": 10 178 } 179 } 180 }, 181 "severity": "ERROR", 182 "source": { 183 "name": "driver_name" 184 }, 185 "code": { 186 "value": "rule_id" 187 }, 188 "originalOutput": "src/MyClass.kt:10:1: error: message (rule_id)" 189 }`}, 190 {`{ 191 "runs": [ 192 { 193 "results": [ 194 { 195 "locations": [ 196 { 197 "physicalLocation": { 198 "artifactLocation": { 199 "uri": "src/MyClass.kt" 200 }, 201 "region": { 202 "startLine": 10 203 } 204 } 205 } 206 ], 207 "fixes": [ 208 { 209 "artifactChanges": [ 210 { 211 "artifactLocation": { 212 "uri": "src/MyClass.kt" 213 }, 214 "replacements": [ 215 { 216 "deletedRegion": { 217 "startLine": 10, 218 "startColumn": 1, 219 "endColumn": 1 220 }, 221 "insertedContent": { 222 "text": "// " 223 } 224 } 225 ] 226 } 227 ] 228 } 229 ], 230 "message": { 231 "markdown": "message" 232 }, 233 "ruleId": "rule_id" 234 } 235 ], 236 "tool": { 237 "driver": { 238 "name": "driver_name" 239 } 240 } 241 } 242 ] 243 }`, `{ 244 "message": "message", 245 "location": { 246 "path": "src/MyClass.kt", 247 "range": { 248 "start": { 249 "line": 10 250 } 251 } 252 }, 253 "source": { 254 "name": "driver_name" 255 }, 256 "code": { 257 "value": "rule_id" 258 }, 259 "suggestions": [ 260 { 261 "range": { 262 "start": { 263 "line": 10, 264 "column": 1 265 }, 266 "end": { 267 "line": 10, 268 "column": 1 269 } 270 }, 271 "text": "// " 272 } 273 ], 274 "originalOutput": "src/MyClass.kt:10:1: : message (rule_id)" 275 }`}, 276 }