github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/plugin/discovery/version_set_test.go (about)

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