github.com/google/osv-scalibr@v0.4.1/internal/mavenutil/maven_test.go (about) 1 // Copyright 2025 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package mavenutil 16 17 import ( 18 "path/filepath" 19 "testing" 20 21 "deps.dev/util/resolve" 22 "deps.dev/util/semver" 23 "github.com/google/osv-scalibr/testing/extracttest" 24 ) 25 26 func TestParentPOMPath(t *testing.T) { 27 input := extracttest.GenerateScanInputMock(t, extracttest.ScanInputMockConfig{ 28 Path: filepath.Join("testdata", "my-app", "pom.xml"), 29 }) 30 defer extracttest.CloseTestScanInput(t, input) 31 32 tests := []struct { 33 currentPath, relativePath string 34 want string 35 }{ 36 // testdata 37 // |- maven 38 // | |- my-app 39 // | | |- pom.xml 40 // | |- parent 41 // | | |- pom.xml 42 // |- pom.xml 43 { 44 // Parent path is specified correctly. 45 currentPath: filepath.Join("testdata", "my-app", "pom.xml"), 46 relativePath: "../parent/pom.xml", 47 want: filepath.Join("testdata", "parent", "pom.xml"), 48 }, 49 { 50 // Wrong file name is specified in relative path. 51 currentPath: filepath.Join("testdata", "my-app", "pom.xml"), 52 relativePath: "../parent/abc.xml", 53 want: "", 54 }, 55 { 56 // Wrong directory is specified in relative path. 57 currentPath: filepath.Join("testdata", "my-app", "pom.xml"), 58 relativePath: "../not-found/pom.xml", 59 want: "", 60 }, 61 { 62 // Only directory is specified. 63 currentPath: filepath.Join("testdata", "my-app", "pom.xml"), 64 relativePath: "../parent", 65 want: filepath.Join("testdata", "parent", "pom.xml"), 66 }, 67 { 68 // Parent relative path is default to '../pom.xml'. 69 currentPath: filepath.Join("testdata", "my-app", "pom.xml"), 70 relativePath: "", 71 want: filepath.Join("testdata", "pom.xml"), 72 }, 73 { 74 // No pom.xml is found even in the default path. 75 currentPath: filepath.Join("testdata", "pom.xml"), 76 relativePath: "", 77 want: "", 78 }, 79 } 80 for _, tt := range tests { 81 got := ParentPOMPath(&input, tt.currentPath, tt.relativePath) 82 if got != filepath.ToSlash(tt.want) { 83 t.Errorf("ParentPOMPath(%s, %s): got %s, want %s", tt.currentPath, tt.relativePath, got, tt.want) 84 } 85 } 86 } 87 88 func TestCompareVersions(t *testing.T) { 89 versionKey := func(name string, version string) resolve.VersionKey { 90 return resolve.VersionKey{ 91 PackageKey: resolve.PackageKey{ 92 System: resolve.Maven, 93 Name: name, 94 }, 95 Version: version, 96 } 97 } 98 semVer := func(version string) *semver.Version { 99 parsed, _ := resolve.Maven.Semver().Parse(version) 100 return parsed 101 } 102 103 tests := []struct { 104 vk resolve.VersionKey 105 a, b *semver.Version 106 want int 107 }{ 108 { 109 versionKey("abc:xyz", "1.0.0"), 110 semVer("1.2.3"), 111 semVer("1.2.3"), 112 0, 113 }, 114 { 115 versionKey("abc:xyz", "1.0.0"), 116 semVer("1.2.3"), 117 semVer("2.3.4"), 118 -1, 119 }, 120 { 121 versionKey("com.google.guava:guava", "1.0.0"), 122 semVer("1.2.3"), 123 semVer("2.3.4"), 124 -1, 125 }, 126 { 127 versionKey("com.google.guava:guava", "1.0.0"), 128 semVer("1.2.3-jre"), 129 semVer("2.3.4-jre"), 130 -1, 131 }, 132 { 133 versionKey("com.google.guava:guava", "1.0.0"), 134 semVer("1.2.3-android"), 135 semVer("2.3.4-android"), 136 -1, 137 }, 138 { 139 versionKey("com.google.guava:guava", "1.0.0"), 140 semVer("2.3.4-android"), 141 semVer("1.2.3-jre"), 142 -1, 143 }, 144 { 145 versionKey("com.google.guava:guava", "1.0.0-jre"), 146 semVer("1.2.3-android"), 147 semVer("1.2.3-jre"), 148 -1, 149 }, 150 { 151 versionKey("com.google.guava:guava", "1.0.0-android"), 152 semVer("1.2.3-android"), 153 semVer("1.2.3-jre"), 154 1, 155 }, 156 { 157 versionKey("commons-io:commons-io", "1.0.0"), 158 semVer("1.2.3"), 159 semVer("2.3.4"), 160 -1, 161 }, 162 { 163 versionKey("commons-io:commons-io", "1.0.0"), 164 semVer("1.2.3"), 165 semVer("20010101.000000"), 166 1, 167 }, 168 } 169 for _, tt := range tests { 170 got := CompareVersions(tt.vk, tt.a, tt.b) 171 if got != tt.want { 172 t.Errorf("CompareVersions(%v, %v, %v): got %b, want %b", tt.vk, tt.a, tt.b, got, tt.want) 173 } 174 } 175 }