Skip to content

Commit 1745b68

Browse files
committed
create unpublished posts, just don't add them to the index or homepage
1 parent 72faac8 commit 1745b68

File tree

4 files changed

+40
-15
lines changed

4 files changed

+40
-15
lines changed

assertions_for_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ in
2525
}
2626
}
2727

28+
func (a Assertions) StringDoesNotContain(str, substr string) {
29+
if strings.Contains(str, substr) {
30+
a.test.Helper()
31+
a.test.Errorf(`Expected not to find
32+
"%s"
33+
in
34+
"%s"
35+
`, substr, str)
36+
}
37+
}
38+
2839
func (a Assertions) True(b bool, s string) {
2940
if !b {
3041
a.test.Helper()

posts.go

+13
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,16 @@ func (ps Posts) Drop(n int) Posts {
4141
func SortPostsByDate(ps *Posts) {
4242
sort.Sort(ps)
4343
}
44+
45+
// Published returns a copy of the Posts, filtering out the unpublished posts
46+
func (ps Posts) Published() Posts {
47+
var onlyPublished = make(Posts, len(ps))
48+
49+
for _, post := range ps {
50+
if post.Published {
51+
onlyPublished = append(onlyPublished, post)
52+
}
53+
}
54+
55+
return onlyPublished
56+
}

write.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,6 @@ func makePage(siteDirectory string, page *Page, tmplt *template.Template) error
6666
}
6767

6868
func makePost(siteDirectory string, post *Post, posts *Posts, tmplt *template.Template) error {
69-
if !post.Published {
70-
return nil
71-
}
72-
7369
path := fmt.Sprintf("%s/posts/%s", siteDirectory, post.Path())
7470
err := os.MkdirAll(path, os.FileMode(0777))
7571

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

113-
recentPost := (*posts)[0]
109+
recentPost := (posts.Published())[0]
114110

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

141-
err = t.ExecuteTemplate(f, "index", posts)
137+
err = t.ExecuteTemplate(f, "index", posts.Published())
142138
return err
143139
}

write_test.go

+14-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package blawg
33
import (
44
"bytes"
55
"html/template"
6-
"io/ioutil"
76
"os"
87
"testing"
98
"time"
@@ -53,7 +52,7 @@ func TestMakePosts(t *testing.T) {
5352
for _, post := range posts {
5453
expectedFile := testSiteDirectory + "/posts/" + post.Path() + "index.html"
5554
assert.FileExists(expectedFile)
56-
contents, _ := ioutil.ReadFile(expectedFile)
55+
contents, _ := os.ReadFile(expectedFile)
5756
assert.StringContains(string(contents), string(post.Title))
5857
}
5958

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

6362
func TestPostsIndex(t *testing.T) {
6463
assert := NewAssertions(t)
65-
postOne := testPost("Abba", "First Post Body", 1979, 12, 5)
66-
postTwo := testPost("Second Post", "Second Post Body", 1989, 12, 5)
64+
post := testPost("Abba", "First Post Body", 1979, 12, 5)
65+
unpublishedPost := testPost("Unpublished Post", "Second Post Body", 1989, 12, 5)
66+
unpublishedPost.Published = false
67+
6768
posts := Posts{
68-
postOne,
69-
postTwo,
69+
post,
70+
unpublishedPost,
7071
}
7172

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

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

7879
assert.FileExists(testSiteDirectory + "/posts/index.html")
80+
fileContents, _ := os.ReadFile(testSiteDirectory + "/posts/index.html")
81+
82+
assert.StringContains(string(fileContents), string(post.Title))
83+
assert.StringDoesNotContain(string(fileContents), string(unpublishedPost.Title))
7984
}
8085

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

139-
unexpectedFile := testSiteDirectory + "/posts/" + post.Path() + "index.html"
140-
assert.FileDoesntExist(unexpectedFile)
144+
unpublishedFile := testSiteDirectory + "/posts/" + post.Path() + "index.html"
145+
assert.FileExists(unpublishedFile)
141146

142147
tearDownTestSite(t)
143148
}

0 commit comments

Comments
 (0)