github.com/yoheimuta/protolint@v0.49.8-0.20240515023657-4ecaebb7575d/internal/linter/report/reporters/sarifReporter_test.go (about)

     1  package reporters_test
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/yoheimuta/go-protoparser/v4/parser/meta"
     8  
     9  	"github.com/yoheimuta/protolint/internal/linter/report/reporters"
    10  	"github.com/yoheimuta/protolint/linter/report"
    11  	"github.com/yoheimuta/protolint/linter/rule"
    12  )
    13  
    14  func TestSarifReporter_Report(t *testing.T) {
    15  	tests := []struct {
    16  		name          string
    17  		inputFailures []report.Failure
    18  		wantOutput    string
    19  	}{
    20  		{
    21  			name: "Prints failures in JSON format",
    22  			inputFailures: []report.Failure{
    23  				report.FailureWithSeverityf(
    24  					meta.Position{
    25  						Filename: "example.proto",
    26  						Offset:   100,
    27  						Line:     5,
    28  						Column:   10,
    29  					},
    30  					"ENUM_NAMES_UPPER_CAMEL_CASE",
    31  					string(rule.SeverityError),
    32  					`EnumField name "fIRST_VALUE" must be CAPITALS_WITH_UNDERSCORES`,
    33  				),
    34  				report.FailureWithSeverityf(
    35  					meta.Position{
    36  						Filename: "example.proto",
    37  						Offset:   200,
    38  						Line:     10,
    39  						Column:   20,
    40  					},
    41  					"ENUM_NAMES_UPPER_CAMEL_CASE",
    42  					string(rule.SeverityWarning),
    43  					`EnumField name "SECOND.VALUE" must be CAPITALS_WITH_UNDERSCORES`,
    44  				),
    45  			},
    46  			wantOutput: `{
    47    "runs": [
    48      {
    49        "artifacts": [
    50          {
    51            "location": {
    52              "uri": "example.proto"
    53            }
    54          }
    55        ],
    56        "results": [
    57          {
    58            "kind": "fail",
    59            "level": "error",
    60            "locations": [
    61              {
    62                "physicalLocation": {
    63                  "artifactLocation": {
    64                    "uri": "example.proto"
    65                  },
    66                  "region": {
    67                    "startColumn": 10,
    68                    "startLine": 5
    69                  }
    70                }
    71              }
    72            ],
    73            "message": {
    74              "text": "EnumField name \"fIRST_VALUE\" must be CAPITALS_WITH_UNDERSCORES"
    75            },
    76            "ruleId": "ENUM_NAMES_UPPER_CAMEL_CASE"
    77          },
    78          {
    79            "kind": "fail",
    80            "level": "warning",
    81            "locations": [
    82              {
    83                "physicalLocation": {
    84                  "artifactLocation": {
    85                    "uri": "example.proto"
    86                  },
    87                  "region": {
    88                    "startColumn": 20,
    89                    "startLine": 10
    90                  }
    91                }
    92              }
    93            ],
    94            "message": {
    95              "text": "EnumField name \"SECOND.VALUE\" must be CAPITALS_WITH_UNDERSCORES"
    96            },
    97            "ruleId": "ENUM_NAMES_UPPER_CAMEL_CASE"
    98          }
    99        ],
   100        "tool": {
   101          "driver": {
   102            "informationUri": "https://github.com/yoheimuta/protolint",
   103            "name": "protolint",
   104            "rules": [
   105              {
   106                "helpUri": "https://github.com/yoheimuta/protolint",
   107                "id": "ENUM_NAMES_UPPER_CAMEL_CASE"
   108              }
   109            ]
   110          }
   111        }
   112      }
   113    ],
   114    "version": "2.1.0"
   115  }`,
   116  		},
   117  	}
   118  
   119  	for _, test := range tests {
   120  		test := test
   121  		t.Run(test.name, func(t *testing.T) {
   122  			buf := &bytes.Buffer{}
   123  			err := reporters.SarifReporter{}.Report(buf, test.inputFailures)
   124  			if err != nil {
   125  				t.Errorf("got err %v, but want nil", err)
   126  				return
   127  			}
   128  			if buf.String() != test.wantOutput {
   129  				t.Errorf("got %s, but want %s", buf.String(), test.wantOutput)
   130  			}
   131  		})
   132  	}
   133  }