github.com/iaas-resource-provision/iaas-rpc@v1.0.7-0.20211021023331-ed21f798c408/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 }