github.com/tetrafolium/tflint@v0.8.0/printer/default_test.go (about)

     1  package printer
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/wata727/tflint/issue"
     9  )
    10  
    11  func TestDefaultPrint(t *testing.T) {
    12  	cases := []struct {
    13  		Name   string
    14  		Issues []*issue.Issue
    15  		Quiet  bool
    16  		Result string
    17  	}{
    18  		{
    19  			Name:   "no issues",
    20  			Issues: []*issue.Issue{},
    21  			Result: successColor("Awesome! Your code is following the best practices :)") + "\n",
    22  		},
    23  		{
    24  			Name:   "no issues with quiet option",
    25  			Issues: []*issue.Issue{},
    26  			Quiet:  true,
    27  			Result: "",
    28  		},
    29  		{
    30  			Name: "multi files",
    31  			Issues: []*issue.Issue{
    32  				{
    33  					Detector: "error detector",
    34  					File:     "template.tf",
    35  					Line:     1,
    36  					Type:     "ERROR",
    37  					Message:  "example error message",
    38  				},
    39  				{
    40  					Detector: "notice detector",
    41  					File:     "application.tf",
    42  					Line:     10,
    43  					Type:     "NOTICE",
    44  					Message:  "example notice message",
    45  					Link:     "https://github.com/wata727/tflint",
    46  				},
    47  				{
    48  					Detector: "warning detector",
    49  					File:     "template.tf",
    50  					Line:     5,
    51  					Type:     "WARNING",
    52  					Message:  "example warning message",
    53  				},
    54  				{
    55  					Detector: "warning detector",
    56  					File:     "template.tf",
    57  					Line:     3,
    58  					Type:     "WARNING",
    59  					Message:  "example warning message",
    60  				},
    61  			},
    62  			Result: fmt.Sprintf(`%s
    63  	%s example notice message (notice detector)
    64  %s
    65  	%s example error message (error detector)
    66  	%s example warning message (warning detector)
    67  	%s example warning message (warning detector)
    68  
    69  Result: %s  (%s , %s , %s)
    70  `, fileColor("application.tf"), noticeColor("NOTICE:10"), fileColor("template.tf"), errorColor("ERROR:1"), warningColor("WARNING:3"), warningColor("WARNING:5"), fileColor("4 issues"), errorColor("1 errors"), warningColor("2 warnings"), noticeColor("1 notices")),
    71  		},
    72  	}
    73  
    74  	for _, tc := range cases {
    75  		stdout := &bytes.Buffer{}
    76  		stderr := &bytes.Buffer{}
    77  		p := NewPrinter(stdout, stderr)
    78  		p.DefaultPrint(tc.Issues, tc.Quiet)
    79  		result := stdout.String()
    80  
    81  		if result != tc.Result {
    82  			t.Fatalf("\nBad: %s\nExpected: %s\n\ntestcase: %s", result, tc.Result, tc.Name)
    83  		}
    84  	}
    85  }