Skip to content

Commit 2e2bef9

Browse files
committed
Add config migration of paging section to pagers array
1 parent e459919 commit 2e2bef9

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed

pkg/config/app_config.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,11 @@ func computeMigratedConfig(path string, content []byte, changes *ChangesSet) ([]
310310
return nil, false, fmt.Errorf("Couldn't migrate config file at `%s`: %w", path, err)
311311
}
312312

313+
err = migratePagers(&rootNode, changes)
314+
if err != nil {
315+
return nil, false, fmt.Errorf("Couldn't migrate config file at `%s`: %w", path, err)
316+
}
317+
313318
// Add more migrations here...
314319

315320
if reflect.DeepEqual(rootNode, originalCopy) {
@@ -469,6 +474,37 @@ func migrateAllBranchesLogCmd(rootNode *yaml.Node, changes *ChangesSet) error {
469474
})
470475
}
471476

477+
func migratePagers(rootNode *yaml.Node, changes *ChangesSet) error {
478+
return yaml_utils.TransformNode(rootNode, []string{"git"}, func(gitNode *yaml.Node) error {
479+
pagingKeyNode, pagingValueNode := yaml_utils.LookupKey(gitNode, "paging")
480+
if pagingKeyNode == nil || pagingValueNode.Kind != yaml.MappingNode {
481+
// If there's no "paging" section (or it's not an object), there's nothing to do
482+
return nil
483+
}
484+
485+
pagersKeyNode, _ := yaml_utils.LookupKey(gitNode, "pagers")
486+
if pagersKeyNode != nil {
487+
// Conversely, if there *is* already a "pagers" array, we also have nothing to do.
488+
// This covers the case where the user keeps both the "paging" section and the "pagers"
489+
// array for the sake of easier testing of old versions.
490+
return nil
491+
}
492+
493+
pagingKeyNode.Value = "pagers"
494+
pagingContentCopy := pagingValueNode.Content
495+
pagingValueNode.Kind = yaml.SequenceNode
496+
pagingValueNode.Tag = "!!seq"
497+
pagingValueNode.Content = []*yaml.Node{{
498+
Kind: yaml.MappingNode,
499+
Content: pagingContentCopy,
500+
}}
501+
502+
changes.Add("Moved git.paging object to git.pagers array")
503+
504+
return nil
505+
})
506+
}
507+
472508
func (c *AppConfig) GetDebug() bool {
473509
return c.debug
474510
}

pkg/config/app_config_test.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,3 +1089,98 @@ func TestAllBranchesLogCmdMigrations(t *testing.T) {
10891089
})
10901090
}
10911091
}
1092+
1093+
func TestPagerMigration(t *testing.T) {
1094+
scenarios := []struct {
1095+
name string
1096+
input string
1097+
expected string
1098+
expectedDidChange bool
1099+
expectedChanges []string
1100+
}{
1101+
{
1102+
name: "Incomplete Configuration Passes uneventfully",
1103+
input: "git:",
1104+
expectedDidChange: false,
1105+
expectedChanges: []string{},
1106+
},
1107+
{
1108+
name: "No paging section",
1109+
input: `git:
1110+
autoFetch: true
1111+
`,
1112+
expected: `git:
1113+
autoFetch: true
1114+
`,
1115+
expectedDidChange: false,
1116+
expectedChanges: []string{},
1117+
},
1118+
{
1119+
name: "Both paging and pagers exist",
1120+
input: `git:
1121+
paging:
1122+
pager: delta --dark --paging=never
1123+
pagers:
1124+
- diff: diff-so-fancy
1125+
`,
1126+
expected: `git:
1127+
paging:
1128+
pager: delta --dark --paging=never
1129+
pagers:
1130+
- diff: diff-so-fancy
1131+
`,
1132+
expectedDidChange: false,
1133+
expectedChanges: []string{},
1134+
},
1135+
{
1136+
name: "paging is not an object",
1137+
input: `git:
1138+
paging: 5
1139+
`,
1140+
expected: `git:
1141+
paging: 5
1142+
`,
1143+
expectedDidChange: false,
1144+
expectedChanges: []string{},
1145+
},
1146+
{
1147+
name: "paging is moved to pagers array (keeping the order)",
1148+
input: `git:
1149+
paging:
1150+
pager: delta --dark --paging=never
1151+
autoFetch: true
1152+
`,
1153+
expected: `git:
1154+
pagers:
1155+
- pager: delta --dark --paging=never
1156+
autoFetch: true
1157+
`,
1158+
expectedDidChange: true,
1159+
expectedChanges: []string{"Moved git.paging object to git.pagers array"},
1160+
},
1161+
{
1162+
name: "paging is moved to pagers array even if empty",
1163+
input: `git:
1164+
paging: {}
1165+
`,
1166+
expected: `git:
1167+
pagers: [{}]
1168+
`,
1169+
expectedDidChange: true,
1170+
expectedChanges: []string{"Moved git.paging object to git.pagers array"},
1171+
},
1172+
}
1173+
1174+
for _, s := range scenarios {
1175+
t.Run(s.name, func(t *testing.T) {
1176+
changes := NewChangesSet()
1177+
actual, didChange, err := computeMigratedConfig("path doesn't matter", []byte(s.input), changes)
1178+
assert.NoError(t, err)
1179+
assert.Equal(t, s.expectedDidChange, didChange)
1180+
if didChange {
1181+
assert.Equal(t, s.expected, string(actual))
1182+
}
1183+
assert.Equal(t, s.expectedChanges, changes.ToSliceFromOldest())
1184+
})
1185+
}
1186+
}

0 commit comments

Comments
 (0)