github.com/quay/claircore@v1.5.28/java/maven_version_test.go (about)

     1  package java
     2  
     3  import "testing"
     4  
     5  func TestMavenVersions(t *testing.T) {
     6  	t.Run("Parse", func(t *testing.T) {
     7  		tc := []string{
     8  			"1.0",
     9  			"1.0.1",
    10  			"1-SNAPSHOT",
    11  			"1-alpha10-SNAPSHOT",
    12  		}
    13  		for _, in := range tc {
    14  			v, err := parseMavenVersion(in)
    15  			t.Logf("in: %q, got: %q", in, v.C.String())
    16  			if err != nil {
    17  				t.Error(err)
    18  			}
    19  		}
    20  	})
    21  	t.Run("Compare", func(t *testing.T) {
    22  		op := map[int]string{
    23  			0:  "==",
    24  			1:  ">",
    25  			-1: "<",
    26  		}
    27  		tc := []struct {
    28  			aIn, bIn string
    29  			want     int
    30  		}{
    31  			// From the wiki
    32  			{aIn: "1.0", bIn: "1.0-alpha", want: 1},
    33  			{aIn: "1.0", bIn: "1", want: 0},
    34  			{aIn: "1-beta", bIn: "1-xyz", want: -1},
    35  			{aIn: "1-beta", bIn: "1-abc", want: -1},
    36  			{aIn: "1.0", bIn: "1.0-abc", want: -1},
    37  			{aIn: "1.0-alpha-10", bIn: "1.0-alpha-2", want: 1},
    38  			{aIn: "1.0-alpha-1.0", bIn: "1.0-alpha-1", want: 0},
    39  			{aIn: "1.0-alpha-1.2", bIn: "1.0-alpha-2", want: -1},
    40  			// ported from the maven tests: https://github.com/apache/maven/blob/maven-3.9.x/maven-artifact/src/test/java/org/apache/maven/artifact/versioning/ComparableVersionTest.java
    41  			{"1", "1", 0},
    42  			{"1", "1.0", 0},
    43  			{"1", "1.0.0", 0},
    44  			{"1.0", "1.0.0", 0},
    45  			{"1", "1-0", 0},
    46  			{"1", "1.0-0", 0},
    47  			{"1.0", "1.0-0", 0},
    48  			// no separator between number and character
    49  			{"1a", "1-a", 0},
    50  			{"1a", "1.0-a", 0},
    51  			{"1a", "1.0.0-a", 0},
    52  			{"1.0a", "1-a", 0},
    53  			{"1.0.0a", "1-a", 0},
    54  			{"1x", "1-x", 0},
    55  			{"1x", "1.0-x", 0},
    56  			{"1x", "1.0.0-x", 0},
    57  			{"1.0x", "1-x", 0},
    58  			{"1.0.0x", "1-x", 0},
    59  			// aliases
    60  			{"1ga", "1", 0},
    61  			{"1release", "1", 0},
    62  			{"1final", "1", 0},
    63  			{"1cr", "1rc", 0},
    64  			// special "aliases" a, b and m for alpha, beta and milestone
    65  			{"1a1", "1-alpha-1", 0},
    66  			{"1b2", "1-beta-2", 0},
    67  			{"1m3", "1-milestone-3", 0},
    68  			// case insensitive
    69  			{"1X", "1x", 0},
    70  			{"1A", "1a", 0},
    71  			{"1B", "1b", 0},
    72  			{"1M", "1m", 0},
    73  			{"1Ga", "1", 0},
    74  			{"1GA", "1", 0},
    75  			{"1RELEASE", "1", 0},
    76  			{"1release", "1", 0},
    77  			{"1RELeaSE", "1", 0},
    78  			{"1Final", "1", 0},
    79  			{"1FinaL", "1", 0},
    80  			{"1FINAL", "1", 0},
    81  			{"1Cr", "1Rc", 0},
    82  			{"1cR", "1rC", 0},
    83  			{"1m3", "1Milestone3", 0},
    84  			{"1m3", "1MileStone3", 0},
    85  			{"1m3", "1MILESTONE3", 0},
    86  			// nonequalities:
    87  			{"1", "2", -1},
    88  			{"1.5", "2", -1},
    89  			{"1", "2.5", -1},
    90  			{"1.0", "1.1", -1},
    91  			{"1.1", "1.2", -1},
    92  			{"1.0.0", "1.1", -1},
    93  			{"1.0.1", "1.1", -1},
    94  			{"1.1", "1.2.0", -1},
    95  			{"1.0-alpha-1", "1.0", -1},
    96  			{"1.0-alpha-1", "1.0-alpha-2", -1},
    97  			{"1.0-alpha-1", "1.0-beta-1", -1},
    98  			{"1.0-beta-1", "1.0-SNAPSHOT", -1},
    99  			{"1.0-SNAPSHOT", "1.0", -1},
   100  			{"1.0-alpha-1-SNAPSHOT", "1.0-alpha-1", -1},
   101  			{"1.0", "1.0-1", -1},
   102  			{"1.0-1", "1.0-2", -1},
   103  			{"1.0.0", "1.0-1", -1},
   104  			{"2.0-1", "2.0.1", -1},
   105  			{"2.0.1-klm", "2.0.1-lmn", -1},
   106  			{"2.0.1", "2.0.1-xyz", -1},
   107  			{"2.0.1", "2.0.1-123", -1},
   108  			{"2.0.1-xyz", "2.0.1-123", -1},
   109  			// MNG-5568
   110  			{"6.1.0rc3", "6.1.0", -1},
   111  			{"6.1.0rc3", "6.1H.5-beta", -1},
   112  			{"6.1.0", "6.1H.5-beta", -1},
   113  			// MNG-6572
   114  			{"20190126.230843", "1234567890.12345", -1},
   115  			{"1234567890.12345", "123456789012345.1H.5-beta", -1},
   116  			{"20190126.230843", "123456789012345.1H.5-beta", -1},
   117  			{"123456789012345.1H.5-beta", "12345678901234567890.1H.5-beta", -1},
   118  			{"1234567890.12345", "12345678901234567890.1H.5-beta", -1},
   119  			{"20190126.230843", "12345678901234567890.1H.5-beta", -1},
   120  			// MNG-6964
   121  			{"1-0.alpha", "1", -1},
   122  			{"1-0.beta", "1", -1},
   123  			{"1-0.alpha", "1-0.beta", -1},
   124  			// github.com/masahiro331/go-mvn-version
   125  			{"2.0.a", "2.0.0.a", -1},
   126  		}
   127  		for _, tc := range tc {
   128  			a, err := parseMavenVersion(tc.aIn)
   129  			if err != nil {
   130  				t.Error(err)
   131  			}
   132  			b, err := parseMavenVersion(tc.bIn)
   133  			if err != nil {
   134  				t.Error(err)
   135  			}
   136  			got, want := a.Compare(b), tc.want
   137  			t.Log(tc.aIn, op[got], tc.bIn)
   138  			if got != want {
   139  				t.Logf("a: %+v", a.C)
   140  				t.Logf("b: %+v", b.C)
   141  				t.Errorf("wanted: %s %s %s", tc.aIn, op[want], tc.bIn)
   142  			}
   143  			if want == 0 {
   144  				continue
   145  			}
   146  			got, want = b.Compare(a), -1*tc.want
   147  			t.Log(tc.bIn, op[got], tc.aIn)
   148  			if got != want {
   149  				t.Logf("b: %+v", b.C)
   150  				t.Logf("a: %+v", a.C)
   151  				t.Errorf("wanted: %s %s %s", tc.bIn, op[want], tc.aIn)
   152  			}
   153  		}
   154  	})
   155  }