Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[nit] refactoring #146

Merged
merged 7 commits into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ func newBroker(bc *blogConfig, w io.Writer) *broker {

func (b *broker) FetchRemoteEntries(published, drafts bool) ([]*entry, error) {
entries := []*entry{}
fixedPageURL := fixedPageEndpointURL(b.blogConfig)
staticPageURL := staticPageEndpointURL(b.blogConfig)
urls := []string{
entryEndPointUrl(b.blogConfig),
fixedPageURL,
staticPageURL,
}
for url := ""; true; {
if url == "" {
Expand All @@ -53,14 +53,14 @@ func (b *broker) FetchRemoteEntries(published, drafts bool) ([]*entry, error) {

feed, err := b.Client.GetFeed(url)
if err != nil {
if url == fixedPageURL {
// Ignore errors in the case of fixed pages, because fixed page is the feature
if url == staticPageURL {
// Ignore errors in the case of static pages, because static page is the feature
// only for pro users.
break
}
return nil, err
}
if b.rootURL != "" {
if b.rootURL == "" {
if l := feed.Links.Find("alternate"); l != nil {
b.rootURL = l.Href
}
Expand Down Expand Up @@ -98,7 +98,7 @@ func (b *broker) LocalPath(e *entry) string {
}
localPath := e.URL.Path

if e.IsDraft && strings.Contains(e.EditURL, "/atom/entry/") {
if e.IsDraft && e.isBlogEntry() {
subdir, entryPath := extractEntryPath(e.URL.Path)
if entryPath == "" {
return ""
Expand Down Expand Up @@ -132,7 +132,7 @@ func (b *broker) StoreFresh(e *entry, path string) (bool, error) {
func (b *broker) Store(e *entry, path, origPath string) error {
logf("store", "%s", path)

if e.IsDraft && strings.Contains(e.EditURL, "/atom/entry/") {
if e.IsDraft && e.isBlogEntry() {
_, entryPath := extractEntryPath(e.URL.Path)
if entryPath == "" {
return fmt.Errorf("invalid path: %s", e.URL.Path)
Expand Down Expand Up @@ -189,7 +189,7 @@ func (b *broker) PostEntry(e *entry, isPage bool) error {
if !isPage {
endPoint = entryEndPointUrl(b.blogConfig)
} else {
endPoint = fixedPageEndpointURL(b.blogConfig)
endPoint = staticPageEndpointURL(b.blogConfig)
}
newEntry, err := asEntry(b.Client.PostEntry(endPoint, e.atom()))
if err != nil {
Expand Down Expand Up @@ -222,6 +222,6 @@ func entryEndPointUrl(bc *blogConfig) string {
return atomEndpointURLRoot(bc) + "entry"
}

func fixedPageEndpointURL(bc *blogConfig) string {
func staticPageEndpointURL(bc *blogConfig) string {
return atomEndpointURLRoot(bc) + "page"
}
8 changes: 8 additions & 0 deletions entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ func (eh *entryHeader) blogID() (string, error) {
return paths[4], nil
}

func (eh *entryHeader) isBlogEntry() bool {
return strings.Contains(eh.EditURL, "/atom/entry/")
}

func (eh *entryHeader) isStaticPage() bool {
return strings.Contains(eh.EditURL, "/atom/page/")
}

// Entry is an entry stored on remote blog providers
type entry struct {
*entryHeader
Expand Down
73 changes: 66 additions & 7 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package main
import (
"bytes"
"fmt"
"net/url"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -78,6 +79,26 @@ func TestBlogsync(t *testing.T) {
}
})

t.Run("list", func(t *testing.T) {
if _, err := blogsync("list"); err != nil {
t.Fatal(err)
}
})

t.Run("fetchRootURL", func(t *testing.T) {
t.Log("fetchRootURL returns the remote root URL")
conf, err := loadConfiguration()
if err != nil {
t.Fatal(err)
}
remotRoot := conf.Get(blogID).fetchRootURL()
u, _ := url.Parse(remotRoot)
// XXX: In case of own URL, blogID and remote URL do not match.
if u.Hostname() != blogID {
t.Errorf("unexpected hostname. got: %s, expected: %s", u.Hostname(), blogID)
}
})

t.Run("post draft and publish", func(t *testing.T) {
t.Log("Post a draft without a custom path and check if the file is saved in the proper location")
app.Reader = strings.NewReader("draft\n")
Expand All @@ -97,14 +118,11 @@ func TestBlogsync(t *testing.T) {
t.Fatalf("unexpected draft file: %s", entryFile)
}

t.Log("Draft files under `_draft/` will revert to the original file name if the file is renamed and pushed again")
d, f := filepath.Split(entryFile)
movedPath := filepath.Join(d, "_"+f)
if err := os.Rename(entryFile, movedPath); err != nil {
t.Fatal(err)
if _, err := blogsync("pull"); err != nil {
t.Error(err)
}
originalEntryFile := entryFile
entryFile = movedPath

t.Log("Check entry metadata")
e, err := entryFromFile(entryFile)
if err != nil {
t.Fatal(err)
Expand All @@ -116,6 +134,14 @@ func TestBlogsync(t *testing.T) {
t.Errorf("Date is registered in a draft. Date: %s", *e.Date)
}

t.Log("Draft files under `_draft/` will revert to the original file name if the file is renamed and pushed again")
d, f := filepath.Split(entryFile)
movedPath := filepath.Join(d, "_"+f)
if err := os.Rename(entryFile, movedPath); err != nil {
t.Fatal(err)
}
originalEntryFile := entryFile
entryFile = movedPath
if err := appendFile(movedPath, "updated\n"); err != nil {
t.Fatal(err)
}
Expand All @@ -132,6 +158,11 @@ func TestBlogsync(t *testing.T) {
}
entryFile = draftFile

t.Log("Check if the draft is fetched and saved in the proper location")
if _, err := blogsync("fetch", entryFile); err != nil {
t.Error(err)
}

t.Log("When a draft is published, a URL is issued and the file is saved in the corresponding location")
publishedFile, err := blogsync("push", "--publish", entryFile)
if err != nil {
Expand Down Expand Up @@ -199,6 +230,34 @@ test`), 0644); err != nil {
t.Errorf("unexpected published file: %s", publishedFile)
}
})

t.Run("post static pages", func(t *testing.T) {
t.Log("Posting a static page without a custom path results in an error")
app.Reader = strings.NewReader("static\n")
if _, err := blogsync("post", "--page", blogID); err == nil {
t.Error("expected error did not occur")
}

t.Log("Posting a static page with a custom path saves the file in the specified location")
customPath := fmt.Sprintf("static-%s", time.Now().Format("20060102150405"))
app.Reader = strings.NewReader("static\n")
entryFile, err := blogsync("post", "--page", "--custom-path", customPath, blogID)
app.Reader = os.Stdin
if err != nil {
t.Fatal(err)
}
if entryFile != filepath.Join(dir, customPath+".md") {
t.Errorf("unexpected published file: %s", entryFile)
}

defer func() {
t.Log("remove the published entry")
if _, err := blogsync("remove", entryFile); err != nil {
t.Fatal(err)
}
}()
})

}

func appendFile(path string, content string) error {
Expand Down