github.com/wata727/tflint@v0.12.2-0.20191013070026-96dd0d36f385/formatter/pretty_test.go (about)

     1  package formatter
     2  
     3  import (
     4  	"bytes"
     5  	"errors"
     6  	"testing"
     7  
     8  	"github.com/fatih/color"
     9  	hcl "github.com/hashicorp/hcl/v2"
    10  	"github.com/wata727/tflint/tflint"
    11  )
    12  
    13  func Test_prettyPrint(t *testing.T) {
    14  	// Disable color
    15  	color.NoColor = true
    16  
    17  	cases := []struct {
    18  		Name    string
    19  		Issues  tflint.Issues
    20  		Error   *tflint.Error
    21  		Sources map[string][]byte
    22  		Stdout  string
    23  		Stderr  string
    24  	}{
    25  		{
    26  			Name:   "no issues",
    27  			Issues: tflint.Issues{},
    28  			Stdout: "",
    29  		},
    30  		{
    31  			Name: "issues",
    32  			Issues: tflint.Issues{
    33  				{
    34  					Rule:    &testRule{},
    35  					Message: "test",
    36  					Range: hcl.Range{
    37  						Filename: "test.tf",
    38  						Start:    hcl.Pos{Line: 1, Column: 1, Byte: 0},
    39  						End:      hcl.Pos{Line: 1, Column: 4, Byte: 3},
    40  					},
    41  					Callers: []hcl.Range{
    42  						{
    43  							Filename: "test.tf",
    44  							Start:    hcl.Pos{Line: 1, Column: 1, Byte: 0},
    45  							End:      hcl.Pos{Line: 1, Column: 4, Byte: 3},
    46  						},
    47  						{
    48  							Filename: "module.tf",
    49  							Start:    hcl.Pos{Line: 2, Column: 3, Byte: 0},
    50  							End:      hcl.Pos{Line: 2, Column: 6, Byte: 3},
    51  						},
    52  					},
    53  				},
    54  			},
    55  			Sources: map[string][]byte{
    56  				"test.tf": []byte("foo = 1"),
    57  			},
    58  			Stdout: `1 issue(s) found:
    59  
    60  Error: test (test_rule)
    61  
    62    on test.tf line 1:
    63     1: foo = 1
    64  
    65  Callers:
    66     test.tf:1,1-4
    67     module.tf:2,3-6
    68  
    69  Reference: https://github.com
    70  
    71  `,
    72  		},
    73  		{
    74  			Name: "no sources",
    75  			Issues: tflint.Issues{
    76  				{
    77  					Rule:    &testRule{},
    78  					Message: "test",
    79  					Range: hcl.Range{
    80  						Filename: "test.tf",
    81  						Start:    hcl.Pos{Line: 1, Column: 1, Byte: 0},
    82  						End:      hcl.Pos{Line: 1, Column: 4, Byte: 3},
    83  					},
    84  				},
    85  			},
    86  			Stdout: `1 issue(s) found:
    87  
    88  Error: test (test_rule)
    89  
    90    on test.tf line 1:
    91     (source code not available)
    92  
    93  Reference: https://github.com
    94  
    95  `,
    96  		},
    97  		{
    98  			Name:   "error",
    99  			Issues: tflint.Issues{},
   100  			Error:  tflint.NewContextError("Failed to work", errors.New("I don't feel like working")),
   101  			Stderr: `Failed to work. An error occurred:
   102  
   103  Error: I don't feel like working
   104  
   105  `,
   106  		},
   107  	}
   108  
   109  	for _, tc := range cases {
   110  		stdout := &bytes.Buffer{}
   111  		stderr := &bytes.Buffer{}
   112  		formatter := &Formatter{Stdout: stdout, Stderr: stderr}
   113  
   114  		formatter.prettyPrint(tc.Issues, tc.Error, tc.Sources)
   115  
   116  		if stdout.String() != tc.Stdout {
   117  			t.Fatalf("Failed %s test: expected=%s, stdout=%s", tc.Name, tc.Stdout, stdout.String())
   118  		}
   119  		if stderr.String() != tc.Stderr {
   120  			t.Fatalf("Failed %s test: expected=%s, stderr=%s", tc.Name, tc.Stderr, stderr.String())
   121  		}
   122  	}
   123  }