Fix -Qu exit code for empty update lists (#2162)

Fix -Qu exit code for empty update lists (#2061)

Previously, -Qun and -Qum without available updates could exit
with code 0 in some cases.
Also fix present and add more tests for such cases.
This commit is contained in:
smolx 2023-05-22 20:35:27 +02:00 committed by GitHub
parent ec15a5b363
commit a0a5e45fe7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 162 additions and 77 deletions

View File

@ -134,6 +134,7 @@ func printUpdateList(ctx context.Context, cfg *settings.Configuration, cmdArgs *
foreignFilter := cmdArgs.ExistsArg("m", "foreign") foreignFilter := cmdArgs.ExistsArg("m", "foreign")
nativeFilter := cmdArgs.ExistsArg("n", "native") nativeFilter := cmdArgs.ExistsArg("n", "native")
noUpdates := true
_ = graph.ForEach(func(pkgName string, ii *dep.InstallInfo) error { _ = graph.ForEach(func(pkgName string, ii *dep.InstallInfo) error {
if !ii.Upgrade { if !ii.Upgrade {
return nil return nil
@ -154,6 +155,7 @@ func printUpdateList(ctx context.Context, cfg *settings.Configuration, cmdArgs *
} }
targets.Remove(pkgName) targets.Remove(pkgName)
noUpdates = false
} }
return nil return nil
@ -168,7 +170,7 @@ func printUpdateList(ctx context.Context, cfg *settings.Configuration, cmdArgs *
return false return false
}) })
if missing { if missing || noUpdates {
return fmt.Errorf("") return fmt.Errorf("")
} }

View File

@ -33,76 +33,8 @@ func TestPrintUpdateList(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.NoError(t, f.Close()) require.NoError(t, f.Close())
testCases := []struct { mockDBName := mock.NewDB("core")
name string mockDB := &mock.DBExecutor{
args []string
targets []string
wantPkgs []string
wantErr bool
}{
{
name: "Qu",
args: []string{"Q", "u"},
targets: []string{},
wantPkgs: []string{
fmt.Sprintf("%s %s -> %s",
text.Bold("linux"),
text.Bold(text.Green("4.3.0")),
text.Bold(text.Green("5.10.0")),
),
fmt.Sprintf("%s %s -> %s",
text.Bold("go"),
text.Bold(text.Green("2:1.20.3-1")),
text.Bold(text.Green("2:1.20.4-1")),
),
fmt.Sprintf("%s %s -> %s",
text.Bold("vosk-api"),
text.Bold(text.Green("0.3.43-1")),
text.Bold(text.Green("0.3.45-1")),
),
},
},
{
name: "Quq",
args: []string{"Q", "u", "q"},
targets: []string{},
wantPkgs: []string{"linux", "go", "vosk-api"},
},
{
name: "Quq linux",
args: []string{"Q", "u", "q"},
targets: []string{"linux"},
wantPkgs: []string{"linux"},
},
{
name: "Qunq",
args: []string{"Q", "u", "n", "q"},
targets: []string{},
wantPkgs: []string{"linux", "go"},
},
{
name: "Qumq",
args: []string{"Q", "u", "m", "q"},
targets: []string{},
wantPkgs: []string{"vosk-api"},
},
{
name: "Quq no-update-pkg",
args: []string{"Q", "u", "q"},
targets: []string{"no-update-pkg"},
wantPkgs: []string{},
},
{
name: "Quq non-existent-pkg",
args: []string{"Q", "u", "q"},
targets: []string{"non-existent-pkg"},
wantPkgs: []string{},
wantErr: true,
},
}
dbName := mock.NewDB("core")
db := &mock.DBExecutor{
AlpmArchitecturesFn: func() ([]string, error) { AlpmArchitecturesFn: func() ([]string, error) {
return []string{"x86_64"}, nil return []string{"x86_64"}, nil
}, },
@ -133,7 +65,7 @@ func TestPrintUpdateList(t *testing.T) {
Package: &mock.Package{ Package: &mock.Package{
PName: "linux", PName: "linux",
PVersion: "5.10.0", PVersion: "5.10.0",
PDB: dbName, PDB: mockDBName,
}, },
LocalVersion: "4.3.0", LocalVersion: "4.3.0",
Reason: alpm.PkgReasonExplicit, Reason: alpm.PkgReasonExplicit,
@ -142,7 +74,7 @@ func TestPrintUpdateList(t *testing.T) {
Package: &mock.Package{ Package: &mock.Package{
PName: "go", PName: "go",
PVersion: "2:1.20.4-1", PVersion: "2:1.20.4-1",
PDB: dbName, PDB: mockDBName,
}, },
LocalVersion: "2:1.20.3-1", LocalVersion: "2:1.20.3-1",
Reason: alpm.PkgReasonExplicit, Reason: alpm.PkgReasonExplicit,
@ -154,7 +86,7 @@ func TestPrintUpdateList(t *testing.T) {
return &mock.Package{ return &mock.Package{
PName: "no-update-pkg", PName: "no-update-pkg",
PVersion: "3.3.3", PVersion: "3.3.3",
PDB: dbName, PDB: mockDBName,
} }
} }
return nil return nil
@ -162,6 +94,40 @@ func TestPrintUpdateList(t *testing.T) {
SetLoggerFn: func(logger *text.Logger) {}, SetLoggerFn: func(logger *text.Logger) {},
} }
mockDBNoUpdates := &mock.DBExecutor{
AlpmArchitecturesFn: func() ([]string, error) {
return []string{"x86_64"}, nil
},
RefreshHandleFn: func() error {
return nil
},
ReposFn: func() []string {
return []string{"core"}
},
InstalledRemotePackagesFn: func() map[string]alpm.IPackage {
return map[string]alpm.IPackage{
"vosk-api": &mock.Package{
PName: "vosk-api",
PVersion: "0.3.43-1",
PBase: "vosk-api",
PReason: alpm.PkgReasonExplicit,
},
}
},
InstalledRemotePackageNamesFn: func() []string {
return []string{"vosk-api"}
},
SyncUpgradesFn: func(
bool,
) (map[string]db.SyncUpgrade, error) {
return map[string]db.SyncUpgrade{}, nil
},
LocalPackageFn: func(s string) mock.IPackage {
return nil
},
SetLoggerFn: func(logger *text.Logger) {},
}
mockAUR := &mockaur.MockAUR{ mockAUR := &mockaur.MockAUR{
GetFn: func(ctx context.Context, query *aur.Query) ([]aur.Pkg, error) { GetFn: func(ctx context.Context, query *aur.Query) ([]aur.Pkg, error) {
return []aur.Pkg{ return []aur.Pkg{
@ -174,6 +140,124 @@ func TestPrintUpdateList(t *testing.T) {
}, },
} }
mockAURNoUpdates := &mockaur.MockAUR{
GetFn: func(ctx context.Context, query *aur.Query) ([]aur.Pkg, error) {
return []aur.Pkg{
{
Name: "vosk-api",
PackageBase: "vosk-api",
Version: "0.3.43-1",
},
}, nil
},
}
type mockData struct {
db *mock.DBExecutor
aurCache *mockaur.MockAUR
}
testCases := []struct {
name string
mockData mockData
args []string
targets []string
wantPkgs []string
wantErr bool
}{
{
name: "Qu",
mockData: mockData{mockDB, mockAUR},
args: []string{"Q", "u"},
targets: []string{},
wantPkgs: []string{
fmt.Sprintf("%s %s -> %s",
text.Bold("linux"),
text.Bold(text.Green("4.3.0")),
text.Bold(text.Green("5.10.0")),
),
fmt.Sprintf("%s %s -> %s",
text.Bold("go"),
text.Bold(text.Green("2:1.20.3-1")),
text.Bold(text.Green("2:1.20.4-1")),
),
fmt.Sprintf("%s %s -> %s",
text.Bold("vosk-api"),
text.Bold(text.Green("0.3.43-1")),
text.Bold(text.Green("0.3.45-1")),
),
},
},
{
name: "Quq",
mockData: mockData{mockDB, mockAUR},
args: []string{"Q", "u", "q"},
targets: []string{},
wantPkgs: []string{"linux", "go", "vosk-api"},
},
{
name: "Quq linux",
mockData: mockData{mockDB, mockAUR},
args: []string{"Q", "u", "q"},
targets: []string{"linux"},
wantPkgs: []string{"linux"},
},
{
name: "Qunq",
mockData: mockData{mockDB, mockAUR},
args: []string{"Q", "u", "n", "q"},
targets: []string{},
wantPkgs: []string{"linux", "go"},
},
{
name: "Qumq",
mockData: mockData{mockDB, mockAUR},
args: []string{"Q", "u", "m", "q"},
targets: []string{},
wantPkgs: []string{"vosk-api"},
},
{
name: "Quq no-update-pkg",
mockData: mockData{mockDB, mockAUR},
args: []string{"Q", "u", "q"},
targets: []string{"no-update-pkg"},
wantPkgs: []string{},
wantErr: true,
},
{
name: "Quq non-existent-pkg",
mockData: mockData{mockDB, mockAUR},
args: []string{"Q", "u", "q"},
targets: []string{"non-existent-pkg"},
wantPkgs: []string{},
wantErr: true,
},
{
name: "Qu no-updates-any",
mockData: mockData{mockDBNoUpdates, mockAURNoUpdates},
args: []string{"Q", "u"},
targets: []string{},
wantPkgs: []string{},
wantErr: true,
},
{
name: "Qun no-updates-native",
mockData: mockData{mockDBNoUpdates, mockAUR},
args: []string{"Q", "u", "n"},
targets: []string{},
wantPkgs: []string{},
wantErr: true,
},
{
name: "Qum no-updates-foreign",
mockData: mockData{mockDB, mockAURNoUpdates},
args: []string{"Q", "u", "m"},
targets: []string{},
wantPkgs: []string{},
wantErr: true,
},
}
for _, tc := range testCases { for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
cmdBuilder := &exe.CmdBuilder{ cmdBuilder := &exe.CmdBuilder{
@ -191,7 +275,7 @@ func TestPrintUpdateList(t *testing.T) {
Logger: NewTestLogger(), Logger: NewTestLogger(),
CmdBuilder: cmdBuilder, CmdBuilder: cmdBuilder,
VCSStore: &vcs.Mock{}, VCSStore: &vcs.Mock{},
AURCache: mockAUR, AURCache: tc.mockData.aurCache,
}, },
} }
@ -203,7 +287,7 @@ func TestPrintUpdateList(t *testing.T) {
r, w, _ := os.Pipe() r, w, _ := os.Pipe()
os.Stdout = w os.Stdout = w
err = handleCmd(context.Background(), cfg, cmdArgs, db) err = handleCmd(context.Background(), cfg, cmdArgs, tc.mockData.db)
w.Close() w.Close()
out, _ := io.ReadAll(r) out, _ := io.ReadAll(r)
@ -212,7 +296,6 @@ func TestPrintUpdateList(t *testing.T) {
if tc.wantErr { if tc.wantErr {
require.Error(t, err) require.Error(t, err)
assert.EqualError(t, err, "") assert.EqualError(t, err, "")
return
} else { } else {
require.NoError(t, err) require.NoError(t, err)
} }