From b8905fcfea5b2c613fac8d50dfd21bd6e4d05439 Mon Sep 17 00:00:00 2001 From: t-takahashi Date: Tue, 2 Jul 2024 07:46:58 +0900 Subject: [PATCH] add test --- core/util/object.go | 20 ++++++- core/util/object_test.go | 121 +++++++++++++++++++++++++++++++++++++++ web/s3_controller.go | 6 +- 3 files changed, 142 insertions(+), 5 deletions(-) create mode 100644 core/util/object_test.go diff --git a/core/util/object.go b/core/util/object.go index c62f26b..1e3d770 100644 --- a/core/util/object.go +++ b/core/util/object.go @@ -5,12 +5,13 @@ import ( "net/http" "os" "path/filepath" + "sort" "github.com/tttol/mos3/core/model" ) -func GenerateS3Objects(r *http.Request, path string) ([]model.S3Object, error) { - dirEntry, err := os.ReadDir(filepath.Join("./upload", path)) +func GenerateS3Objects(r *http.Request, dir string, path string) ([]model.S3Object, error) { + dirEntry, err := os.ReadDir(filepath.Join(dir, path)) if err != nil { slog.Error("ReadDir error", "error", err) return nil, err @@ -25,6 +26,19 @@ func GenerateS3Objects(r *http.Request, path string) ([]model.S3Object, error) { s3Objects = append(s3Objects, obj) } + sorted := sortObjects(s3Objects) - return s3Objects, nil + return sorted, nil +} + +func sortObjects(s3Objects []model.S3Object) []model.S3Object { + sort.Slice(s3Objects, func(i, j int) bool { + // sort by IsDir asc + if s3Objects[i].IsDir != s3Objects[j].IsDir { + return s3Objects[i].IsDir + } + // sort by Name asc + return s3Objects[i].Name < s3Objects[j].Name + }) + return s3Objects } diff --git a/core/util/object_test.go b/core/util/object_test.go new file mode 100644 index 0000000..493f641 --- /dev/null +++ b/core/util/object_test.go @@ -0,0 +1,121 @@ +package util + +import ( + "net/http/httptest" + "os" + "reflect" + "testing" + + "github.com/tttol/mos3/core/model" +) + +func setupTestDir(t *testing.T) { + err := os.MkdirAll("./testdir/hoge/", os.ModePerm) + if err != nil { + t.Fatalf("setupTestDir error: %v", err) + } + _, err = os.Create("./testdir/hoge/file1.txt") + if err != nil { + t.Fatalf("setupTestDir error: %v", err) + } + _, err = os.Create("./testdir/hoge/file2.txt") + if err != nil { + t.Fatalf("setupTestDir error: %v", err) + } + err = os.Mkdir("./testdir/hoge/dir1", os.ModePerm) + if err != nil { + t.Fatalf("setupTestDir error: %v", err) + } +} + +func removeTestDir() { + os.RemoveAll("./testdir") +} + +func TestGenerateS3Objects(t *testing.T) { + setupTestDir(t) + defer removeTestDir() + + req := httptest.NewRequest("GET", "/hoge", nil) + + expected := []model.S3Object{ + { + Name: "dir1", + FullPath: "/hoge/dir1", + IsDir: true, + }, + { + Name: "file1.txt", + FullPath: "/hoge/file1.txt", + IsDir: false, + }, + { + Name: "file2.txt", + FullPath: "/hoge/file2.txt", + IsDir: false, + }, + } + + actual, err := GenerateS3Objects(req, "./testdir", "hoge") + if err != nil { + t.Fatalf("GenerateS3Objects error: %v", err) + } + + if !reflect.DeepEqual(expected, actual) { + t.Errorf("expected %v, got %v", expected, actual) + } +} + +func TestSortObjects(t *testing.T) { + input := []model.S3Object{ + { + Name: "ccc.txt", + IsDir: false, + }, + { + Name: "bbb.txt", + IsDir: false, + }, + { + Name: "aaa.txt", + IsDir: false, + }, + { + Name: "dir2", + IsDir: true, + }, + { + Name: "dir1", + IsDir: true, + }, + } + + expected := []model.S3Object{ + { + Name: "dir1", + IsDir: true, + }, + { + Name: "dir2", + IsDir: true, + }, + { + Name: "aaa.txt", + IsDir: false, + }, + { + Name: "bbb.txt", + IsDir: false, + }, + { + Name: "ccc.txt", + IsDir: false, + }, + } + + actual := sortObjects(input) + + if !reflect.DeepEqual(expected, actual) { + t.Errorf("expected %v, got %v", expected, actual) + } +} diff --git a/web/s3_controller.go b/web/s3_controller.go index 442efd0..8ba7546 100644 --- a/web/s3_controller.go +++ b/web/s3_controller.go @@ -8,7 +8,9 @@ import ( "github.com/tttol/mos3/core/util" ) -const uploadDir = "./upload" +const ( + UPLOAD_DIR = "./upload" +) func S3Handler(w http.ResponseWriter, r *http.Request) { slog.Info("S3Handler is called.") @@ -18,7 +20,7 @@ func S3Handler(w http.ResponseWriter, r *http.Request) { return } - s3Objects, err := util.GenerateS3Objects(r, path) + s3Objects, err := util.GenerateS3Objects(r, UPLOAD_DIR, path) if err != nil { slog.Error("GetS3Objects error", "error", err) http.Error(w, err.Error(), http.StatusInternalServerError)