package installer import ( "os" "path/filepath" "runtime" "testing" "gitgud.io/mike/mpv-manager/pkg/constants" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) // installFakeTools creates executable POSIX shell scripts that print canned // command output and prepends their directory to PATH, so the version-probe // parsing loops can be exercised deterministically without real package // managers. Each value is the script body (without the shebang). func installFakeTools(t *testing.T, tools map[string]string) { t.Helper() if runtime.GOOS == "windows" { t.Skip("fake tool scripts require a POSIX shell") } dir := t.TempDir() for name, body := range tools { require.NoError(t, os.WriteFile(filepath.Join(dir, name), []byte("#!/bin/sh\n"+body), 0755)) } t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH")) } func fakeToolScripts() map[string]string { return map[string]string{ "flatpak": "cat <<'MPV_FAKE_EOF'\nID: io.mpv.Mpv\nVersion: 0.39.0\nMPV_FAKE_EOF\n", "apt-get": "exit 0\n", // GetPackageVersion matches "Installed:" at line start, so no indent here. "apt-cache": "cat <<'MPV_FAKE_EOF'\nmpv:\nInstalled: 0.39.0\nCandidate: 0.39.0\nMPV_FAKE_EOF\n", "dnf": "cat <<'MPV_FAKE_EOF'\nInstalled Packages\nName : mpv\nVersion : 7.0.1\nMPV_FAKE_EOF\n", "rpm": "echo '1.2-3'\n", "brew": "echo 'mpv 0.39.0'\n", // pacman fails for "celluloid" itself so the alternative-name loop runs. "pacman": "case \"$2\" in\n celluloid) echo \"error: package 'celluloid' was not found\" >&2; exit 1;;\nesac\ncat <<'MPV_FAKE_EOF'\nName : mpv\nVersion : 1:0.41.0-2\nMPV_FAKE_EOF\n", } } func TestFlatpakDetectionWithFakeTool(t *testing.T) { installFakeTools(t, fakeToolScripts()) assert.Equal(t, "0.39.0", getFlatpakAppVersion("io.mpv.Mpv")) apps := detectFlatpakApps() require.Len(t, apps, 2, "the fake flatpak reports every app as installed") assert.Equal(t, "MPV", apps[0].AppName) assert.Equal(t, "flatpak", apps[0].InstallType) assert.Equal(t, "0.39.0", apps[0].Version) } func TestPackageVersionProbesWithFakeTools(t *testing.T) { installFakeTools(t, fakeToolScripts()) version, _ := getPackageAppVersion(constants.PMFamilyDebian, "mpv") assert.Equal(t, "0.39.0", version) version, _ = getPackageAppVersion(constants.PMFamilyRHEL, "mpv") assert.Equal(t, "7.0.1", version) version, _ = getPackageAppVersion(constants.PMFamilySUSE, "mpv") assert.Equal(t, "1.2-3", version) // Discovery retains the package epoch, matching canonical version checks. version, _ = getPackageAppVersion(constants.PMFamilyArch, "mpv") assert.Equal(t, "1:0.41.0-2", version) // celluloid itself is "not installed"; the alternative names are tried. version, _ = getPackageAppVersion(constants.PMFamilyArch, "celluloid") assert.Equal(t, "1:0.41.0-2", version) } func TestDetectPackageAppsWithFakeTools(t *testing.T) { installFakeTools(t, fakeToolScripts()) apps := detectPackageApps() require.NotEmpty(t, apps) byMethod := map[string]DetectedApp{} for _, app := range apps { byMethod[app.MethodID] = app assert.Equal(t, "package", app.InstallType) assert.NotEmpty(t, app.Version) } assert.Contains(t, byMethod, "mpv-package") }