github.com/kanishk98/terraform@v1.3.0-dev.0.20220917174235-661ca8088a6a/internal/moduledeps/module_test.go (about)

     1  package moduledeps
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/internal/addrs"
     9  	"github.com/hashicorp/terraform/internal/plugin/discovery"
    10  )
    11  
    12  func TestModuleWalkTree(t *testing.T) {
    13  	type walkStep struct {
    14  		Path       []string
    15  		ParentName string
    16  	}
    17  
    18  	tests := []struct {
    19  		Root      *Module
    20  		WalkOrder []walkStep
    21  	}{
    22  		{
    23  			&Module{
    24  				Name:     "root",
    25  				Children: nil,
    26  			},
    27  			[]walkStep{
    28  				{
    29  					Path:       []string{"root"},
    30  					ParentName: "",
    31  				},
    32  			},
    33  		},
    34  		{
    35  			&Module{
    36  				Name: "root",
    37  				Children: []*Module{
    38  					{
    39  						Name: "child",
    40  					},
    41  				},
    42  			},
    43  			[]walkStep{
    44  				{
    45  					Path:       []string{"root"},
    46  					ParentName: "",
    47  				},
    48  				{
    49  					Path:       []string{"root", "child"},
    50  					ParentName: "root",
    51  				},
    52  			},
    53  		},
    54  		{
    55  			&Module{
    56  				Name: "root",
    57  				Children: []*Module{
    58  					{
    59  						Name: "child",
    60  						Children: []*Module{
    61  							{
    62  								Name: "grandchild",
    63  							},
    64  						},
    65  					},
    66  				},
    67  			},
    68  			[]walkStep{
    69  				{
    70  					Path:       []string{"root"},
    71  					ParentName: "",
    72  				},
    73  				{
    74  					Path:       []string{"root", "child"},
    75  					ParentName: "root",
    76  				},
    77  				{
    78  					Path:       []string{"root", "child", "grandchild"},
    79  					ParentName: "child",
    80  				},
    81  			},
    82  		},
    83  		{
    84  			&Module{
    85  				Name: "root",
    86  				Children: []*Module{
    87  					{
    88  						Name: "child1",
    89  						Children: []*Module{
    90  							{
    91  								Name: "grandchild1",
    92  							},
    93  						},
    94  					},
    95  					{
    96  						Name: "child2",
    97  						Children: []*Module{
    98  							{
    99  								Name: "grandchild2",
   100  							},
   101  						},
   102  					},
   103  				},
   104  			},
   105  			[]walkStep{
   106  				{
   107  					Path:       []string{"root"},
   108  					ParentName: "",
   109  				},
   110  				{
   111  					Path:       []string{"root", "child1"},
   112  					ParentName: "root",
   113  				},
   114  				{
   115  					Path:       []string{"root", "child1", "grandchild1"},
   116  					ParentName: "child1",
   117  				},
   118  				{
   119  					Path:       []string{"root", "child2"},
   120  					ParentName: "root",
   121  				},
   122  				{
   123  					Path:       []string{"root", "child2", "grandchild2"},
   124  					ParentName: "child2",
   125  				},
   126  			},
   127  		},
   128  	}
   129  
   130  	for i, test := range tests {
   131  		t.Run(fmt.Sprintf("%02d", i), func(t *testing.T) {
   132  			wo := test.WalkOrder
   133  			test.Root.WalkTree(func(path []string, parent *Module, current *Module) error {
   134  				if len(wo) == 0 {
   135  					t.Fatalf("ran out of walk steps while expecting one for %#v", path)
   136  				}
   137  				step := wo[0]
   138  				wo = wo[1:]
   139  				if got, want := path, step.Path; !reflect.DeepEqual(got, want) {
   140  					t.Errorf("wrong path %#v; want %#v", got, want)
   141  				}
   142  				parentName := ""
   143  				if parent != nil {
   144  					parentName = parent.Name
   145  				}
   146  				if got, want := parentName, step.ParentName; got != want {
   147  					t.Errorf("wrong parent name %q; want %q", got, want)
   148  				}
   149  
   150  				if got, want := current.Name, path[len(path)-1]; got != want {
   151  					t.Errorf("mismatching current.Name %q and final path element %q", got, want)
   152  				}
   153  				return nil
   154  			})
   155  		})
   156  	}
   157  }
   158  
   159  func TestModuleSortChildren(t *testing.T) {
   160  	m := &Module{
   161  		Name: "root",
   162  		Children: []*Module{
   163  			{
   164  				Name: "apple",
   165  			},
   166  			{
   167  				Name: "zebra",
   168  			},
   169  			{
   170  				Name: "xylophone",
   171  			},
   172  			{
   173  				Name: "pig",
   174  			},
   175  		},
   176  	}
   177  
   178  	m.SortChildren()
   179  
   180  	want := []string{"apple", "pig", "xylophone", "zebra"}
   181  	var got []string
   182  	for _, c := range m.Children {
   183  		got = append(got, c.Name)
   184  	}
   185  
   186  	if !reflect.DeepEqual(want, got) {
   187  		t.Errorf("wrong order %#v; want %#v", want, got)
   188  	}
   189  }
   190  
   191  func TestModuleProviderRequirements(t *testing.T) {
   192  	m := &Module{
   193  		Name: "root",
   194  		Providers: Providers{
   195  			addrs.NewDefaultProvider("foo"): ProviderDependency{
   196  				Constraints: discovery.ConstraintStr(">=1.0.0").MustParse(),
   197  			},
   198  			addrs.NewDefaultProvider("baz"): ProviderDependency{
   199  				Constraints: discovery.ConstraintStr(">=3.0.0").MustParse(),
   200  			},
   201  		},
   202  	}
   203  
   204  	reqd := m.ProviderRequirements()
   205  	if len(reqd) != 2 {
   206  		t.Errorf("wrong number of elements in %#v; want 2", reqd)
   207  	}
   208  	if got, want := reqd["foo"].Versions.String(), ">=1.0.0"; got != want {
   209  		t.Errorf("wrong combination of versions for 'foo' %q; want %q", got, want)
   210  	}
   211  	if got, want := reqd["baz"].Versions.String(), ">=3.0.0"; got != want {
   212  		t.Errorf("wrong combination of versions for 'baz' %q; want %q", got, want)
   213  	}
   214  }