github.com/opentofu/opentofu@v1.7.1/internal/plugin/discovery/version_set_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 discovery
     7  
     8  import (
     9  	"fmt"
    10  	"testing"
    11  )
    12  
    13  func TestVersionSet(t *testing.T) {
    14  	tests := []struct {
    15  		ConstraintStr string
    16  		VersionStr    string
    17  		ShouldHave    bool
    18  	}{
    19  		// These test cases are not exhaustive since the underlying go-version
    20  		// library is well-tested. This is mainly here just to exercise our
    21  		// wrapper code, but also used as an opportunity to cover some basic
    22  		// but important cases such as the ~> constraint so that we'll be more
    23  		// likely to catch any accidental breaking behavior changes in the
    24  		// underlying library.
    25  		{
    26  			">=1.0.0",
    27  			"1.0.0",
    28  			true,
    29  		},
    30  		{
    31  			">=1.0.0",
    32  			"0.0.0",
    33  			false,
    34  		},
    35  		{
    36  			">=1.0.0",
    37  			"1.1.0-beta1",
    38  			false,
    39  		},
    40  		{
    41  			">=1.0.0",
    42  			"1.1.0",
    43  			true,
    44  		},
    45  		{
    46  			"~>1.1.0-a",
    47  			"1.1.0-beta1",
    48  			true,
    49  		},
    50  		{
    51  			"~>1.1.0",
    52  			"1.1.2",
    53  			true,
    54  		},
    55  		{
    56  			"~>1.1.0",
    57  			"1.2.0",
    58  			false,
    59  		},
    60  	}
    61  
    62  	for _, test := range tests {
    63  		t.Run(fmt.Sprintf("%s has %s", test.ConstraintStr, test.VersionStr), func(t *testing.T) {
    64  			accepted, err := ConstraintStr(test.ConstraintStr).Parse()
    65  			if err != nil {
    66  				t.Fatalf("unwanted error parsing constraints string %q: %s", test.ConstraintStr, err)
    67  			}
    68  
    69  			version, err := VersionStr(test.VersionStr).Parse()
    70  			if err != nil {
    71  				t.Fatalf("unwanted error parsing version string %q: %s", test.VersionStr, err)
    72  			}
    73  
    74  			if got, want := accepted.Allows(version), test.ShouldHave; got != want {
    75  				t.Errorf("Has returned %#v; want %#v", got, want)
    76  			}
    77  		})
    78  	}
    79  }