Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions agent/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"path/filepath"
"regexp"
"runtime"
"slices"
"strings"

"github.com/shirou/gopsutil/v4/disk"
Expand Down Expand Up @@ -103,6 +104,17 @@ func listFiles(query LSRequest) (LSResponse, error) {
})
}

// Sort alphabetically: directories then files
slices.SortFunc(respContents, func(a, b LSFile) int {
if a.IsDir && !b.IsDir {
return -1
}
if !a.IsDir && b.IsDir {
return 1
}
return strings.Compare(a.Name, b.Name)
})

absolutePath := pathToArray(absolutePathString)

return LSResponse{
Expand Down
11 changes: 6 additions & 5 deletions agent/ls_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,16 @@ func TestListFilesSuccess(t *testing.T) {
require.NoError(t, err)

require.Equal(t, tmpDir, resp.AbsolutePathString)
require.ElementsMatch(t, []LSFile{
// Output is sorted
require.Equal(t, []LSFile{
{
Name: "repos",
AbsolutePathString: reposDir,
Name: "Downloads",
AbsolutePathString: downloadsDir,
IsDir: true,
},
{
Name: "Downloads",
AbsolutePathString: downloadsDir,
Name: "repos",
AbsolutePathString: reposDir,
IsDir: true,
},
{
Expand Down
Loading