From e5aa1a0e5c418e5fb08b60e13564d009bc440cab Mon Sep 17 00:00:00 2001 From: Lorenzo Corallo Date: Sat, 17 Oct 2026 19:44:09 +0200 Subject: [PATCH] test(autostart): keep the Linux tests away from an agent installed on the machine TestInstalledParsesEntries redirected XDG_CONFIG_HOME but still read the package's unit in /usr/lib/systemd/user and its global enablement in /etc/systemd/user, so on a machine with the agent installed Installed() found it and the test failed. The tests now point those paths at a temporary directory too, and a new test covers the package unit, which counts only once enabled for every user. --- internal/autostart/linux_test.go | 38 +++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/internal/autostart/linux_test.go b/internal/autostart/linux_test.go index a26b52f..db51946 100644 --- a/internal/autostart/linux_test.go +++ b/internal/autostart/linux_test.go @@ -8,8 +8,20 @@ import ( "testing" ) -func TestInstalledParsesEntries(t *testing.T) { +// isolate keeps the tests away from the machine's own registration: a user config directory and package unit paths +// in a temporary directory, so an agent installed on the machine running the tests does not count. +func isolate(t *testing.T) { + t.Helper() t.Setenv("XDG_CONFIG_HOME", t.TempDir()) + system := t.TempDir() + units, enabled := systemUnits, globalEnabled + t.Cleanup(func() { systemUnits, globalEnabled = units, enabled }) + systemUnits = []string{filepath.Join(system, "lib", UnitName), filepath.Join(system, "etc", UnitName)} + globalEnabled = filepath.Join(system, "etc", "default.target.wants", UnitName) +} + +func TestInstalledParsesEntries(t *testing.T) { + isolate(t) unit, desktop, err := paths() if err != nil { t.Fatal(err) @@ -37,3 +49,27 @@ func TestInstalledParsesEntries(t *testing.T) { t.Fatalf("systemd unit: %q, %v", got, ok) } } + +// TestInstalledPackageUnit: the package's unit counts only once enabled for every user. +func TestInstalledPackageUnit(t *testing.T) { + isolate(t) + exe := "/usr/bin/polinetwork-esign" + if err := os.MkdirAll(filepath.Dir(systemUnits[0]), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(systemUnits[0], []byte(SystemdUnit(exe)), 0o644); err != nil { + t.Fatal(err) + } + if _, ok := Installed(); ok { + t.Fatal("installed while the package unit is not enabled") + } + if err := os.MkdirAll(filepath.Dir(globalEnabled), 0o755); err != nil { + t.Fatal(err) + } + if err := os.Symlink(systemUnits[0], globalEnabled); err != nil { + t.Fatal(err) + } + if got, ok := Installed(); !ok || got != exe { + t.Fatalf("package unit: %q, %v", got, ok) + } +}