Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
tttol committed Jul 1, 2024
1 parent ddd7e38 commit b8905fc
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 5 deletions.
20 changes: 17 additions & 3 deletions core/util/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
121 changes: 121 additions & 0 deletions core/util/object_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
6 changes: 4 additions & 2 deletions web/s3_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand All @@ -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)
Expand Down

0 comments on commit b8905fc

Please sign in to comment.