Skip to content

Commit

Permalink
增加测试
Browse files Browse the repository at this point in the history
  • Loading branch information
movsb committed Jun 21, 2024
1 parent 98f6c05 commit 5a2d2ac
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 10 deletions.
11 changes: 10 additions & 1 deletion service/comment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
)

func TestPreviewComment(t *testing.T) {
t.SkipNow()
initService()
rsp, err := blog.PreviewComment(guest, &proto.PreviewCommentRequest{
Markdown: `<a>`,
Expand All @@ -17,4 +16,14 @@ func TestPreviewComment(t *testing.T) {
if err == nil || !strings.Contains(err.Error(), "不能包含") {
t.Fatal(rsp, err)
}
rsp2, err := blog.CreateComment(guest, &proto.Comment{
PostId: 1,
Author: `昵称`,
Email: `fake@twofei.com`,
SourceType: `markdown`,
Source: `<marquee style="max-width: 100px;">(🏃逃……</marquee>`,
})
if err == nil || !strings.Contains(err.Error(), `不能包含`) {
t.Fatal(rsp2, err)
}
}
3 changes: 0 additions & 3 deletions service/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ func (s *Service) FileSystem(srv proto.Management_FileSystemServer) error {
} else if initReq != nil {
initialized = true
if init := initReq.GetPost(); init != nil {
if s.postDataFS == nil {
return errors.New(`对此编号的文件系统不可用。`)
}
pfs, err = s.postDataFS.ForPost(int(init.Id))
}
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func newService(ctx context.Context, cancel context.CancelFunc, cfg *config.Conf
testing: testing,

cfg: cfg,
postDataFS: nil,
postDataFS: &theme_fs.Empty{},

db: db,
tdb: taorm.NewDB(db),
Expand Down
6 changes: 1 addition & 5 deletions service/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"html"
"log"
"net/url"
"os"
"path"
"slices"
"strings"
Expand Down Expand Up @@ -166,15 +165,12 @@ func (s *Service) setPostLink(p *proto.Post, k proto.LinkKind) {
}

func (s *Service) OpenAsset(id int64) gold_utils.WebFileSystem {
if s.testing {
panic(`测试服务器不用于本地文件系统。`)
}
u := utils.Must1(url.Parse(s.cfg.Site.Home))
if u.Path == "" {
u.Path = "/"
}
return gold_utils.NewWebFileSystem(
os.DirFS(s.cfg.Data.File.Path),
utils.Must1(s.postDataFS.ForPost(int(id))),
u.JoinPath(fmt.Sprintf("/%d/", id)),
)
}
Expand Down
4 changes: 4 additions & 0 deletions service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package service_test
import (
"context"
"log"
"os"

"github.com/movsb/taoblog/cmd/config"
"github.com/movsb/taoblog/cmd/server"
Expand All @@ -18,6 +19,9 @@ var admin context.Context
var guest context.Context

func initService() {
// 测试环境应该不依赖本地系统。
os.Setenv(`DEV`, `0`)

cfg := config.DefaultConfig()
cfg.Auth.Basic.Username = `test`
cfg.Auth.Basic.Password = `test`
Expand Down
13 changes: 13 additions & 0 deletions theme/modules/fs/fs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package theme_fs

import (
"embed"
"io/fs"
)

Expand All @@ -12,3 +13,15 @@ type FS interface {
// 针对单篇文章/评论的文件系统。
ForPost(id int) (fs.FS, error)
}

type Empty struct{}

var empty embed.FS

func (Empty) Root() (fs.FS, error) {
return empty, nil
}

func (Empty) ForPost(id int) (fs.FS, error) {
return empty, nil
}

0 comments on commit 5a2d2ac

Please sign in to comment.