From 89216f3afd970581218da690c4769ec2769a768c Mon Sep 17 00:00:00 2001 From: Joey Holtzman <89617856+jdholtz@users.noreply.github.com> Date: Fri, 8 Aug 2025 00:59:05 -0700 Subject: [PATCH] Fix cleaning AUR always cleaning all packages (#2650) --- clean.go | 2 +- clean_test.go | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+), 1 deletion(-) diff --git a/clean.go b/clean.go index 148554aa..21ecb631 100644 --- a/clean.go +++ b/clean.go @@ -52,7 +52,7 @@ func syncClean(ctx context.Context, run *runtime.Runtime, cmdArgs *parser.Argume keepInstalled := false keepCurrent := false - removeAll := cmdArgs.ExistsArg("c", "clean") + _, removeAll, _ := cmdArgs.GetArg("c", "clean") for _, v := range run.PacmanConf.CleanMethod { switch v { diff --git a/clean_test.go b/clean_test.go index bf106c30..bb411821 100644 --- a/clean_test.go +++ b/clean_test.go @@ -6,11 +6,14 @@ package main import ( "context" "fmt" + "os" "os/exec" + "path/filepath" "strings" "testing" "github.com/Jguer/go-alpm/v2" + pacmanconf "github.com/Morganamilo/go-pacmanconf" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -114,3 +117,106 @@ func TestCleanHanging(t *testing.T) { }) } } + +func TestIntegrationCleanAUR(t *testing.T) { + buildDir := filepath.Join(t.TempDir(), "build") + yayGitDir := filepath.Join(buildDir, "yay-git") + zoomDir := filepath.Join(buildDir, "zoom") + + t.Parallel() + + testCases := []struct { + name string + args []string + wantDirs []string + }{ + { + name: "Sync clean AUR", + args: []string{"S", "c", "a"}, + wantDirs: []string{"zoom"}, + }, + { + name: "Sync clean double AUR", + args: []string{"S", "c", "c", "a"}, + wantDirs: []string{}, + }, + } + + dbExc := &mock.DBExecutor{ + PackageOptionalDependsFn: func(i alpm.IPackage) []alpm.Depend { + if i.Name() == "linux" { + return []alpm.Depend{ + { + Name: "linux-headers", + }, + } + } + + return []alpm.Depend{} + }, + PackageProvidesFn: func(p alpm.IPackage) []alpm.Depend { return []alpm.Depend{} }, + PackageDependsFn: func(p alpm.IPackage) []alpm.Depend { return []alpm.Depend{} }, + InstalledRemotePackagesFn: func() map[string]alpm.IPackage { + return map[string]alpm.IPackage{ + "zoom": &mock.Package{ + PName: "zoom", + PVersion: "6.5.8-1", + PBase: "zoom", + PReason: alpm.PkgReasonExplicit, + }, + } + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + mockRunner := &exe.MockRunner{ + CaptureFn: func(cmd *exec.Cmd) (stdout string, stderr string, err error) { + return "", "", nil + }, + ShowFn: func(cmd *exec.Cmd) error { return nil }, + } + + cfg := &settings.Configuration{ + BuildDir: buildDir, + Mode: parser.ModeAUR, + } + pacmanConf := &pacmanconf.Config{ + // Only testing the keep installed clean method right now + CleanMethod: []string{"KeepInstalled"}, + } + run := &runtime.Runtime{ + Cfg: cfg, + PacmanConf: pacmanConf, + Logger: newTestLogger(), + } + cmdArgs := parser.MakeArguments() + cmdArgs.AddArg(tc.args...) + + // Create the package directories to be cleaned + err := os.MkdirAll(yayGitDir, 0755) + require.NoError(t, err) + err = os.MkdirAll(zoomDir, 0755) + require.NoError(t, err) + + err = handleCmd(context.Background(), + run, cmdArgs, dbExc, + ) + require.NoError(t, err) + + // This should only test AUR cleaning, so no calls to an external command should be made + assert.Len(t, mockRunner.ShowCalls, 0) + + // Make sure the directories left after cleaning are the only ones we expect + packageDirs, err := os.ReadDir(buildDir) + require.NoError(t, err) + + var packageDirNames []string + for _, dir := range packageDirs { + packageDirNames = append(packageDirNames, dir.Name()) + } + + assert.ElementsMatch(t, tc.wantDirs, packageDirNames) + }) + } +}