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