github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/lang/globalref/analyzer_test.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package globalref
     5  
     6  import (
     7  	"context"
     8  	"path/filepath"
     9  	"testing"
    10  
    11  	"github.com/zclconf/go-cty/cty"
    12  
    13  	"github.com/terramate-io/tf/addrs"
    14  	"github.com/terramate-io/tf/configs/configload"
    15  	"github.com/terramate-io/tf/configs/configschema"
    16  	"github.com/terramate-io/tf/initwd"
    17  	"github.com/terramate-io/tf/providers"
    18  	"github.com/terramate-io/tf/registry"
    19  )
    20  
    21  func testAnalyzer(t *testing.T, fixtureName string) *Analyzer {
    22  	configDir := filepath.Join("testdata", fixtureName)
    23  
    24  	loader, cleanup := configload.NewLoaderForTests(t)
    25  	defer cleanup()
    26  
    27  	inst := initwd.NewModuleInstaller(loader.ModulesDir(), loader, registry.NewClient(nil, nil))
    28  	_, instDiags := inst.InstallModules(context.Background(), configDir, "tests", true, false, initwd.ModuleInstallHooksImpl{})
    29  	if instDiags.HasErrors() {
    30  		t.Fatalf("unexpected module installation errors: %s", instDiags.Err().Error())
    31  	}
    32  	if err := loader.RefreshModules(); err != nil {
    33  		t.Fatalf("failed to refresh modules after install: %s", err)
    34  	}
    35  
    36  	cfg, loadDiags := loader.LoadConfig(configDir)
    37  	if loadDiags.HasErrors() {
    38  		t.Fatalf("unexpected configuration errors: %s", loadDiags.Error())
    39  	}
    40  
    41  	resourceTypeSchema := &configschema.Block{
    42  		Attributes: map[string]*configschema.Attribute{
    43  			"string": {Type: cty.String, Optional: true},
    44  			"number": {Type: cty.Number, Optional: true},
    45  			"any":    {Type: cty.DynamicPseudoType, Optional: true},
    46  		},
    47  		BlockTypes: map[string]*configschema.NestedBlock{
    48  			"single": {
    49  				Nesting: configschema.NestingSingle,
    50  				Block: configschema.Block{
    51  					Attributes: map[string]*configschema.Attribute{
    52  						"z": {Type: cty.String, Optional: true},
    53  					},
    54  				},
    55  			},
    56  			"group": {
    57  				Nesting: configschema.NestingGroup,
    58  				Block: configschema.Block{
    59  					Attributes: map[string]*configschema.Attribute{
    60  						"z": {Type: cty.String, Optional: true},
    61  					},
    62  				},
    63  			},
    64  			"list": {
    65  				Nesting: configschema.NestingList,
    66  				Block: configschema.Block{
    67  					Attributes: map[string]*configschema.Attribute{
    68  						"z": {Type: cty.String, Optional: true},
    69  					},
    70  				},
    71  			},
    72  			"map": {
    73  				Nesting: configschema.NestingMap,
    74  				Block: configschema.Block{
    75  					Attributes: map[string]*configschema.Attribute{
    76  						"z": {Type: cty.String, Optional: true},
    77  					},
    78  				},
    79  			},
    80  			"set": {
    81  				Nesting: configschema.NestingSet,
    82  				Block: configschema.Block{
    83  					Attributes: map[string]*configschema.Attribute{
    84  						"z": {Type: cty.String, Optional: true},
    85  					},
    86  				},
    87  			},
    88  		},
    89  	}
    90  	schemas := map[addrs.Provider]providers.ProviderSchema{
    91  		addrs.MustParseProviderSourceString("hashicorp/test"): {
    92  			ResourceTypes: map[string]providers.Schema{
    93  				"test_thing": {
    94  					Block: resourceTypeSchema,
    95  				},
    96  			},
    97  			DataSources: map[string]providers.Schema{
    98  				"test_thing": {
    99  					Block: resourceTypeSchema,
   100  				},
   101  			},
   102  		},
   103  	}
   104  
   105  	return NewAnalyzer(cfg, schemas)
   106  }