github.com/CodeherentUK/terraform@v0.11.10/configs/parser_config_test.go (about)

     1  package configs
     2  
     3  import (
     4  	"io/ioutil"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/hcl2/hcl"
     9  )
    10  
    11  // TestParseLoadConfigFileSuccess is a simple test that just verifies that
    12  // a number of test configuration files (in test-fixtures/valid-files) can
    13  // be parsed without raising any diagnostics.
    14  //
    15  // This test does not verify that reading these files produces the correct
    16  // file element contents. More detailed assertions may be made on some subset
    17  // of these configuration files in other tests.
    18  func TestParserLoadConfigFileSuccess(t *testing.T) {
    19  	files, err := ioutil.ReadDir("test-fixtures/valid-files")
    20  	if err != nil {
    21  		t.Fatal(err)
    22  	}
    23  
    24  	for _, info := range files {
    25  		name := info.Name()
    26  		t.Run(name, func(t *testing.T) {
    27  			src, err := ioutil.ReadFile(filepath.Join("test-fixtures/valid-files", name))
    28  			if err != nil {
    29  				t.Fatal(err)
    30  			}
    31  
    32  			parser := testParser(map[string]string{
    33  				name: string(src),
    34  			})
    35  
    36  			_, diags := parser.LoadConfigFile(name)
    37  			if diags.HasErrors() {
    38  				t.Errorf("unexpected error diagnostics")
    39  				for _, diag := range diags {
    40  					t.Logf("- %s", diag)
    41  				}
    42  			}
    43  		})
    44  	}
    45  }
    46  
    47  // TestParseLoadConfigFileFailure is a simple test that just verifies that
    48  // a number of test configuration files (in test-fixtures/invalid-files)
    49  // produce errors as expected.
    50  //
    51  // This test does not verify specific error messages, so more detailed
    52  // assertions should be made on some subset of these configuration files in
    53  // other tests.
    54  func TestParserLoadConfigFileFailure(t *testing.T) {
    55  	files, err := ioutil.ReadDir("test-fixtures/invalid-files")
    56  	if err != nil {
    57  		t.Fatal(err)
    58  	}
    59  
    60  	for _, info := range files {
    61  		name := info.Name()
    62  		t.Run(name, func(t *testing.T) {
    63  			src, err := ioutil.ReadFile(filepath.Join("test-fixtures/invalid-files", name))
    64  			if err != nil {
    65  				t.Fatal(err)
    66  			}
    67  
    68  			parser := testParser(map[string]string{
    69  				name: string(src),
    70  			})
    71  
    72  			_, diags := parser.LoadConfigFile(name)
    73  			if !diags.HasErrors() {
    74  				t.Errorf("LoadConfigFile succeeded; want errors")
    75  			}
    76  			for _, diag := range diags {
    77  				t.Logf("- %s", diag)
    78  			}
    79  		})
    80  	}
    81  }
    82  
    83  // This test uses a subset of the same fixture files as
    84  // TestParserLoadConfigFileFailure, but additionally verifies that each
    85  // file produces the expected diagnostic summary.
    86  func TestParserLoadConfigFileFailureMessages(t *testing.T) {
    87  	tests := []struct {
    88  		Filename     string
    89  		WantSeverity hcl.DiagnosticSeverity
    90  		WantDiag     string
    91  	}{
    92  		{
    93  			"invalid-files/data-resource-lifecycle.tf",
    94  			hcl.DiagError,
    95  			"Unsupported lifecycle block",
    96  		},
    97  		{
    98  			"invalid-files/variable-type-unknown.tf",
    99  			hcl.DiagError,
   100  			"Invalid type specification",
   101  		},
   102  		{
   103  			"invalid-files/unexpected-attr.tf",
   104  			hcl.DiagError,
   105  			"Unsupported attribute",
   106  		},
   107  		{
   108  			"invalid-files/unexpected-block.tf",
   109  			hcl.DiagError,
   110  			"Unsupported block type",
   111  		},
   112  		{
   113  			"invalid-files/resource-lifecycle-badbool.tf",
   114  			hcl.DiagError,
   115  			"Unsuitable value type",
   116  		},
   117  		{
   118  			"valid-files/resources-ignorechanges-all-legacy.tf",
   119  			hcl.DiagWarning,
   120  			"Deprecated ignore_changes wildcard",
   121  		},
   122  		{
   123  			"valid-files/resources-ignorechanges-all-legacy.tf.json",
   124  			hcl.DiagWarning,
   125  			"Deprecated ignore_changes wildcard",
   126  		},
   127  	}
   128  
   129  	for _, test := range tests {
   130  		t.Run(test.Filename, func(t *testing.T) {
   131  			src, err := ioutil.ReadFile(filepath.Join("test-fixtures", test.Filename))
   132  			if err != nil {
   133  				t.Fatal(err)
   134  			}
   135  
   136  			parser := testParser(map[string]string{
   137  				test.Filename: string(src),
   138  			})
   139  
   140  			_, diags := parser.LoadConfigFile(test.Filename)
   141  			if len(diags) != 1 {
   142  				t.Errorf("Wrong number of diagnostics %d; want 1", len(diags))
   143  				for _, diag := range diags {
   144  					t.Logf("- %s", diag)
   145  				}
   146  				return
   147  			}
   148  			if diags[0].Severity != test.WantSeverity {
   149  				t.Errorf("Wrong diagnostic severity %#v; want %#v", diags[0].Severity, test.WantSeverity)
   150  			}
   151  			if diags[0].Summary != test.WantDiag {
   152  				t.Errorf("Wrong diagnostic summary\ngot:  %s\nwant: %s", diags[0].Summary, test.WantDiag)
   153  			}
   154  		})
   155  	}
   156  }