@@ -311,3 +311,187 @@ func TestSeverity_caseSensitive(t *testing.T) {
311311
312312 assert .Equal (t , expectedCases , resultingCases )
313313}
314+
315+ func TestSeverity_transform (t * testing.T ) {
316+ lineCache := fsutils .NewLineCache (fsutils .NewFileCache ())
317+ files := fsutils .NewFiles (lineCache , "" )
318+
319+ testCases := []struct {
320+ desc string
321+ opts SeverityOptions
322+ issue * result.Issue
323+ expected * result.Issue
324+ }{
325+ {
326+ desc : "apply severity from rule" ,
327+ opts : SeverityOptions {
328+ Default : "error" ,
329+ Rules : []SeverityRule {
330+ {
331+ Severity : "info" ,
332+ BaseRule : BaseRule {
333+ Linters : []string {"linter1" },
334+ },
335+ },
336+ },
337+ },
338+ issue : & result.Issue {
339+ Text : "This is a report" ,
340+ FromLinter : "linter1" ,
341+ },
342+ expected : & result.Issue {
343+ Text : "This is a report" ,
344+ FromLinter : "linter1" ,
345+ Severity : "info" ,
346+ },
347+ },
348+ {
349+ desc : "apply severity from default" ,
350+ opts : SeverityOptions {
351+ Default : "error" ,
352+ Rules : []SeverityRule {
353+ {
354+ Severity : "info" ,
355+ BaseRule : BaseRule {
356+ Linters : []string {"linter1" },
357+ },
358+ },
359+ },
360+ },
361+ issue : & result.Issue {
362+ Text : "This is a report" ,
363+ FromLinter : "linter2" ,
364+ },
365+ expected : & result.Issue {
366+ Text : "This is a report" ,
367+ FromLinter : "linter2" ,
368+ Severity : "error" ,
369+ },
370+ },
371+ {
372+ desc : "severity from rule override severity from linter" ,
373+ opts : SeverityOptions {
374+ Default : "error" ,
375+ Rules : []SeverityRule {
376+ {
377+ Severity : "info" ,
378+ BaseRule : BaseRule {
379+ Linters : []string {"linter1" },
380+ },
381+ },
382+ },
383+ },
384+ issue : & result.Issue {
385+ Text : "This is a report" ,
386+ FromLinter : "linter1" ,
387+ Severity : "huge" ,
388+ },
389+ expected : & result.Issue {
390+ Text : "This is a report" ,
391+ FromLinter : "linter1" ,
392+ Severity : "info" ,
393+ },
394+ },
395+ {
396+ desc : "severity from default override severity from linter" ,
397+ opts : SeverityOptions {
398+ Default : "error" ,
399+ Rules : []SeverityRule {
400+ {
401+ Severity : "info" ,
402+ BaseRule : BaseRule {
403+ Linters : []string {"linter1" },
404+ },
405+ },
406+ },
407+ },
408+ issue : & result.Issue {
409+ Text : "This is a report" ,
410+ FromLinter : "linter2" ,
411+ Severity : "huge" ,
412+ },
413+ expected : & result.Issue {
414+ Text : "This is a report" ,
415+ FromLinter : "linter2" ,
416+ Severity : "error" ,
417+ },
418+ },
419+ {
420+ desc : "keep severity from linter as rule" ,
421+ opts : SeverityOptions {
422+ Default : "error" ,
423+ Rules : []SeverityRule {
424+ {
425+ Severity : "@" ,
426+ BaseRule : BaseRule {
427+ Linters : []string {"linter1" },
428+ },
429+ },
430+ },
431+ },
432+ issue : & result.Issue {
433+ Text : "This is a report" ,
434+ FromLinter : "linter1" ,
435+ Severity : "huge" ,
436+ },
437+ expected : & result.Issue {
438+ Text : "This is a report" ,
439+ FromLinter : "linter1" ,
440+ Severity : "huge" ,
441+ },
442+ },
443+ {
444+ desc : "keep severity from linter as default" ,
445+ opts : SeverityOptions {
446+ Default : "@" ,
447+ Rules : []SeverityRule {
448+ {
449+ Severity : "info" ,
450+ BaseRule : BaseRule {
451+ Linters : []string {"linter1" },
452+ },
453+ },
454+ },
455+ },
456+ issue : & result.Issue {
457+ Text : "This is a report" ,
458+ FromLinter : "linter2" ,
459+ Severity : "huge" ,
460+ },
461+ expected : & result.Issue {
462+ Text : "This is a report" ,
463+ FromLinter : "linter2" ,
464+ Severity : "huge" ,
465+ },
466+ },
467+ {
468+ desc : "keep severity from linter as default (without rule)" ,
469+ opts : SeverityOptions {
470+ Default : "@" ,
471+ },
472+ issue : & result.Issue {
473+ Text : "This is a report" ,
474+ FromLinter : "linter2" ,
475+ Severity : "huge" ,
476+ },
477+ expected : & result.Issue {
478+ Text : "This is a report" ,
479+ FromLinter : "linter2" ,
480+ Severity : "huge" ,
481+ },
482+ },
483+ }
484+
485+ for _ , test := range testCases {
486+ test := test
487+ t .Run (test .desc , func (t * testing.T ) {
488+ t .Parallel ()
489+
490+ p := NewSeverity (nil , files , test .opts )
491+
492+ newIssue := p .transform (test .issue )
493+
494+ assert .Equal (t , test .expected , newIssue )
495+ })
496+ }
497+ }
0 commit comments