Skip to content

Commit

Permalink
fix(parser): provide actual filename in file inclusion error msg
Browse files Browse the repository at this point in the history
Fixes bytesparadise#384

Signed-off-by: Xavier Coulon <[email protected]>
  • Loading branch information
xcoulon committed Sep 8, 2019
1 parent d65ff0c commit 87f0b24
Show file tree
Hide file tree
Showing 42 changed files with 474 additions and 467 deletions.
4 changes: 2 additions & 2 deletions pkg/parser/blank_line_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ second paragraph`
},
},
}
verifyPreflight(expected, doc)
verifyPreflight("test.adoc", expected, doc)
})
It("blank line with spaces and tabs between 2 paragraphs and after second paragraph", func() {
doc := `first paragraph
Expand Down Expand Up @@ -63,7 +63,7 @@ second paragraph
},
},
}
verifyPreflight(expected, doc)
verifyPreflight("test.adoc", expected, doc)
})

})
2 changes: 1 addition & 1 deletion pkg/parser/comment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ a second paragraph`
},
},
}
verifyPreflight(expected, doc)
verifyPreflight("test.adoc", expected, doc)
})
})

Expand Down
4 changes: 2 additions & 2 deletions pkg/parser/cross_reference_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ with some content linked to <<thetitle>>!`
},
},
}
verifyPreflight(expected, source)
verifyPreflight("test.adoc", expected, source)
})

It("cross-reference with custom id and label", func() {
Expand Down Expand Up @@ -92,7 +92,7 @@ with some content linked to <<thetitle,a label to the title>>!`
},
},
}
verifyPreflight(expected, source)
verifyPreflight("test.adoc", expected, source)
})
})
})
Expand Down
10 changes: 5 additions & 5 deletions pkg/parser/delimited_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ var _ = Describe("delimited blocks - preflight", func() {
},
},
}
verifyPreflight(expected, source)
verifyPreflight("test.adoc", expected, source)
})

It("fenced block after a paragraph", func() {
Expand Down Expand Up @@ -158,7 +158,7 @@ var _ = Describe("delimited blocks - preflight", func() {
},
},
}
verifyPreflight(expected, source)
verifyPreflight("test.adoc", expected, source)
})

It("fenced block with unclosed delimiter", func() {
Expand Down Expand Up @@ -424,7 +424,7 @@ then a normal paragraph.`
},
},
}
verifyPreflight(expected, source)
verifyPreflight("test.adoc", expected, source)
})

It("listing block just after a paragraph", func() {
Expand Down Expand Up @@ -460,7 +460,7 @@ some listing code
},
},
}
verifyPreflight(expected, source)
verifyPreflight("test.adoc", expected, source)
})

It("listing block with unclosed delimiter", func() {
Expand Down Expand Up @@ -722,7 +722,7 @@ paragraphs
},
},
}
verifyPreflight(expected, source)
verifyPreflight("test.adoc", expected, source)
})
})

Expand Down
28 changes: 18 additions & 10 deletions pkg/parser/document_preprocessing.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func parseElements(filename string, elements []interface{}, attrs types.Document
result = append(result, e)
case types.FileInclusion:
// read the file and include its content
embedded, err := parseFileToInclude(e, attrs, opts...)
embedded, err := parseFileToInclude(filename, e, attrs, opts...)
if err != nil {
// do not fail, but instead report the error in the console
log.Errorf("failed to include file '%s': %v", e.Location, err)
Expand Down Expand Up @@ -91,16 +91,24 @@ var invalidFileTmpl *template.Template

func init() {
var err error
invalidFileTmpl, err = template.New("invalid file to include").Parse(`Unresolved directive in test.adoc - {{ . }}`)
invalidFileTmpl, err = template.New("invalid file to include").Parse(`Unresolved directive in {{ .Filename }} - {{ .Error }}`)
if err != nil {
log.Fatalf("failed to initialize template: %v", err)
}
}

func invalidFileErrMsg(path, rawText string, err error) (types.PreflightDocument, error) {
type invalidFileData struct {
Filename string
Error string
}

func invalidFileErrMsg(filename, path, rawText string, err error) (types.PreflightDocument, error) {
log.WithError(err).Errorf("failed to include '%s'", path)
buf := bytes.NewBuffer(nil)
err = invalidFileTmpl.Execute(buf, rawText)
err = invalidFileTmpl.Execute(buf, invalidFileData{
Filename: filename,
Error: rawText,
})
if err != nil {
return types.PreflightDocument{}, err
}
Expand All @@ -120,31 +128,31 @@ func invalidFileErrMsg(path, rawText string, err error) (types.PreflightDocument
}, nil
}

func parseFileToInclude(incl types.FileInclusion, attrs types.DocumentAttributes, opts ...Option) (types.PreflightDocument, error) {
func parseFileToInclude(filename string, incl types.FileInclusion, attrs types.DocumentAttributes, opts ...Option) (types.PreflightDocument, error) {
path := incl.Location.Resolve(attrs)
log.Debugf("parsing '%s'...", path)
f, absPath, done, err := open(path)
defer done()
if err != nil {
return invalidFileErrMsg(path, incl.RawText, err)
return invalidFileErrMsg(filename, path, incl.RawText, err)
}
content := bytes.NewBuffer(nil)
scanner := bufio.NewScanner(bufio.NewReader(f))
if lineRanges, ok := incl.LineRanges(); ok {
if err := readWithinLines(scanner, content, lineRanges); err != nil {
return invalidFileErrMsg(path, incl.RawText, err)
return invalidFileErrMsg(filename, path, incl.RawText, err)
}
} else if tagRanges, ok := incl.TagRanges(); ok {
if err := readWithinTags(scanner, content, tagRanges); err != nil {
return invalidFileErrMsg(path, incl.RawText, err)
return invalidFileErrMsg(filename, path, incl.RawText, err)
}
} else {
if err := readAll(scanner, content); err != nil {
return invalidFileErrMsg(path, incl.RawText, err)
return invalidFileErrMsg(filename, path, incl.RawText, err)
}
}
if err := scanner.Err(); err != nil {
msg, err2 := invalidFileErrMsg(path, incl.RawText, err)
msg, err2 := invalidFileErrMsg(filename, path, incl.RawText, err)
if err2 != nil {
return types.PreflightDocument{}, err2
}
Expand Down
Loading

0 comments on commit 87f0b24

Please sign in to comment.