github.com/vanstinator/golangci-lint@v0.0.0-20240223191551-cc572f00d9d1/pkg/printers/teamcity_test.go (about)

     1  package printers
     2  
     3  import (
     4  	"bytes"
     5  	"go/token"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/vanstinator/golangci-lint/pkg/result"
    12  )
    13  
    14  func TestTeamCity_Print(t *testing.T) {
    15  	issues := []result.Issue{
    16  		{
    17  			FromLinter: "linter-a",
    18  			Text:       "warning issue",
    19  			Pos: token.Position{
    20  				Filename: "path/to/filea.go",
    21  				Offset:   2,
    22  				Line:     10,
    23  				Column:   4,
    24  			},
    25  		},
    26  		{
    27  			FromLinter: "linter-a",
    28  			Severity:   "error",
    29  			Text:       "error issue",
    30  			Pos: token.Position{
    31  				Filename: "path/to/filea.go",
    32  				Offset:   2,
    33  				Line:     10,
    34  			},
    35  		},
    36  		{
    37  			FromLinter: "linter-b",
    38  			Text:       "info issue",
    39  			SourceLines: []string{
    40  				"func foo() {",
    41  				"\tfmt.Println(\"bar\")",
    42  				"}",
    43  			},
    44  			Pos: token.Position{
    45  				Filename: "path/to/fileb.go",
    46  				Offset:   5,
    47  				Line:     300,
    48  				Column:   9,
    49  			},
    50  		},
    51  	}
    52  
    53  	buf := new(bytes.Buffer)
    54  	printer := NewTeamCity(buf)
    55  
    56  	err := printer.Print(issues)
    57  	require.NoError(t, err)
    58  
    59  	expected := `##teamcity[InspectionType id='linter-a' name='linter-a' description='linter-a' category='Golangci-lint reports']
    60  ##teamcity[inspection typeId='linter-a' message='warning issue' file='path/to/filea.go' line='10' SEVERITY='']
    61  ##teamcity[inspection typeId='linter-a' message='error issue' file='path/to/filea.go' line='10' SEVERITY='ERROR']
    62  ##teamcity[InspectionType id='linter-b' name='linter-b' description='linter-b' category='Golangci-lint reports']
    63  ##teamcity[inspection typeId='linter-b' message='info issue' file='path/to/fileb.go' line='300' SEVERITY='']
    64  `
    65  
    66  	assert.Equal(t, expected, buf.String())
    67  }
    68  
    69  func TestTeamCity_limit(t *testing.T) {
    70  	tests := []struct {
    71  		input    string
    72  		max      int
    73  		expected string
    74  	}{
    75  		{
    76  			input:    "golangci-lint",
    77  			max:      0,
    78  			expected: "",
    79  		},
    80  		{
    81  			input:    "golangci-lint",
    82  			max:      8,
    83  			expected: "golangci",
    84  		},
    85  		{
    86  			input:    "golangci-lint",
    87  			max:      13,
    88  			expected: "golangci-lint",
    89  		},
    90  		{
    91  			input:    "golangci-lint",
    92  			max:      15,
    93  			expected: "golangci-lint",
    94  		},
    95  		{
    96  			input:    "こんにちは",
    97  			max:      3,
    98  			expected: "こんに",
    99  		},
   100  	}
   101  
   102  	for _, tc := range tests {
   103  		require.Equal(t, tc.expected, limit(tc.input, tc.max))
   104  	}
   105  }