github.com/jpreese/tflint@v0.19.2-0.20200908152133-b01686250fb6/tflint/annotation_test.go (about)

     1  package tflint
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  
    10  	"github.com/google/go-cmp/cmp"
    11  	"github.com/google/go-cmp/cmp/cmpopts"
    12  	hcl "github.com/hashicorp/hcl/v2"
    13  	"github.com/hashicorp/hcl/v2/hclsyntax"
    14  )
    15  
    16  func Test_NewAnnotations(t *testing.T) {
    17  	currentDir, err := os.Getwd()
    18  	if err != nil {
    19  		t.Fatal(err)
    20  	}
    21  	defer os.Chdir(currentDir)
    22  
    23  	src, err := ioutil.ReadFile(filepath.Join(currentDir, "test-fixtures", "annotations", "resource.tf"))
    24  	if err != nil {
    25  		t.Fatal(err)
    26  	}
    27  	tokens, diags := hclsyntax.LexConfig(src, "resource.tf", hcl.Pos{Byte: 0, Line: 1, Column: 1})
    28  	if diags.HasErrors() {
    29  		t.Fatal(diags)
    30  	}
    31  	ret := NewAnnotations(tokens)
    32  
    33  	expected := Annotations{
    34  		{
    35  			Content: "aws_instance_invalid_type",
    36  			Token: hclsyntax.Token{
    37  				Type:  hclsyntax.TokenComment,
    38  				Bytes: []byte("/* tflint-ignore: aws_instance_invalid_type */"),
    39  				Range: hcl.Range{
    40  					Filename: "resource.tf",
    41  					Start:    hcl.Pos{Line: 2, Column: 5},
    42  					End:      hcl.Pos{Line: 2, Column: 51},
    43  				},
    44  			},
    45  		},
    46  		{
    47  			Content: "aws_instance_invalid_type",
    48  			Token: hclsyntax.Token{
    49  				Type:  hclsyntax.TokenComment,
    50  				Bytes: []byte(fmt.Sprintf("// tflint-ignore: aws_instance_invalid_type%s", newLine())),
    51  				Range: hcl.Range{
    52  					Filename: "resource.tf",
    53  					Start:    hcl.Pos{Line: 3, Column: 32},
    54  					End:      hcl.Pos{Line: 4, Column: 1},
    55  				},
    56  			},
    57  		},
    58  		{
    59  			Content: "aws_instance_invalid_type",
    60  			Token: hclsyntax.Token{
    61  				Type:  hclsyntax.TokenComment,
    62  				Bytes: []byte(fmt.Sprintf("# tflint-ignore: aws_instance_invalid_type This is also comment%s", newLine())),
    63  				Range: hcl.Range{
    64  					Filename: "resource.tf",
    65  					Start:    hcl.Pos{Line: 4, Column: 5},
    66  					End:      hcl.Pos{Line: 5, Column: 1},
    67  				},
    68  			},
    69  		},
    70  	}
    71  
    72  	opts := cmpopts.IgnoreFields(hcl.Pos{}, "Byte")
    73  	if !cmp.Equal(expected, ret, opts) {
    74  		t.Fatalf("Test failed. Diff: %s", cmp.Diff(expected, ret, opts))
    75  	}
    76  }
    77  
    78  func Test_IsAffected(t *testing.T) {
    79  	issue := &Issue{
    80  		Rule:    &testRule{},
    81  		Message: "Test rule",
    82  		Range: hcl.Range{
    83  			Filename: "test.tf",
    84  			Start:    hcl.Pos{Line: 2},
    85  		},
    86  	}
    87  
    88  	cases := []struct {
    89  		Name       string
    90  		Annotation Annotation
    91  		Expected   bool
    92  	}{
    93  		{
    94  			Name: "affected (same line)",
    95  			Annotation: Annotation{
    96  				Content: "test_rule",
    97  				Token: hclsyntax.Token{
    98  					Type: hclsyntax.TokenComment,
    99  					Range: hcl.Range{
   100  						Filename: "test.tf",
   101  						Start:    hcl.Pos{Line: 2},
   102  					},
   103  				},
   104  			},
   105  			Expected: true,
   106  		},
   107  		{
   108  			Name: "affected (above line)",
   109  			Annotation: Annotation{
   110  				Content: "test_rule",
   111  				Token: hclsyntax.Token{
   112  					Type: hclsyntax.TokenComment,
   113  					Range: hcl.Range{
   114  						Filename: "test.tf",
   115  						Start:    hcl.Pos{Line: 1},
   116  					},
   117  				},
   118  			},
   119  			Expected: true,
   120  		},
   121  		{
   122  			Name: "not affected (under line)",
   123  			Annotation: Annotation{
   124  				Content: "test_rule",
   125  				Token: hclsyntax.Token{
   126  					Type: hclsyntax.TokenComment,
   127  					Range: hcl.Range{
   128  						Filename: "test.tf",
   129  						Start:    hcl.Pos{Line: 3},
   130  					},
   131  				},
   132  			},
   133  			Expected: false,
   134  		},
   135  		{
   136  			Name: "not affected (another filename)",
   137  			Annotation: Annotation{
   138  				Content: "test_rule",
   139  				Token: hclsyntax.Token{
   140  					Type: hclsyntax.TokenComment,
   141  					Range: hcl.Range{
   142  						Filename: "test2.tf",
   143  						Start:    hcl.Pos{Line: 2},
   144  					},
   145  				},
   146  			},
   147  			Expected: false,
   148  		},
   149  		{
   150  			Name: "not affected (another rule)",
   151  			Annotation: Annotation{
   152  				Content: "test_another_rule",
   153  				Token: hclsyntax.Token{
   154  					Type: hclsyntax.TokenComment,
   155  					Range: hcl.Range{
   156  						Filename: "test.tf",
   157  						Start:    hcl.Pos{Line: 2},
   158  					},
   159  				},
   160  			},
   161  			Expected: false,
   162  		},
   163  		{
   164  			Name: "affected (all)",
   165  			Annotation: Annotation{
   166  				Content: "all",
   167  				Token: hclsyntax.Token{
   168  					Type: hclsyntax.TokenComment,
   169  					Range: hcl.Range{
   170  						Filename: "test.tf",
   171  						Start:    hcl.Pos{Line: 2},
   172  					},
   173  				},
   174  			},
   175  			Expected: true,
   176  		},
   177  	}
   178  
   179  	for _, tc := range cases {
   180  		ret := tc.Annotation.IsAffected(issue)
   181  		if ret != tc.Expected {
   182  			t.Fatalf("Failed `%s` test: expected=%t, got=%t", tc.Name, tc.Expected, ret)
   183  		}
   184  	}
   185  }