Skip to content

Commit

Permalink
Merge pull request #99 from halkt/not-localpath
Browse files Browse the repository at this point in the history
記事更新時に更新元ファイルのentryHeaderのURLが未設定の場合も更新ができるように修正
  • Loading branch information
Songmu authored Oct 14, 2023
2 parents 9326b9d + 76ce8e5 commit 3c914cc
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
9 changes: 8 additions & 1 deletion broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (b *broker) PutEntry(e *entry) error {
if e.CustomPath != "" {
newEntry.CustomPath = e.CustomPath
}
return b.Store(newEntry, b.LocalPath(newEntry), b.LocalPath(e))
return b.Store(newEntry, b.LocalPath(newEntry), b.originalPath(e))
}

func (b *broker) PostEntry(e *entry, isPage bool) error {
Expand All @@ -163,6 +163,13 @@ func (b *broker) PostEntry(e *entry, isPage bool) error {
return b.Store(newEntry, b.LocalPath(newEntry), "")
}

func (b *broker) originalPath(e *entry) string {
if e.URL == nil {
return ""
}
return b.LocalPath(e)
}

func atomEndpointURLRoot(bc *blogConfig) string {
owner := bc.Owner
if owner == "" {
Expand Down
63 changes: 63 additions & 0 deletions broker_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package main

import (
"net/url"
"runtime"
"testing"
"time"

"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -38,3 +41,63 @@ func TestEntryEndPointUrl(t *testing.T) {
})
}
}

func TestOriginalPath(t *testing.T) {
u, _ := url.Parse("http://hatenablog.example.com/2")
jst, _ := time.LoadLocation("Asia/Tokyo")
d := time.Date(2023, 10, 10, 0, 0, 0, 0, jst)

testCases := []struct {
name string
entry entry
expect string
expectWindows string
}{
{
name: "entry has URL",
entry: entry{
entryHeader: &entryHeader{
URL: &entryURL{u},
EditURL: u.String() + "/edit",
Title: "test",
Date: &d,
IsDraft: true,
},
LastModified: &d,
Content: "テスト",
},
expect: "example1.hatenablog.com/2.md",
expectWindows: "example1.hatenablog.com\\2.md",
},
{
name: "Not URL",
entry: entry{
entryHeader: &entryHeader{
EditURL: u.String() + "/edit",
Title: "hoge",
IsDraft: true,
},
LastModified: &d,
Content: "テスト",
},
expect: "",
expectWindows: "",
},
}

config := blogConfig{
BlogID: "example1.hatenablog.com",
Username: "sample1",
}
broker := newBroker(&config)

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got := broker.originalPath(&tc.entry)
if runtime.GOOS == "windows" {
tc.expect = tc.expectWindows
}
assert.Equal(t, tc.expect, got)
})
}
}

0 comments on commit 3c914cc

Please sign in to comment.