github.com/googleapis/api-linter@v1.65.2/cmd/api-linter/github_actions_test.go (about)

     1  // Copyright 2022 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // 		https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/google/go-cmp/cmp"
    21  	"github.com/googleapis/api-linter/lint"
    22  	"google.golang.org/protobuf/types/descriptorpb"
    23  )
    24  
    25  func TestFormatGitHubActionOutput(t *testing.T) {
    26  	tests := []struct {
    27  		name string
    28  		data []lint.Response
    29  		want string
    30  	}{
    31  		{
    32  			name: "Empty input",
    33  			data: []lint.Response{},
    34  			want: "",
    35  		},
    36  		{
    37  			name: "Example with partial location specifics",
    38  			data: []lint.Response{
    39  				{
    40  					FilePath: "example.proto",
    41  					Problems: []lint.Problem{
    42  						{
    43  							RuleID:  "line::col::endLine::endColumn",
    44  							Message: "line, column, endline, and endColumn",
    45  							Location: &descriptorpb.SourceCodeInfo_Location{
    46  								Span: []int32{5, 6, 7, 8},
    47  							},
    48  						},
    49  						{
    50  							RuleID:  "line::col::endLine",
    51  							Message: "Line, column, and endline",
    52  							Location: &descriptorpb.SourceCodeInfo_Location{
    53  								Span: []int32{5, 6, 7},
    54  							},
    55  						},
    56  						{
    57  							RuleID:  "line::col",
    58  							Message: "Line and column",
    59  							Location: &descriptorpb.SourceCodeInfo_Location{
    60  								Span: []int32{5, 6},
    61  							},
    62  						},
    63  						{
    64  							RuleID:  "line",
    65  							Message: "Line only",
    66  							Location: &descriptorpb.SourceCodeInfo_Location{
    67  								Span: []int32{5},
    68  							},
    69  						},
    70  					},
    71  				},
    72  			},
    73  			want: `::error file=example.proto,endColumn=8,endLine=7,col=6,line=5,title=line։։col։։endLine։։endColumn::line, column, endline, and endColumn
    74  ::error file=example.proto,endLine=7,col=6,line=5,title=line։։col։։endLine::Line, column, and endline
    75  ::error file=example.proto,col=6,line=5,title=line։։col::Line and column
    76  ::error file=example.proto,line=5,title=line::Line only
    77  `,
    78  		},
    79  		{
    80  			name: "Example with location specifics",
    81  			data: []lint.Response{
    82  				{
    83  					FilePath: "example.proto",
    84  					Problems: []lint.Problem{
    85  						{
    86  							RuleID: "core::naming_formats::field_names",
    87  							Location: &descriptorpb.SourceCodeInfo_Location{
    88  								Span: []int32{1, 2, 3, 4},
    89  							},
    90  						},
    91  						{
    92  							RuleID:  "core::naming_formats::field_names",
    93  							Message: "multi\nline\ncomment",
    94  							Location: &descriptorpb.SourceCodeInfo_Location{
    95  								Span: []int32{5, 6, 7, 8},
    96  							},
    97  						},
    98  					},
    99  				},
   100  			},
   101  			want: `::error file=example.proto,endColumn=4,endLine=3,col=2,line=1,title=core։։naming_formats։։field_names::\n\nhttps://linter.aip.dev/naming_formats/field_names
   102  ::error file=example.proto,endColumn=8,endLine=7,col=6,line=5,title=core։։naming_formats։։field_names::multi\nline\ncomment\n\nhttps://linter.aip.dev/naming_formats/field_names
   103  `,
   104  		},
   105  		{
   106  			name: "Example with a couple of responses",
   107  			data: []lint.Response{
   108  				{
   109  					FilePath: "example.proto",
   110  					Problems: []lint.Problem{
   111  						{RuleID: "core::naming_formats::field_names"},
   112  						{RuleID: "core::naming_formats::field_names"},
   113  					},
   114  				},
   115  				{
   116  					FilePath: "example2.proto",
   117  					Problems: []lint.Problem{
   118  						{RuleID: "core::0131::request_message::name"},
   119  						{RuleID: "core::0132::response_message::name"},
   120  					},
   121  				},
   122  				{
   123  					FilePath: "example3.proto",
   124  					Problems: []lint.Problem{
   125  						{RuleID: "core::naming_formats::field_names"},
   126  					},
   127  				},
   128  				{
   129  					FilePath: "example4.proto",
   130  					Problems: []lint.Problem{
   131  						{RuleID: "core::naming_formats::field_names"},
   132  						{RuleID: "core::0132::response_message::name"},
   133  					},
   134  				},
   135  			},
   136  			want: `::error file=example.proto,title=core։։naming_formats։։field_names::\n\nhttps://linter.aip.dev/naming_formats/field_names
   137  ::error file=example.proto,title=core։։naming_formats։։field_names::\n\nhttps://linter.aip.dev/naming_formats/field_names
   138  ::error file=example2.proto,title=core։։0131։։request_message։։name::\n\nhttps://linter.aip.dev/131/request_message/name
   139  ::error file=example2.proto,title=core։։0132։։response_message։։name::\n\nhttps://linter.aip.dev/132/response_message/name
   140  ::error file=example3.proto,title=core։։naming_formats։։field_names::\n\nhttps://linter.aip.dev/naming_formats/field_names
   141  ::error file=example4.proto,title=core։։naming_formats։։field_names::\n\nhttps://linter.aip.dev/naming_formats/field_names
   142  ::error file=example4.proto,title=core։։0132։։response_message։։name::\n\nhttps://linter.aip.dev/132/response_message/name
   143  `,
   144  		},
   145  	}
   146  	for _, test := range tests {
   147  		t.Run(test.name, func(t *testing.T) {
   148  			got := formatGitHubActionOutput(test.data)
   149  			if diff := cmp.Diff(string(test.want), string(got)); diff != "" {
   150  				t.Errorf("formatGitHubActionOutput() mismatch (-want +got):\n%s", diff)
   151  			}
   152  		})
   153  	}
   154  }