Skip to content

Commit 8628bff

Browse files
a-orenclaude
andcommitted
fix(go): split on \s+ in parseModuleVersions and add unit tests
Use regex whitespace splitting instead of single space to handle tabs and multiple spaces in go list -m all output. Add unit tests covering standard lines, replace directives, varied whitespace, blank lines, malformed input, and duplicate handling. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 8dfee73 commit 8628bff

2 files changed

Lines changed: 88 additions & 1 deletion

File tree

src/main/java/io/github/guacsec/trustifyda/providers/GoModulesProvider.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,8 @@ private Map<String, List<String>> getFinalPackagesVersionsForModule(
390390
static Map<String, String> parseModuleVersions(String goListOutput) {
391391
return Arrays.stream(goListOutput.split(Operations.GENERIC_LINE_SEPARATOR))
392392
.map(String::trim)
393-
.map(line -> line.split(" "))
393+
.filter(line -> !line.isEmpty())
394+
.map(line -> line.split("\\s+"))
394395
.filter(parts -> parts.length == 2 || (parts.length >= 4 && parts[2].equals("=>")))
395396
.collect(
396397
Collectors.toMap(
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2023-2025 Trustify Dependency Analytics Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
*
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package io.github.guacsec.trustifyda.providers;
18+
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
21+
import java.util.Map;
22+
import org.junit.jupiter.api.Test;
23+
24+
class GoModulesParseModuleVersionsTest {
25+
26+
@Test
27+
void parseStandardModuleLines() {
28+
String input = "github.com/foo/bar v1.2.3\ngithub.com/baz/qux v0.1.0\n";
29+
Map<String, String> result = GoModulesProvider.parseModuleVersions(input);
30+
assertThat(result)
31+
.containsEntry("github.com/foo/bar", "v1.2.3")
32+
.containsEntry("github.com/baz/qux", "v0.1.0")
33+
.hasSize(2);
34+
}
35+
36+
@Test
37+
void parseReplaceDirectiveLines() {
38+
String input = "github.com/old/mod v1.0.0 => github.com/new/mod v2.0.0\n";
39+
Map<String, String> result = GoModulesProvider.parseModuleVersions(input);
40+
assertThat(result).containsEntry("github.com/old/mod", "v2.0.0").hasSize(1);
41+
}
42+
43+
@Test
44+
void parseWithMultipleSpacesAndTabs() {
45+
String input = "github.com/foo/bar v1.2.3\ngithub.com/baz/qux\tv0.1.0\n";
46+
Map<String, String> result = GoModulesProvider.parseModuleVersions(input);
47+
assertThat(result)
48+
.containsEntry("github.com/foo/bar", "v1.2.3")
49+
.containsEntry("github.com/baz/qux", "v0.1.0")
50+
.hasSize(2);
51+
}
52+
53+
@Test
54+
void parseWithBlankAndWhitespaceOnlyLines() {
55+
String input = "\n \ngithub.com/foo/bar v1.0.0\n\n";
56+
Map<String, String> result = GoModulesProvider.parseModuleVersions(input);
57+
assertThat(result).containsEntry("github.com/foo/bar", "v1.0.0").hasSize(1);
58+
}
59+
60+
@Test
61+
void parseSkipsMalformedLines() {
62+
String input = "github.com/foo/bar v1.0.0\nsingle-token\nthree tokens here\n";
63+
Map<String, String> result = GoModulesProvider.parseModuleVersions(input);
64+
assertThat(result).containsEntry("github.com/foo/bar", "v1.0.0").hasSize(1);
65+
}
66+
67+
@Test
68+
void parseEmptyInput() {
69+
Map<String, String> result = GoModulesProvider.parseModuleVersions("");
70+
assertThat(result).isEmpty();
71+
}
72+
73+
@Test
74+
void parseReplaceDirectiveWithTabSeparation() {
75+
String input = "github.com/old/mod\tv1.0.0\t=>\tgithub.com/new/mod\tv2.0.0\n";
76+
Map<String, String> result = GoModulesProvider.parseModuleVersions(input);
77+
assertThat(result).containsEntry("github.com/old/mod", "v2.0.0").hasSize(1);
78+
}
79+
80+
@Test
81+
void parseDuplicateModuleKeepsLast() {
82+
String input = "github.com/foo/bar v1.0.0\ngithub.com/foo/bar v2.0.0\n";
83+
Map<String, String> result = GoModulesProvider.parseModuleVersions(input);
84+
assertThat(result).containsEntry("github.com/foo/bar", "v2.0.0").hasSize(1);
85+
}
86+
}

0 commit comments

Comments
 (0)