Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion internal/collector/updates.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,18 @@ func (c *UpdatesCollector) Collect(ctx context.Context) (Updates, error) {
defer cancel()

cmd := exec.CommandContext(ctx, c.aptPath, "list", "--upgradable")
cmd.Env = append(os.Environ(), "DEBIAN_FRONTEND=noninteractive")
// apt localises its output via gettext; the parser above matches the
// English strings, so pin the locale rather than inheriting the
// operator's. LC_ALL beats LANG and LANGUAGE, but all three are set so
// no inherited value can leak through. PATH must be preserved because
// aptPath is typically the bare string "apt", resolved via PATH.
cmd.Env = []string{
"LC_ALL=C",
"LANG=C",
"LANGUAGE=",
"DEBIAN_FRONTEND=noninteractive",
"PATH=" + os.Getenv("PATH"),
}
out, err := cmd.Output()
if err != nil {
return Updates{}, fmt.Errorf("run apt list --upgradable: %w", err)
Expand Down
47 changes: 47 additions & 0 deletions internal/collector/updates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package collector

import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
Expand Down Expand Up @@ -92,3 +94,48 @@ func TestUpdatesCollector_Collect_StalenessDetection(t *testing.T) {
t.Fatalf("expected Stale=true for a 48h old cache with a 6h threshold, got %+v", updates)
}
}

func TestUpdatesCollector_Collect_PinsLocale(t *testing.T) {
dir := t.TempDir()
envFile := filepath.Join(dir, "env.out")
aptStub := writeFakeVcgencmd(t, dir, "fake-apt", fmt.Sprintf("env > %q", envFile))

c := &UpdatesCollector{
aptPath: aptStub,
listsDir: filepath.Join(dir, "missing-lists"),
now: time.Now,
}

if _, err := c.Collect(context.Background()); err != nil {
t.Fatalf("Collect: %v", err)
}

out, err := os.ReadFile(envFile)
if err != nil {
t.Fatalf("read captured child environment: %v", err)
}

env := map[string]string{}
for _, line := range strings.Split(strings.TrimSpace(string(out)), "\n") {
k, v, ok := strings.Cut(line, "=")
if !ok {
continue
}
env[k] = v
}

if env["LC_ALL"] != "C" {
t.Errorf("LC_ALL = %q, want C", env["LC_ALL"])
}
if env["LANG"] != "C" {
t.Errorf("LANG = %q, want C", env["LANG"])
}
if v, ok := env["LANGUAGE"]; ok && v != "" {
t.Errorf("LANGUAGE = %q, want empty", v)
}
// Regression guard: PATH must survive, otherwise "apt" (a bare command
// name, resolved via PATH) would not be executable at all.
if env["PATH"] == "" {
t.Error("PATH not present in child environment; apt would not be resolvable")
}
}
Loading