Skip to content

Commit b18bacc

Browse files
fix: test filepaths
1 parent 0fc111c commit b18bacc

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

services/articles.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ const (
1212
)
1313

1414
func MarkdownArticlesPath() string {
15+
root := os.Getenv("ROOT_DIR")
1516
if strings.Compare(os.Getenv("ENV"), "dev") == 0 {
16-
return path.Join(Root, MarkdownArticlesPathBase, "dev")
17+
return path.Join(root, MarkdownArticlesPathBase, "dev")
1718
}
18-
return path.Join(Root, MarkdownArticlesPathBase, "prod")
19+
return path.Join(root, MarkdownArticlesPathBase, "prod")
1920
}
2021

2122
func RetrieveLocalMdArticles() ([]string, error) {
@@ -35,7 +36,7 @@ func RetrieveLocalMdArticles() ([]string, error) {
3536

3637
func RetriveLocalHtmlArticle(article string) (string, error) {
3738
article += ".html"
38-
file, err := os.ReadFile(path.Join(Root, HTMLArticlesPath, article))
39+
file, err := os.ReadFile(path.Join(os.Getenv("ROOT_DIR"), HTMLArticlesPath, article))
3940
if err != nil {
4041
return "", err
4142
}
@@ -46,7 +47,7 @@ func RetriveLocalHtmlArticle(article string) (string, error) {
4647
func PersistHtmlArticle(article string, content string) error {
4748
article = strings.Replace(article, ".md", ".html", 1)
4849

49-
file, err := os.Create(path.Join(Root, HTMLArticlesPath, article))
50+
file, err := os.Create(path.Join(os.Getenv("ROOT_DIR"), HTMLArticlesPath, article))
5051
if err != nil {
5152
return err
5253
}

services/markdown.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"fmt"
88
"io"
99
"os"
10-
"path/filepath"
1110
"regexp"
1211
"strings"
1312

@@ -17,10 +16,6 @@ import (
1716
"potblog/infrastructure"
1817
)
1918

20-
var (
21-
Root = os.Getenv("ROOT_DIR")
22-
)
23-
2419
func renderTemplate(t templ.Component) string {
2520
buf := templ.GetBuffer()
2621
defer templ.ReleaseBuffer(buf)
@@ -32,8 +27,7 @@ func renderTemplate(t templ.Component) string {
3227
return buf.String()
3328
}
3429

35-
func ReadMarkdownFile(relative_filepath string) string {
36-
filepath := filepath.Join(Root, relative_filepath)
30+
func ReadMarkdownFile(filepath string) string {
3731
file, err := os.Open(filepath)
3832
if err != nil {
3933
return ""

services/markdown_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,20 @@ package services
33
import (
44
"io"
55
"os"
6+
"path"
67
"path/filepath"
78
"potblog/infrastructure"
89
"reflect"
10+
"runtime"
911
"strings"
1012
"testing"
1113
)
1214

15+
var (
16+
_, b, _, _ = runtime.Caller(0)
17+
Root = filepath.Join(filepath.Dir(b), "..")
18+
)
19+
1320
func pointerTo[T ~string](s T) *T {
1421
return &s
1522
}
@@ -56,7 +63,7 @@ func Test_ReadMarkdownFile(t *testing.T) {
5663
}
5764
for _, tt := range tests {
5865
t.Run(tt.name, func(t *testing.T) {
59-
if got := ReadMarkdownFile(tt.args.filename); got != tt.want {
66+
if got := ReadMarkdownFile(path.Join(Root, tt.args.filename)); got != tt.want {
6067
t.Errorf("ReadMarkdownFile() = %v\nWant %v", got, tt.want)
6168
}
6269
})
@@ -92,6 +99,7 @@ func TestMarkdownToHTML(t *testing.T) {
9299
},
93100
}
94101
for _, tt := range tests {
102+
os.Setenv("ROOT_DIR", Root)
95103
t.Run(tt.name, func(t *testing.T) {
96104
got, err := ConvertMarkdownToHTML(tt.args.md)
97105
if (err != nil) != tt.wantErr {

0 commit comments

Comments
 (0)