From b2dfa0c56ffa3a439247e9c5d1014f68a22bae26 Mon Sep 17 00:00:00 2001 From: Jo Date: Thu, 22 Jan 2026 10:28:47 +0100 Subject: [PATCH] chore(migration): default to "" as sortBy to keep existing behavior (#2768) * bump builtin version * add sortby migration to keep current user behavior --- Makefile | 2 +- main.go | 2 +- pkg/settings/config.go | 2 +- pkg/settings/migrations.go | 21 ++++++ pkg/settings/migrations_test.go | 112 ++++++++++++++++++++++++++++++++ 5 files changed, 136 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 8d9243ce..b0df4911 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ GO ?= go PKGNAME := yay PREFIX := /usr/local -MAJORVERSION := 12 +MAJORVERSION := 13 MINORVERSION := 0 PATCHVERSION := 0 VERSION ?= ${MAJORVERSION}.${MINORVERSION}.${PATCHVERSION} diff --git a/main.go b/main.go index 2ff441ad..39b63d22 100644 --- a/main.go +++ b/main.go @@ -18,7 +18,7 @@ import ( ) var ( - yayVersion = "12.5.3" // To be set by compiler. + yayVersion = "13.0.0" // To be set by compiler. localePath = "/usr/share/locale" // To be set by compiler. ) diff --git a/pkg/settings/config.go b/pkg/settings/config.go index 20b57ee9..fbbba7fb 100644 --- a/pkg/settings/config.go +++ b/pkg/settings/config.go @@ -209,7 +209,7 @@ func DefaultConfig(version string) *Configuration { BottomUp: true, CompletionInterval: 7, MaxConcurrentDownloads: 1, - SortBy: "votes", + SortBy: "", SearchBy: "name-desc", SudoLoop: false, GitBin: "git", diff --git a/pkg/settings/migrations.go b/pkg/settings/migrations.go index 9983fe0d..bd6e8a99 100644 --- a/pkg/settings/migrations.go +++ b/pkg/settings/migrations.go @@ -39,9 +39,30 @@ func (migration *configProviderMigration) TargetVersion() string { return "11.2.1" } +type configSortByMigration struct{} + +func (migration *configSortByMigration) String() string { + return gotext.Get("Reset 'sortby' setting to default") +} + +func (migration *configSortByMigration) Do(config *Configuration) bool { + if config.SortBy != "" { + config.SortBy = "" + + return true + } + + return false +} + +func (migration *configSortByMigration) TargetVersion() string { + return "13.0.0" +} + func DefaultMigrations() []configMigration { return []configMigration{ &configProviderMigration{}, + &configSortByMigration{}, } } diff --git a/pkg/settings/migrations_test.go b/pkg/settings/migrations_test.go index a051217b..4662baa6 100644 --- a/pkg/settings/migrations_test.go +++ b/pkg/settings/migrations_test.go @@ -161,3 +161,115 @@ func TestProvidesMigration(t *testing.T) { }) } } + +func TestSortByMigrationDo(t *testing.T) { + migration := &configSortByMigration{} + config := Configuration{ + SortBy: "name", + } + + assert.True(t, migration.Do(&config)) + + falseConfig := Configuration{SortBy: ""} + + assert.False(t, migration.Do(&falseConfig)) +} + +func TestSortByMigration(t *testing.T) { + t.Parallel() + type testCase struct { + desc string + testConfig *Configuration + newVersion string + wantSave bool + } + + testCases := []testCase{ + { + desc: "to upgrade", + testConfig: &Configuration{ + Version: "12.9.0", + SortBy: "name", + }, + newVersion: "13.0.0", + wantSave: true, + }, + { + desc: "to upgrade-git", + testConfig: &Configuration{ + Version: "12.9.0.r7.g6f60892", + SortBy: "votes", + }, + newVersion: "13.0.0", + wantSave: true, + }, + { + desc: "to not upgrade", + testConfig: &Configuration{ + Version: "12.9.0", + SortBy: "", + }, + newVersion: "13.0.0", + wantSave: false, + }, + { + desc: "to not upgrade - target version", + testConfig: &Configuration{ + Version: "13.0.0", + SortBy: "name", + }, + newVersion: "13.0.0", + wantSave: false, + }, + { + desc: "to not upgrade - new version", + testConfig: &Configuration{ + Version: "13.1.0", + SortBy: "name", + }, + newVersion: "13.1.0", + wantSave: false, + }, + } + + for _, tc := range testCases { + t.Run(tc.desc, func(t *testing.T) { + // Create temporary file for config + configFile, err := os.CreateTemp("/tmp", "yay-*-config.json") + require.NoError(t, err) + + testFilePath := configFile.Name() + defer os.Remove(testFilePath) + // Create config with configVersion and sortby + tcConfig := Configuration{ + Version: tc.testConfig.Version, + SortBy: tc.testConfig.SortBy, + // Create runtime with runtimeVersion + } + + // Run Migration + err = tcConfig.RunMigrations(newTestLogger(), + []configMigration{&configSortByMigration{}}, + testFilePath, tc.newVersion) + + require.NoError(t, err) + + // Check file contents if wantSave otherwise check file empty + cfile, err := os.Open(testFilePath) + require.NoError(t, err) + defer cfile.Close() + + decoder := json.NewDecoder(cfile) + newConfig := Configuration{} + err = decoder.Decode(&newConfig) + if tc.wantSave { + require.NoError(t, err) + assert.Equal(t, tc.newVersion, newConfig.Version) + assert.Equal(t, "", newConfig.SortBy) + } else { + require.Error(t, err) + assert.Empty(t, newConfig.Version) + } + }) + } +}