From 7c7bcedcc5bdb4e5a7f64097befa363a5994a96d Mon Sep 17 00:00:00 2001 From: Andrew Walsh Date: Sun, 26 Nov 2023 21:26:22 -0800 Subject: [PATCH] Account for colons in version value. --- apt_query.go | 6 +++--- apt_query_test.go | 15 +++++++++++---- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/apt_query.go b/apt_query.go index 665933c..579aa75 100644 --- a/apt_query.go +++ b/apt_query.go @@ -62,10 +62,10 @@ func getPackages(names []string) AptPackages { pkg := AptPackage{} for _, line := range strings.Split(paragraph, "\n") { if strings.HasPrefix(line, "Package: ") { - pkg.Name = strings.TrimSpace(strings.Split(line, ":")[1]) + pkg.Name = strings.TrimSpace(strings.SplitN(line, ":", 2)[1]) } else if strings.HasPrefix(line, "Version: ") { - pkg.Version = strings.TrimSpace(strings.Split(line, ":")[1]) - } else if strings.HasPrefix(line, "N: Unable to locate package ") || strings.HasPrefix(line, "E: ") { + pkg.Version = strings.TrimSpace(strings.SplitN(line, ":", 2)[1]) + } else if strings.HasPrefix(line, "N: ") || strings.HasPrefix(line, "E: ") { if !contains(errorMessages, line) { errorMessages = append(errorMessages, line) } diff --git a/apt_query_test.go b/apt_query_test.go index ea585d3..e091985 100644 --- a/apt_query_test.go +++ b/apt_query_test.go @@ -31,14 +31,16 @@ func (r *RunResult) expectSuccessfulOut(expected string) { if r.Stderr != "" { r.TestContext.Errorf("Unexpected stderr messages found.\nExpected: none\nActual:\n'%s'", r.Stderr) } - if r.Stdout != expected+"\n" { // Output will always have a end of output newline. - r.TestContext.Errorf("Unexpected stdout found.\nExpected:\n'%s'\nActual:\n'%s'", expected, r.Stdout) + fullExpected := expected + "\n" // Output will always have a end of output newline. + if r.Stdout != fullExpected { // Output will always have a end of output newline. + r.TestContext.Errorf("Unexpected stdout found.\nExpected:\n'%s'\nActual:\n'%s'", fullExpected, r.Stdout) } } func (r *RunResult) expectError(expected string) { - if r.Stderr != expected+"\n" { // Output will always have a end of output newline. - r.TestContext.Errorf("Unexpected stderr found.\nExpected:\n'%s'\nActual:\n'%s'", expected, r.Stderr) + fullExpected := expected + "\n" // Output will always have a end of output newline. + if r.Stderr != fullExpected { + r.TestContext.Errorf("Unexpected stderr found.\nExpected:\n'%s'\nActual:\n'%s'", fullExpected, r.Stderr) } } @@ -62,6 +64,11 @@ func TestNormalizedList_SinglePackageExists_StdoutsSinglePackageNameVersionPair( result.expectSuccessfulOut("xdot=1.2-3") } +func TestNormalizedList_VersionContainsColon_StdoutsEntireVersion(t *testing.T) { + var result = run(t, "normalized-list", "default-jre") + result.expectSuccessfulOut("default-jre=2:1.17-74") +} + func TestNormalizedList_NonExistentPackageName_StderrsAptCacheErrors(t *testing.T) { var result = run(t, "normalized-list", "nonexistentpackagename") result.expectError(