github.com/opentofu/opentofu@v1.7.1/internal/lang/globalref/analyzer_test.go (about)

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