|
| 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