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

     1  package printers
     2  
     3  import (
     4  	"bytes"
     5  	"go/token"
     6  	"testing"
     7  
     8  	"github.com/fatih/color"
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"github.com/vanstinator/golangci-lint/pkg/logutils"
    13  	"github.com/vanstinator/golangci-lint/pkg/result"
    14  )
    15  
    16  func TestTab_Print(t *testing.T) {
    17  	// force color globally
    18  	backup := color.NoColor
    19  	t.Cleanup(func() {
    20  		color.NoColor = backup
    21  	})
    22  	color.NoColor = false
    23  
    24  	issues := []result.Issue{
    25  		{
    26  			FromLinter: "linter-a",
    27  			Severity:   "warning",
    28  			Text:       "some issue",
    29  			Pos: token.Position{
    30  				Filename: "path/to/filea.go",
    31  				Offset:   2,
    32  				Line:     10,
    33  				Column:   4,
    34  			},
    35  		},
    36  		{
    37  			FromLinter: "linter-b",
    38  			Severity:   "error",
    39  			Text:       "another issue",
    40  			SourceLines: []string{
    41  				"func foo() {",
    42  				"\tfmt.Println(\"bar\")",
    43  				"}",
    44  			},
    45  			Pos: token.Position{
    46  				Filename: "path/to/fileb.go",
    47  				Offset:   5,
    48  				Line:     300,
    49  				Column:   9,
    50  			},
    51  		},
    52  	}
    53  
    54  	testCases := []struct {
    55  		desc            string
    56  		printLinterName bool
    57  		useColors       bool
    58  		expected        string
    59  	}{
    60  		{
    61  			desc:            "with linter name",
    62  			printLinterName: true,
    63  			useColors:       false,
    64  			expected: `path/to/filea.go:10:4   linter-a  some issue
    65  path/to/fileb.go:300:9  linter-b  another issue
    66  `,
    67  		},
    68  		{
    69  			desc:            "disable all options",
    70  			printLinterName: false,
    71  			useColors:       false,
    72  			expected: `path/to/filea.go:10:4   some issue
    73  path/to/fileb.go:300:9  another issue
    74  `,
    75  		},
    76  		{
    77  			desc:            "enable all options",
    78  			printLinterName: true,
    79  			useColors:       true,
    80  			//nolint:lll // color characters must be in a simple string.
    81  			expected: "\x1b[1mpath/to/filea.go:10\x1b[22m:4   linter-a  \x1b[31msome issue\x1b[0m\n\x1b[1mpath/to/fileb.go:300\x1b[22m:9  linter-b  \x1b[31manother issue\x1b[0m\n",
    82  		},
    83  	}
    84  
    85  	for _, test := range testCases {
    86  		test := test
    87  		t.Run(test.desc, func(t *testing.T) {
    88  			t.Parallel()
    89  
    90  			buf := new(bytes.Buffer)
    91  
    92  			printer := NewTab(test.printLinterName, test.useColors, logutils.NewStderrLog(logutils.DebugKeyEmpty), buf)
    93  
    94  			err := printer.Print(issues)
    95  			require.NoError(t, err)
    96  
    97  			assert.Equal(t, test.expected, buf.String())
    98  		})
    99  	}
   100  }