diff --git a/agent/ls.go b/agent/ls.go index 9e65e26fdd4b0..5c90e5e602540 100644 --- a/agent/ls.go +++ b/agent/ls.go @@ -7,6 +7,7 @@ import ( "path/filepath" "regexp" "runtime" + "slices" "strings" "github.com/shirou/gopsutil/v4/disk" @@ -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{ diff --git a/agent/ls_internal_test.go b/agent/ls_internal_test.go index acc4ea2929444..0c4e42f2d0cc9 100644 --- a/agent/ls_internal_test.go +++ b/agent/ls_internal_test.go @@ -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, }, {