Skip to content

Commit

Permalink
retain explicit sort order when using indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
ayoisaiah committed Oct 31, 2024
1 parent 24e0567 commit 969ced3
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 23 deletions.
7 changes: 3 additions & 4 deletions internal/sortfiles/sortfiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ func ForRenamingAndUndo(changes file.Changes, revert bool) {
})
}

// EnforceHierarchicalOrder ensures all files in the same directory are sorted
// Hierarchically ensures all files in the same directory are sorted
// before children directories.
func EnforceHierarchicalOrder(changes file.Changes) {
func Hierarchically(changes file.Changes) {
slices.SortStableFunc(changes, func(a, b *file.Change) int {
lenA, lenB := len(a.BaseDir), len(b.BaseDir)
if lenA == lenB {
Expand Down Expand Up @@ -242,9 +242,8 @@ func Changes(
changes file.Changes,
conf *config.Config,
) {
// TODO: EnforceHierarchicalOrder should be the default sort?
if conf.SortPerDir {
EnforceHierarchicalOrder(changes)
Hierarchically(changes)
}

//nolint:exhaustive // default sort not needed
Expand Down
2 changes: 1 addition & 1 deletion internal/sortfiles/sortfiles_test/sortfiles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func TestSortFiles_EnforceHierarchicalOrder(t *testing.T) {
t.Run(tc.Name, func(t *testing.T) {
unsorted := sortTest(t, tc.Unsorted)

sortfiles.EnforceHierarchicalOrder(unsorted)
sortfiles.Hierarchically(unsorted)

testutil.CompareSourcePath(t, tc.Sorted, unsorted)
})
Expand Down
6 changes: 4 additions & 2 deletions replace/replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ func replaceMatches(
return nil, err
}

if vars.IndexMatches() > 0 {
sortfiles.EnforceHierarchicalOrder(matches)
// If using indexes without an explicit sort, ensure that the files
// are arranged hierarchically
if vars.IndexMatches() > 0 && conf.Sort == config.SortDefault {
sortfiles.Hierarchically(matches)
}

var pairs int
Expand Down
86 changes: 70 additions & 16 deletions replace/replace_test/replace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,6 @@ func TestReplace(t *testing.T) {
},
Args: []string{"-f", "budget", "-r", "forecast", "-l", "-2"},
},
{
Name: "replace the first 2 matches in reverse",
Changes: file.Changes{
{
Source: "budget_budget_budget_2023.xlsx",
},
{
Source: "budget_2024.xlsx",
},
},
Want: []string{
"budget_forecast_forecast_2023.xlsx",
"forecast_2024.xlsx",
},
Args: []string{"-f", "budget", "-r", "forecast", "-l", "-2"},
},
{
Name: "rename with capture variables",
Changes: file.Changes{
Expand Down Expand Up @@ -271,6 +255,76 @@ func TestReplace(t *testing.T) {
"pictures",
},
},
{
Name: "retain file order with an explicit sort",
Changes: file.Changes{
{
BaseDir: "testdata/dir1",
Source: "doc.txt",
},
{
BaseDir: "testdata/dir1",
Source: "file.md",
},
{
BaseDir: "testdata",
Source: "audio.mp3",
},
{
BaseDir: "testdata",
Source: "binary.mp3",
},
},
Want: []string{
"testdata/dir1/001.txt",
"testdata/dir1/002.md",
"testdata/003.mp3",
"testdata/004.mp3",
},
Args: []string{
"-f",
`.*\.(txt|md|mp3)`,
"-r",
"{%03d}{ext}",
"-R",
"--sort",
"size",
},
},
{
Name: "without an explicit sort, arrange hierarchically",
Changes: file.Changes{
{
BaseDir: "testdata/dir1",
Source: "doc.txt",
},
{
BaseDir: "testdata/dir1",
Source: "file.md",
},
{
BaseDir: "testdata",
Source: "audio.mp3",
},
{
BaseDir: "testdata",
Source: "binary.mp3",
},
},
Want: []string{
"testdata/001.mp3",
"testdata/002.mp3",
"testdata/dir1/003.txt",
"testdata/dir1/004.md",
},
Args: []string{
"-f",
`.*\.(txt|md|mp3)`,
"-r",
"{%03d}{ext}",
"-R",
},
},
}

replaceTest(t, testCases)
Expand Down

0 comments on commit 969ced3

Please sign in to comment.