Skip to content

Commit

Permalink
permitting empty files again. Fixing some tests to now pass.
Browse files Browse the repository at this point in the history
  • Loading branch information
spf13 committed May 1, 2014
1 parent 3395e1c commit fb7d45e
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 39 deletions.
3 changes: 2 additions & 1 deletion hugolib/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func BenchmarkParsePage(b *testing.B) {
sample.ReadFrom(f)
b.ResetTimer()
for i := 0; i < b.N; i++ {
ReadFrom(bytes.NewReader(sample.Bytes()), "bench")
page, _ := NewPage("bench")
page.ReadFrom(bytes.NewReader(sample.Bytes()))
}
}
29 changes: 22 additions & 7 deletions hugolib/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,16 @@ func layouts(types string, layout string) (layouts []string) {
return
}

func NewPageFrom(buf io.Reader, name string) (page *Page, err error) {
p, err := NewPage(name)
if err != nil {
return p, err
}
err = p.ReadFrom(buf)

return p, err
}

func NewPage(name string) (page *Page, err error) {
if len(name) == 0 {
return nil, errors.New("Zero length page name")
Expand Down Expand Up @@ -296,6 +306,9 @@ func (p *Page) RelPermalink() (string, error) {
}

func (page *Page) update(f interface{}) error {
if f == nil {
return fmt.Errorf("no metadata found")
}
m := f.(map[string]interface{})

for k, v := range m {
Expand Down Expand Up @@ -537,13 +550,15 @@ func (page *Page) parse(reader io.Reader) error {
page.renderable = psr.IsRenderable()
page.frontmatter = psr.FrontMatter()
meta, err := psr.Metadata()
if err != nil {
jww.ERROR.Printf("Error parsing page meta data for %s", page.FileName)
jww.ERROR.Println(err)
return err
}
if err = page.update(meta); err != nil {
return err
if meta != nil {
if err != nil {
jww.ERROR.Printf("Error parsing page meta data for %s", page.FileName)
jww.ERROR.Println(err)
return err
}
if err = page.update(meta); err != nil {
return err
}
}

page.rawContent = psr.Content()
Expand Down
4 changes: 3 additions & 1 deletion hugolib/page_taxonomy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ func TestParseTaxonomies(t *testing.T) {
PAGE_YAML_WITH_TAXONOMIES_A,
PAGE_YAML_WITH_TAXONOMIES_B,
} {
p, err := ReadFrom(strings.NewReader(test), "page/with/taxonomy")

p, _ := NewPage("page/with/taxonomy")
err := p.ReadFrom(strings.NewReader(test))
if err != nil {
t.Fatalf("Failed parsing %q: %s", test, err)
}
Expand Down
52 changes: 34 additions & 18 deletions hugolib/page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ func checkError(t *testing.T, err error, expected string) {
}

func TestDegenerateEmptyPageZeroLengthName(t *testing.T) {
_, err := ReadFrom(strings.NewReader(EMPTY_PAGE), "")

_, err := NewPage("")
if err == nil {
t.Fatalf("A zero length page name must return an error")
}
Expand All @@ -233,12 +234,10 @@ func TestDegenerateEmptyPageZeroLengthName(t *testing.T) {
}

func TestDegenerateEmptyPage(t *testing.T) {
_, err := ReadFrom(strings.NewReader(EMPTY_PAGE), "test")
_, err := NewPageFrom(strings.NewReader(EMPTY_PAGE), "test")
if err != nil {
t.Fatalf("Empty files should not trigger an error. Should be able to touch a file while watching without erroring out.")
}

//checkError(t, err, "EOF")
}

func checkPageTitle(t *testing.T, page *Page, title string) {
Expand Down Expand Up @@ -297,7 +296,8 @@ func checkTruncation(t *testing.T, page *Page, shouldBe bool, msg string) {
}

func TestCreateNewPage(t *testing.T) {
p, err := ReadFrom(strings.NewReader(SIMPLE_PAGE), "simple.md")
p, _ := NewPage("simple.md")
err := p.ReadFrom(strings.NewReader(SIMPLE_PAGE))
p.Convert()

if err != nil {
Expand All @@ -312,7 +312,8 @@ func TestCreateNewPage(t *testing.T) {
}

func TestPageWithDelimiter(t *testing.T) {
p, err := ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_SUMMARY_DELIMITER), "simple.md")
p, _ := NewPage("simple.md")
err := p.ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_SUMMARY_DELIMITER))
p.Convert()
if err != nil {
t.Fatalf("Unable to create a page with frontmatter and body content: %s", err)
Expand All @@ -326,7 +327,8 @@ func TestPageWithDelimiter(t *testing.T) {
}

func TestPageWithShortCodeInSummary(t *testing.T) {
p, err := ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_SHORTCODE_IN_SUMMARY), "simple.md")
p, _ := NewPage("simple.md")
err := p.ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_SHORTCODE_IN_SUMMARY))
p.Convert()
if err != nil {
t.Fatalf("Unable to create a page with frontmatter and body content: %s", err)
Expand All @@ -339,7 +341,8 @@ func TestPageWithShortCodeInSummary(t *testing.T) {
}

func TestTableOfContents(t *testing.T) {
p, err := ReadFrom(strings.NewReader(PAGE_WITH_TOC), "tocpage.md")
p, _ := NewPage("tocpage.md")
err := p.ReadFrom(strings.NewReader(PAGE_WITH_TOC))
p.Convert()
if err != nil {
t.Fatalf("Unable to create a page with frontmatter and body content: %s", err)
Expand All @@ -349,7 +352,8 @@ func TestTableOfContents(t *testing.T) {
}

func TestPageWithMoreTag(t *testing.T) {
p, err := ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_SUMMARY_DELIMITER_SAME_LINE), "simple.md")
p, _ := NewPage("simple.md")
err := p.ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_SUMMARY_DELIMITER_SAME_LINE))
p.Convert()
if err != nil {
t.Fatalf("Unable to create a page with frontmatter and body content: %s", err)
Expand All @@ -362,7 +366,8 @@ func TestPageWithMoreTag(t *testing.T) {
}

func TestPageWithDate(t *testing.T) {
p, err := ReadFrom(strings.NewReader(SIMPLE_PAGE_RFC3339_DATE), "simple")
p, _ := NewPage("simple.md")
err := p.ReadFrom(strings.NewReader(SIMPLE_PAGE_RFC3339_DATE))
p.Convert()
if err != nil {
t.Fatalf("Unable to create a page with frontmatter and body content: %s", err)
Expand All @@ -375,7 +380,8 @@ func TestPageWithDate(t *testing.T) {
}

func TestWordCount(t *testing.T) {
p, err := ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_LONG_CONTENT), "simple.md")
p, _ := NewPage("simple.md")
err := p.ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_LONG_CONTENT))
p.Convert()
p.analyzePage()
if err != nil {
Expand Down Expand Up @@ -408,7 +414,8 @@ func TestCreatePage(t *testing.T) {
}

for _, test := range tests {
if _, err := ReadFrom(strings.NewReader(test.r), "page"); err != nil {
p, _ := NewPage("page")
if err := p.ReadFrom(strings.NewReader(test.r)); err != nil {
t.Errorf("Unable to parse page: %s", err)
}
}
Expand All @@ -422,7 +429,9 @@ func TestDegenerateInvalidFrontMatterShortDelim(t *testing.T) {
{INVALID_FRONT_MATTER_SHORT_DELIM_ENDING, "Unable to read frontmatter at filepos 45: EOF"},
}
for _, test := range tests {
_, err := ReadFrom(strings.NewReader(test.r), "invalid/front/matter/short/delim")

p, _ := NewPage("invalid/front/matter/short/delim")
err := p.ReadFrom(strings.NewReader(test.r))
checkError(t, err, test.err)
}
}
Expand All @@ -439,15 +448,19 @@ func TestShouldRenderContent(t *testing.T) {
}

for _, test := range tests {
p := pageMust(ReadFrom(strings.NewReader(test.text), "render/front/matter"))

p, _ := NewPage("render/front/matter")
err := p.ReadFrom(strings.NewReader(test.text))
p = pageMust(p, err)
if p.IsRenderable() != test.render {
t.Errorf("expected p.IsRenderable() == %t, got %t", test.render, p.IsRenderable())
}
}
}

func TestDifferentFrontMatterVarTypes(t *testing.T) {
page, _ := ReadFrom(strings.NewReader(PAGE_WITH_VARIOUS_FRONTMATTER_TYPES), "test/file1.md")
page, _ := NewPage("test/file1.md")
_ = page.ReadFrom(strings.NewReader(PAGE_WITH_VARIOUS_FRONTMATTER_TYPES))

dateval, _ := time.Parse(time.RFC3339, "1979-05-27T07:32:00Z")
if page.GetParam("a_string") != "bar" {
Expand All @@ -468,14 +481,16 @@ func TestDifferentFrontMatterVarTypes(t *testing.T) {
}

func TestDegenerateInvalidFrontMatterLeadingWhitespace(t *testing.T) {
_, err := ReadFrom(strings.NewReader(INVALID_FRONT_MATTER_LEADING_WS), "invalid/front/matter/leading/ws")
p, _ := NewPage("invalid/front/matter/leading/ws")
err := p.ReadFrom(strings.NewReader(INVALID_FRONT_MATTER_LEADING_WS))
if err != nil {
t.Fatalf("Unable to parse front matter given leading whitespace: %s", err)
}
}

func TestSectionEvaluation(t *testing.T) {
page, _ := ReadFrom(strings.NewReader(SIMPLE_PAGE), "blue/file1.md")
page, _ := NewPage("blue/file1.md")
page.ReadFrom(strings.NewReader(SIMPLE_PAGE))
if page.Section != "blue" {
t.Errorf("Section should be %s, got: %s", "blue", page.Section)
}
Expand Down Expand Up @@ -519,7 +534,8 @@ func TestLayoutOverride(t *testing.T) {
{SIMPLE_PAGE_TYPE_LAYOUT, path_no_directory, L("barfoo/buzfoo.html", "buzfoo.html")},
}
for _, test := range tests {
p, err := ReadFrom(strings.NewReader(test.content), test.path)
p, _ := NewPage(test.path)
err := p.ReadFrom(strings.NewReader(test.content))
if err != nil {
t.Fatalf("Unable to parse content:\n%s\n", test.content)
}
Expand Down
4 changes: 2 additions & 2 deletions hugolib/page_time_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ date: 02 May 2010 15:29 PST
Page With Date HugoLong`

func TestDegenerateDateFrontMatter(t *testing.T) {
p, _ := ReadFrom(strings.NewReader(PAGE_WITH_INVALID_DATE), "page/with/invalid/date")
p, _ := NewPageFrom(strings.NewReader(PAGE_WITH_INVALID_DATE), "page/with/invalid/date")
if p.Date != *new(time.Time) {
t.Fatalf("Date should be set to time.Time zero value. Got: %s", p.Date)
}
Expand Down Expand Up @@ -116,7 +116,7 @@ func TestParsingDateInFrontMatter(t *testing.T) {
if e != nil {
t.Fatalf("Unable to parse date time (RFC3339) for running the test: %s", e)
}
p, err := ReadFrom(strings.NewReader(test.buf), "page/with/date")
p, err := NewPageFrom(strings.NewReader(test.buf), "page/with/date")
if err != nil {
t.Fatalf("Expected to be able to parse page.")
}
Expand Down
6 changes: 3 additions & 3 deletions hugolib/path_seperators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ Sample Text
`

func TestDegenerateMissingFolderInPageFilename(t *testing.T) {
p, err := ReadFrom(strings.NewReader(SIMPLE_PAGE_YAML), path.Join("foobar"))
p, err := NewPageFrom(strings.NewReader(SIMPLE_PAGE_YAML), path.Join("foobar"))
if err != nil {
t.Fatalf("Error in ReadFrom")
t.Fatalf("Error in NewPageFrom")
}
if p.Section != "" {
t.Fatalf("No section should be set for a file path: foobar")
Expand All @@ -35,7 +35,7 @@ func TestNewPageWithFilePath(t *testing.T) {
}

for _, el := range toCheck {
p, err := ReadFrom(strings.NewReader(SIMPLE_PAGE_YAML), el.input)
p, err := NewPageFrom(strings.NewReader(SIMPLE_PAGE_YAML), el.input)
p.guessSection()
if err != nil {
t.Errorf("Reading from SIMPLE_PAGE_YAML resulted in an error: %s", err)
Expand Down
2 changes: 1 addition & 1 deletion hugolib/permalinks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestPermalinkValidation(t *testing.T) {
}

func TestPermalinkExpansion(t *testing.T) {
page, err := ReadFrom(strings.NewReader(SIMPLE_PAGE_JSON), "blue/test-page.md")
page, err := NewPageFrom(strings.NewReader(SIMPLE_PAGE_JSON), "blue/test-page.md")
if err != nil {
t.Fatalf("failed before we began, could not parse SIMPLE_PAGE_JSON: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion hugolib/shortcode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func pageFromString(in, filename string) (*Page, error) {
return ReadFrom(strings.NewReader(in), filename)
return NewPageFrom(strings.NewReader(in), filename)
}

func CheckShortCodeMatch(t *testing.T, input, expected string, template Template) {
Expand Down
8 changes: 4 additions & 4 deletions hugolib/site_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func pageMust(p *Page, err error) *Page {
}

func TestDegenerateRenderThingMissingTemplate(t *testing.T) {
p, _ := ReadFrom(strings.NewReader(PAGE_SIMPLE_TITLE), "content/a/file.md")
p, _ := NewPageFrom(strings.NewReader(PAGE_SIMPLE_TITLE), "content/a/file.md")
p.Convert()
s := new(Site)
s.prepTemplates()
Expand Down Expand Up @@ -109,7 +109,7 @@ func TestRenderThing(t *testing.T) {
s.prepTemplates()

for i, test := range tests {
p, err := ReadFrom(strings.NewReader(test.content), "content/a/file.md")
p, err := NewPageFrom(strings.NewReader(test.content), "content/a/file.md")
p.Convert()
if err != nil {
t.Fatalf("Error parsing buffer: %s", err)
Expand Down Expand Up @@ -158,7 +158,7 @@ func TestRenderThingOrDefault(t *testing.T) {
s.prepTemplates()

for i, test := range tests {
p, err := ReadFrom(strings.NewReader(PAGE_SIMPLE_TITLE), "content/a/file.md")
p, err := NewPageFrom(strings.NewReader(PAGE_SIMPLE_TITLE), "content/a/file.md")
if err != nil {
t.Fatalf("Error parsing buffer: %s", err)
}
Expand Down Expand Up @@ -205,7 +205,7 @@ func TestTargetPath(t *testing.T) {
}

for _, test := range tests {
p := pageMust(ReadFrom(strings.NewReader(test.content), helpers.AbsPathify(test.doc)))
p := pageMust(NewPageFrom(strings.NewReader(test.content), helpers.AbsPathify(test.doc)))

expected := test.expectedOutFile

Expand Down
2 changes: 1 addition & 1 deletion hugolib/taxonomy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

func TestSitePossibleTaxonomies(t *testing.T) {
site := new(Site)
page, _ := ReadFrom(strings.NewReader(PAGE_YAML_WITH_TAXONOMIES_A), "path/to/page")
page, _ := NewPageFrom(strings.NewReader(PAGE_YAML_WITH_TAXONOMIES_A), "path/to/page")
site.Pages = append(site.Pages, page)
taxonomies := site.possibleTaxonomies()
if !compareStringSlice(taxonomies, []string{"tags", "categories"}) {
Expand Down

0 comments on commit fb7d45e

Please sign in to comment.