Skip to content

Commit

Permalink
create unpublished posts, just don't add them to the index or homepage
Browse files Browse the repository at this point in the history
  • Loading branch information
gypsydave5 committed Dec 5, 2024
1 parent 72faac8 commit 1745b68
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 15 deletions.
11 changes: 11 additions & 0 deletions assertions_for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ in
}
}

func (a Assertions) StringDoesNotContain(str, substr string) {
if strings.Contains(str, substr) {
a.test.Helper()
a.test.Errorf(`Expected not to find
"%s"
in
"%s"
`, substr, str)
}
}

func (a Assertions) True(b bool, s string) {
if !b {
a.test.Helper()
Expand Down
13 changes: 13 additions & 0 deletions posts.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,16 @@ func (ps Posts) Drop(n int) Posts {
func SortPostsByDate(ps *Posts) {
sort.Sort(ps)
}

// Published returns a copy of the Posts, filtering out the unpublished posts
func (ps Posts) Published() Posts {
var onlyPublished = make(Posts, len(ps))

for _, post := range ps {
if post.Published {
onlyPublished = append(onlyPublished, post)
}
}

return onlyPublished
}
8 changes: 2 additions & 6 deletions write.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ func makePage(siteDirectory string, page *Page, tmplt *template.Template) error
}

func makePost(siteDirectory string, post *Post, posts *Posts, tmplt *template.Template) error {
if !post.Published {
return nil
}

path := fmt.Sprintf("%s/posts/%s", siteDirectory, post.Path())
err := os.MkdirAll(path, os.FileMode(0777))

Expand Down Expand Up @@ -110,7 +106,7 @@ func makeHomepage(siteDirectory string, posts *Posts, t *template.Template) erro
return errors.New("no posts")
}

recentPost := (*posts)[0]
recentPost := (posts.Published())[0]

page := PostPage{
&recentPost,
Expand Down Expand Up @@ -138,6 +134,6 @@ func makePostIndex(siteDirectory string, posts *Posts, t *template.Template) err
}
defer f.Close()

err = t.ExecuteTemplate(f, "index", posts)
err = t.ExecuteTemplate(f, "index", posts.Published())
return err
}
23 changes: 14 additions & 9 deletions write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package blawg
import (
"bytes"
"html/template"
"io/ioutil"
"os"
"testing"
"time"
Expand Down Expand Up @@ -53,7 +52,7 @@ func TestMakePosts(t *testing.T) {
for _, post := range posts {
expectedFile := testSiteDirectory + "/posts/" + post.Path() + "index.html"
assert.FileExists(expectedFile)
contents, _ := ioutil.ReadFile(expectedFile)
contents, _ := os.ReadFile(expectedFile)

Check failure on line 55 in write_test.go

View workflow job for this annotation

GitHub Actions / build

undefined: os.ReadFile
assert.StringContains(string(contents), string(post.Title))
}

Expand All @@ -62,20 +61,26 @@ func TestMakePosts(t *testing.T) {

func TestPostsIndex(t *testing.T) {
assert := NewAssertions(t)
postOne := testPost("Abba", "First Post Body", 1979, 12, 5)
postTwo := testPost("Second Post", "Second Post Body", 1989, 12, 5)
post := testPost("Abba", "First Post Body", 1979, 12, 5)
unpublishedPost := testPost("Unpublished Post", "Second Post Body", 1989, 12, 5)
unpublishedPost.Published = false

posts := Posts{
postOne,
postTwo,
post,
unpublishedPost,
}

indexTemplate, err := template.New("index").Parse(`<p>{{range .}}{{.Title}}{{end}}</p>"`)
indexTemplate, err := template.New("index").Parse("{{range .}}<p>{{.Title}}</p>\n{{end}}")
assert.NotError(err)

err = makePostIndex(testSiteDirectory, &posts, indexTemplate)
assert.NotError(err)

assert.FileExists(testSiteDirectory + "/posts/index.html")
fileContents, _ := os.ReadFile(testSiteDirectory + "/posts/index.html")

Check failure on line 80 in write_test.go

View workflow job for this annotation

GitHub Actions / build

undefined: os.ReadFile

assert.StringContains(string(fileContents), string(post.Title))
assert.StringDoesNotContain(string(fileContents), string(unpublishedPost.Title))
}

func TestBuildPostPath(t *testing.T) {
Expand Down Expand Up @@ -136,8 +141,8 @@ func TestNotSavingUnpublishedPost(t *testing.T) {
err = makePost(testSiteDirectory, &post, nil, stubTemplate())
assert.NotError(err)

unexpectedFile := testSiteDirectory + "/posts/" + post.Path() + "index.html"
assert.FileDoesntExist(unexpectedFile)
unpublishedFile := testSiteDirectory + "/posts/" + post.Path() + "index.html"
assert.FileExists(unpublishedFile)

tearDownTestSite(t)
}
Expand Down

0 comments on commit 1745b68

Please sign in to comment.