github.com/muratcelep/terraform@v1.1.0-beta2-not-internal-4/not-internal/getproviders/types_test.go (about)

     1  package getproviders
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestVersionConstraintsString(t *testing.T) {
     8  	testCases := map[string]struct {
     9  		spec VersionConstraints
    10  		want string
    11  	}{
    12  		"exact": {
    13  			MustParseVersionConstraints("1.2.3"),
    14  			"1.2.3",
    15  		},
    16  		"prerelease": {
    17  			MustParseVersionConstraints("1.2.3-beta"),
    18  			"1.2.3-beta",
    19  		},
    20  		"metadata": {
    21  			MustParseVersionConstraints("1.2.3+foo.bar"),
    22  			"1.2.3+foo.bar",
    23  		},
    24  		"prerelease and metadata": {
    25  			MustParseVersionConstraints("1.2.3-beta+foo.bar"),
    26  			"1.2.3-beta+foo.bar",
    27  		},
    28  		"major only": {
    29  			MustParseVersionConstraints(">= 3"),
    30  			">= 3.0.0",
    31  		},
    32  		"major only with pessimistic operator": {
    33  			MustParseVersionConstraints("~> 3"),
    34  			"~> 3.0",
    35  		},
    36  		"pessimistic minor": {
    37  			MustParseVersionConstraints("~> 3.0"),
    38  			"~> 3.0",
    39  		},
    40  		"pessimistic patch": {
    41  			MustParseVersionConstraints("~> 3.0.0"),
    42  			"~> 3.0.0",
    43  		},
    44  		"other operators": {
    45  			MustParseVersionConstraints("> 1.0.0, < 1.0.0, >= 1.0.0, <= 1.0.0, != 1.0.0"),
    46  			"> 1.0.0, >= 1.0.0, <= 1.0.0, < 1.0.0, != 1.0.0",
    47  		},
    48  		"multiple": {
    49  			MustParseVersionConstraints(">= 3.0, < 4.0"),
    50  			">= 3.0.0, < 4.0.0",
    51  		},
    52  		"duplicates removed": {
    53  			MustParseVersionConstraints(">= 1.2.3, 1.2.3, ~> 1.2, 1.2.3"),
    54  			"~> 1.2, >= 1.2.3, 1.2.3",
    55  		},
    56  		"equivalent duplicates removed": {
    57  			MustParseVersionConstraints(">= 2.68, >= 2.68.0"),
    58  			">= 2.68.0",
    59  		},
    60  		"consistent ordering, exhaustive": {
    61  			// This weird jumble is just to exercise the different sort
    62  			// ordering codepaths. Hopefully nothing quite this horrific
    63  			// shows up often in practice.
    64  			MustParseVersionConstraints("< 1.2.3, <= 1.2.3, != 1.2.3, 1.2.3+local.2, 1.2.3+local.1, = 1.2.4, = 1.2.3, > 2, > 1.2.3, >= 1.2.3, ~> 1.2.3, ~> 1.2"),
    65  			"~> 1.2, > 1.2.3, >= 1.2.3, 1.2.3, ~> 1.2.3, <= 1.2.3, < 1.2.3, != 1.2.3, 1.2.3+local.1, 1.2.3+local.2, 1.2.4, > 2.0.0",
    66  		},
    67  		"consistent ordering, more typical": {
    68  			// This one is aiming to simulate a common situation where
    69  			// various different modules express compatible constraints
    70  			// but some modules are more constrained than others. The
    71  			// combined results here can be kinda confusing, but hopefully
    72  			// ordering them consistently makes them a _little_ easier to read.
    73  			MustParseVersionConstraints("~> 1.2, >= 1.2, 1.2.4"),
    74  			">= 1.2.0, ~> 1.2, 1.2.4",
    75  		},
    76  		"consistent ordering, disjoint": {
    77  			// One situation where our presentation of version constraints is
    78  			// particularly important is when a user accidentally ends up with
    79  			// disjoint constraints that can therefore never match. In that
    80  			// case, our ordering should hopefully make it easier to determine
    81  			// that the constraints are disjoint, as a first step to debugging,
    82  			// by showing > or >= constrains sorted after < or <= constraints.
    83  			MustParseVersionConstraints(">= 2, >= 1.2, < 1.3"),
    84  			">= 1.2.0, < 1.3.0, >= 2.0.0",
    85  		},
    86  	}
    87  	for name, tc := range testCases {
    88  		t.Run(name, func(t *testing.T) {
    89  			got := VersionConstraintsString(tc.spec)
    90  
    91  			if got != tc.want {
    92  				t.Errorf("wrong\n got: %q\nwant: %q", got, tc.want)
    93  			}
    94  		})
    95  	}
    96  }
    97  
    98  func TestParsePlatform(t *testing.T) {
    99  	tests := []struct {
   100  		Input string
   101  		Want  Platform
   102  		Err   bool
   103  	}{
   104  		{
   105  			"",
   106  			Platform{},
   107  			true,
   108  		},
   109  		{
   110  			"too_many_notes",
   111  			Platform{},
   112  			true,
   113  		},
   114  		{
   115  			"extra _ whitespaces ",
   116  			Platform{},
   117  			true,
   118  		},
   119  		{
   120  			"arbitrary_os",
   121  			Platform{OS: "arbitrary", Arch: "os"},
   122  			false,
   123  		},
   124  	}
   125  
   126  	for _, test := range tests {
   127  		got, err := ParsePlatform(test.Input)
   128  		if err != nil {
   129  			if test.Err == false {
   130  				t.Errorf("unexpected error: %s", err.Error())
   131  			}
   132  		} else {
   133  			if test.Err {
   134  				t.Errorf("wrong result: expected error, got none")
   135  			}
   136  		}
   137  		if got != test.Want {
   138  			t.Errorf("wrong\n got: %q\nwant: %q", got, test.Want)
   139  		}
   140  	}
   141  }