mirror of
https://github.com/awalsh128/cache-apt-pkgs-action.git
synced 2026-06-16 04:04:53 +00:00
test: add apt warning regression coverage
This commit is contained in:
parent
4c083a0635
commit
47536a99e2
|
|
@ -10,7 +10,7 @@
|
|||
EXECUTION-OBJ-END
|
||||
2025/03/15 22:29:14 EXECUTION-OBJ-START
|
||||
{
|
||||
"Cmd": "bash -c apt-cache showpkg libvips | grep -A 1 \"Reverse Provides\" | tail -1",
|
||||
"Cmd": "apt-cache showpkg libvips",
|
||||
"Stdout": "libvips42 8.9.1-2 (= )\n",
|
||||
"Stderr": "",
|
||||
"CombinedOut": "libvips42 8.9.1-2 (= )\n",
|
||||
|
|
|
|||
|
|
@ -33,20 +33,33 @@ func isErrLine(line string) bool {
|
|||
|
||||
// Resolves virtual packages names to their concrete one.
|
||||
func getNonVirtualPackage(executor exec.Executor, name string) (pkg *AptPackage, err error) {
|
||||
execution := executor.Exec("bash", "-c", fmt.Sprintf("apt-cache showpkg %s | grep -A 1 \"Reverse Provides\" | tail -1", name))
|
||||
execution := executor.Exec("apt-cache", "showpkg", name)
|
||||
err = execution.Error()
|
||||
if err != nil {
|
||||
logging.Fatal(err)
|
||||
return pkg, err
|
||||
}
|
||||
if isErrLine(execution.CombinedOut) {
|
||||
return pkg, execution.Error()
|
||||
|
||||
inReverseProvides := false
|
||||
for _, line := range strings.Split(execution.CombinedOut, "\n") {
|
||||
trimmed := strings.TrimSpace(line)
|
||||
if trimmed == "" {
|
||||
continue
|
||||
}
|
||||
if trimmed == "Reverse Provides:" {
|
||||
inReverseProvides = true
|
||||
continue
|
||||
}
|
||||
if !inReverseProvides || strings.HasPrefix(trimmed, "W: ") || isErrLine(trimmed) {
|
||||
continue
|
||||
}
|
||||
splitLine := GetSplitLine(trimmed, " ", 3)
|
||||
if len(splitLine.Words) < 2 {
|
||||
continue
|
||||
}
|
||||
return &AptPackage{Name: splitLine.Words[0], Version: splitLine.Words[1]}, nil
|
||||
}
|
||||
splitLine := GetSplitLine(execution.CombinedOut, " ", 3)
|
||||
if len(splitLine.Words) < 2 {
|
||||
return pkg, fmt.Errorf("unable to parse space delimited line's package name and version from apt-cache showpkg output below:\n%s", execution.CombinedOut)
|
||||
}
|
||||
return &AptPackage{Name: splitLine.Words[0], Version: splitLine.Words[1]}, nil
|
||||
return pkg, fmt.Errorf("unable to parse reverse provides package name and version from apt-cache showpkg output below:\n%s", execution.CombinedOut)
|
||||
}
|
||||
|
||||
func getPackage(executor exec.Executor, paragraph string) (pkg *AptPackage, err error) {
|
||||
|
|
|
|||
53
src/internal/common/apt_test.go
Normal file
53
src/internal/common/apt_test.go
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
execpkg "awalsh128.com/cache-apt-pkgs-action/src/internal/exec"
|
||||
)
|
||||
|
||||
type stubExecutor struct {
|
||||
executions map[string]*execpkg.Execution
|
||||
}
|
||||
|
||||
func (s stubExecutor) Exec(name string, arg ...string) *execpkg.Execution {
|
||||
cmd := name + " " + strings.Join(arg, " ")
|
||||
execution, ok := s.executions[cmd]
|
||||
if !ok {
|
||||
panic(fmt.Sprintf("unexpected command: %s", cmd))
|
||||
}
|
||||
return execution
|
||||
}
|
||||
|
||||
func TestGetNonVirtualPackage_IgnoresWarningsAfterReverseProvides(t *testing.T) {
|
||||
executor := stubExecutor{
|
||||
executions: map[string]*execpkg.Execution{
|
||||
"apt-cache showpkg libopenblas0-openmp": {
|
||||
Cmd: "apt-cache showpkg libopenblas0-openmp",
|
||||
CombinedOut: strings.Join([]string{
|
||||
"Package: libopenblas0-openmp",
|
||||
"Reverse Provides:",
|
||||
"libopenblas0-openmp 0.3.26+ds-1ubuntu0.1 (= )",
|
||||
"W: Unable to read /etc/apt/apt.conf.d/99github-actions - open (13: Permission denied)",
|
||||
"",
|
||||
}, "\n"),
|
||||
ExitCode: 0,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
pkg, err := getNonVirtualPackage(executor, "libopenblas0-openmp")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if pkg == nil {
|
||||
t.Fatal("expected package but got nil")
|
||||
}
|
||||
|
||||
expected := AptPackage{Name: "libopenblas0-openmp", Version: "0.3.26+ds-1ubuntu0.1"}
|
||||
if *pkg != expected {
|
||||
t.Fatalf("unexpected package.\nexpected: %+v\nactual: %+v", expected, *pkg)
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue