permitting empty files again. Fixing some tests to now pass.

This commit is contained in:
spf13 2014-05-01 14:11:56 -04:00
parent 3395e1cb92
commit fb7d45e613
10 changed files with 73 additions and 39 deletions

View file

@ -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()))
}
}

View file

@ -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")
@ -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 {
@ -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()

View file

@ -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)
}

View file

@ -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")
}
@ -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) {
@ -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 {
@ -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)
@ -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)
@ -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)
@ -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)
@ -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)
@ -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 {
@ -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)
}
}
@ -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)
}
}
@ -439,7 +448,10 @@ 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())
}
@ -447,7 +459,8 @@ func TestShouldRenderContent(t *testing.T) {
}
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" {
@ -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)
}
@ -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)
}

View file

@ -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)
}
@ -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.")
}

View file

@ -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")
@ -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)

View file

@ -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)
}

View file

@ -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) {

View file

@ -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()
@ -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)
@ -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)
}
@ -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

View file

@ -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"}) {