github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/configs/provider_test.go (about)

     1  package configs
     2  
     3  import (
     4  	"io/ioutil"
     5  	"testing"
     6  
     7  	"github.com/go-test/deep"
     8  	"github.com/hashicorp/hcl/v2"
     9  	"github.com/hashicorp/hcl/v2/hclsyntax"
    10  	"github.com/hashicorp/terraform/internal/addrs"
    11  )
    12  
    13  func TestProviderReservedNames(t *testing.T) {
    14  	src, err := ioutil.ReadFile("testdata/invalid-files/provider-reserved.tf")
    15  	if err != nil {
    16  		t.Fatal(err)
    17  	}
    18  	parser := testParser(map[string]string{
    19  		"config.tf": string(src),
    20  	})
    21  	_, diags := parser.LoadConfigFile("config.tf")
    22  
    23  	assertExactDiagnostics(t, diags, []string{
    24  		//TODO: This deprecation warning will be removed in terraform v0.15.
    25  		`config.tf:4,13-20: Version constraints inside provider configuration blocks are deprecated; Terraform 0.13 and earlier allowed provider version constraints inside the provider configuration block, but that is now deprecated and will be removed in a future version of Terraform. To silence this warning, move the provider version constraint into the required_providers block.`,
    26  		`config.tf:10,3-8: Reserved argument name in provider block; The provider argument name "count" is reserved for use by Terraform in a future version.`,
    27  		`config.tf:11,3-13: Reserved argument name in provider block; The provider argument name "depends_on" is reserved for use by Terraform in a future version.`,
    28  		`config.tf:12,3-11: Reserved argument name in provider block; The provider argument name "for_each" is reserved for use by Terraform in a future version.`,
    29  		`config.tf:14,3-12: Reserved block type name in provider block; The block type name "lifecycle" is reserved for use by Terraform in a future version.`,
    30  		`config.tf:15,3-9: Reserved block type name in provider block; The block type name "locals" is reserved for use by Terraform in a future version.`,
    31  		`config.tf:13,3-9: Reserved argument name in provider block; The provider argument name "source" is reserved for use by Terraform in a future version.`,
    32  	})
    33  }
    34  
    35  func TestParseProviderConfigCompact(t *testing.T) {
    36  	tests := []struct {
    37  		Input    string
    38  		Want     addrs.LocalProviderConfig
    39  		WantDiag string
    40  	}{
    41  		{
    42  			`aws`,
    43  			addrs.LocalProviderConfig{
    44  				LocalName: "aws",
    45  			},
    46  			``,
    47  		},
    48  		{
    49  			`aws.foo`,
    50  			addrs.LocalProviderConfig{
    51  				LocalName: "aws",
    52  				Alias:     "foo",
    53  			},
    54  			``,
    55  		},
    56  		{
    57  			`aws["foo"]`,
    58  			addrs.LocalProviderConfig{},
    59  			`The provider type name must either stand alone or be followed by an alias name separated with a dot.`,
    60  		},
    61  	}
    62  
    63  	for _, test := range tests {
    64  		t.Run(test.Input, func(t *testing.T) {
    65  			traversal, parseDiags := hclsyntax.ParseTraversalAbs([]byte(test.Input), "", hcl.Pos{})
    66  			if len(parseDiags) != 0 {
    67  				t.Errorf("unexpected diagnostics during parse")
    68  				for _, diag := range parseDiags {
    69  					t.Logf("- %s", diag)
    70  				}
    71  				return
    72  			}
    73  
    74  			got, diags := ParseProviderConfigCompact(traversal)
    75  
    76  			if test.WantDiag != "" {
    77  				if len(diags) != 1 {
    78  					t.Fatalf("got %d diagnostics; want 1", len(diags))
    79  				}
    80  				gotDetail := diags[0].Description().Detail
    81  				if gotDetail != test.WantDiag {
    82  					t.Fatalf("wrong diagnostic detail\ngot:  %s\nwant: %s", gotDetail, test.WantDiag)
    83  				}
    84  				return
    85  			} else {
    86  				if len(diags) != 0 {
    87  					t.Fatalf("got %d diagnostics; want 0", len(diags))
    88  				}
    89  			}
    90  
    91  			for _, problem := range deep.Equal(got, test.Want) {
    92  				t.Error(problem)
    93  			}
    94  		})
    95  	}
    96  }
    97  
    98  func TestParseProviderConfigCompactStr(t *testing.T) {
    99  	tests := []struct {
   100  		Input    string
   101  		Want     addrs.LocalProviderConfig
   102  		WantDiag string
   103  	}{
   104  		{
   105  			`aws`,
   106  			addrs.LocalProviderConfig{
   107  				LocalName: "aws",
   108  			},
   109  			``,
   110  		},
   111  		{
   112  			`aws.foo`,
   113  			addrs.LocalProviderConfig{
   114  				LocalName: "aws",
   115  				Alias:     "foo",
   116  			},
   117  			``,
   118  		},
   119  		{
   120  			`aws["foo"]`,
   121  			addrs.LocalProviderConfig{},
   122  			`The provider type name must either stand alone or be followed by an alias name separated with a dot.`,
   123  		},
   124  	}
   125  
   126  	for _, test := range tests {
   127  		t.Run(test.Input, func(t *testing.T) {
   128  			got, diags := ParseProviderConfigCompactStr(test.Input)
   129  
   130  			if test.WantDiag != "" {
   131  				if len(diags) != 1 {
   132  					t.Fatalf("got %d diagnostics; want 1", len(diags))
   133  				}
   134  				gotDetail := diags[0].Description().Detail
   135  				if gotDetail != test.WantDiag {
   136  					t.Fatalf("wrong diagnostic detail\ngot:  %s\nwant: %s", gotDetail, test.WantDiag)
   137  				}
   138  				return
   139  			} else {
   140  				if len(diags) != 0 {
   141  					t.Fatalf("got %d diagnostics; want 0", len(diags))
   142  				}
   143  			}
   144  
   145  			for _, problem := range deep.Equal(got, test.Want) {
   146  				t.Error(problem)
   147  			}
   148  		})
   149  	}
   150  }