diff --git a/libasciidoc.go b/libasciidoc.go index f66f25d5..5299f46f 100644 --- a/libasciidoc.go +++ b/libasciidoc.go @@ -35,14 +35,14 @@ func ConvertFileToHTML(ctx context.Context, filename string, output io.Writer, o return nil, errors.Wrapf(err, "error opening %s", filename) } defer file.Close() - return ConvertToHTML(ctx, file, output, options...) + return ConvertToHTML(ctx, filename, file, output, options...) } // ConvertToHTML converts the content of the given reader `r` into a full HTML document, written in the given writer `output`. // Returns an error if a problem occurred -func ConvertToHTML(ctx context.Context, r io.Reader, output io.Writer, options ...renderer.Option) (map[string]interface{}, error) { +func ConvertToHTML(ctx context.Context, filename string, r io.Reader, output io.Writer, options ...renderer.Option) (map[string]interface{}, error) { log.Debugf("parsing the asciidoc source...") - doc, err := parser.ParseDocument("", r) + doc, err := parser.ParseDocument(filename, r) if err != nil { return nil, errors.Wrapf(err, "error while parsing the document") } diff --git a/libasciidoc_test.go b/libasciidoc_test.go index daa88488..aa788793 100644 --- a/libasciidoc_test.go +++ b/libasciidoc_test.go @@ -146,6 +146,29 @@ a paragraph with _italic content_` Expect(source).To(RenderHTML5Body(expectedContent)) Expect(source).To(RenderHTML5Title(expectedTitle)) }) + + It("should include adoc file without leveloffset from local file", func() { + source := "include::test/includes/grandchild-include.adoc[]" + expected := `
+

first line of grandchild

+
+
+

last line of grandchild

+
` + Expect(source).To(RenderHTML5Body(expected, WithFilename("foo.adoc"))) + }) + + It("should include adoc file without leveloffset from relative file", func() { + source := "include::../test/includes/grandchild-include.adoc[]" + expected := `
+

first line of grandchild

+
+
+

last line of grandchild

+
` + + Expect(source).To(RenderHTML5Body(expected, WithFilename("tmp/foo.adoc"))) + }) }) Context("complete Document ", func() { diff --git a/pkg/parser/file_inclusion.go b/pkg/parser/file_inclusion.go index a6fcc81f..7944bbc7 100644 --- a/pkg/parser/file_inclusion.go +++ b/pkg/parser/file_inclusion.go @@ -26,9 +26,10 @@ func init() { 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) + currentDir := filepath.Dir(filename) + log.Debugf("parsing '%s' from '%s' (%s)", path, currentDir, filename) log.Debugf("file inclusion attributes: %s", spew.Sdump(incl.Attributes)) - f, absPath, done, err := open(path) + f, absPath, done, err := open(filepath.Join(currentDir, path)) defer done() if err != nil { return invalidFileErrMsg(filename, path, incl.RawText, err) @@ -211,6 +212,7 @@ func open(path string) (*os.File, string, func(), error) { return nil, "", func() {}, err } absPath, err := filepath.Abs(path) + log.Debugf("file path: %s", absPath) if err != nil { return nil, "", func() { log.Debugf("restoring current working dir to: %s", wd) diff --git a/pkg/parser/file_inclusion_test.go b/pkg/parser/file_inclusion_test.go index 3fc83cf6..a6af153d 100644 --- a/pkg/parser/file_inclusion_test.go +++ b/pkg/parser/file_inclusion_test.go @@ -80,7 +80,7 @@ var _ = Describe("file location", func() { var _ = Describe("file inclusions - preflight with preprocessing", func() { - It("should include adoc file without leveloffset", func() { + It("should include adoc file without leveloffset from local file", func() { console, reset := ConfigureLogger() defer reset() source := "include::../../test/includes/chapter-a.adoc[]" @@ -112,10 +112,46 @@ var _ = Describe("file inclusions - preflight with preprocessing", func() { }, }, } - Expect(source).To(BecomePreflightDocument(expected)) + Expect(source).To(BecomePreflightDocument(expected, WithFilename("foo.adoc"))) // verify no error/warning in logs Expect(console).ToNot(ContainAnyMessageWithLevels(log.ErrorLevel, log.WarnLevel)) + }) + It("should include adoc file without leveloffset from relative file", func() { + console, reset := ConfigureLogger() + defer reset() + source := "include::../../../test/includes/chapter-a.adoc[]" + expected := types.PreflightDocument{ + Blocks: []interface{}{ + types.Section{ + Attributes: types.ElementAttributes{ + types.AttrID: "chapter_a", + types.AttrCustomID: false, + }, + Level: 0, + Title: types.InlineElements{ + types.StringElement{ + Content: "Chapter A", + }, + }, + Elements: []interface{}{}, + }, + types.BlankLine{}, + types.Paragraph{ + Attributes: types.ElementAttributes{}, + Lines: []types.InlineElements{ + { + types.StringElement{ + Content: "content", + }, + }, + }, + }, + }, + } + Expect(source).To(BecomePreflightDocument(expected, WithFilename("tmp/foo.adoc"))) + // verify no error/warning in logs + Expect(console).ToNot(ContainAnyMessageWithLevels(log.ErrorLevel, log.WarnLevel)) }) It("should include adoc file with leveloffset", func() { @@ -1224,7 +1260,7 @@ include::../../test/includes/unknown.adoc[leveloffset=+1] Context("inclusion with attribute in path", func() { - It("should resolve path with attribute in standalone block", func() { + It("should resolve path with attribute in standalone block from local file", func() { source := `:includedir: ../../test/includes include::{includedir}/grandchild-include.adoc[]` @@ -1258,7 +1294,44 @@ include::{includedir}/grandchild-include.adoc[]` }, }, } - Expect(source).To(BecomePreflightDocument(expected)) + Expect(source).To(BecomePreflightDocument(expected, WithFilename("foo.adoc"))) + }) + + It("should resolve path with attribute in standalone block from relative file", func() { + source := `:includedir: ../../../test/includes + +include::{includedir}/grandchild-include.adoc[]` + expected := types.PreflightDocument{ + Blocks: []interface{}{ + types.DocumentAttributeDeclaration{ + Name: "includedir", + Value: "../../../test/includes", + }, + types.BlankLine{}, + types.Paragraph{ + Attributes: types.ElementAttributes{}, + Lines: []types.InlineElements{ + { + types.StringElement{ + Content: "first line of grandchild", + }, + }, + }, + }, + types.BlankLine{}, + types.Paragraph{ + Attributes: types.ElementAttributes{}, + Lines: []types.InlineElements{ + { + types.StringElement{ + Content: "last line of grandchild", + }, + }, + }, + }, + }, + } + Expect(source).To(BecomePreflightDocument(expected, WithFilename("tmp/foo.adoc"))) }) It("should resolve path with attribute in delimited block", func() { @@ -1401,6 +1474,50 @@ include::../../test/includes/hello_world.go.txt[lines=1] var _ = Describe("file inclusions - preflight without preprocessing", func() { + It("should include adoc file without leveloffset in local dir", func() { + console, reset := ConfigureLogger() + defer reset() + source := "include::../../test/includes/chapter-a.adoc[]" + expected := types.PreflightDocument{ + Blocks: []interface{}{ + types.FileInclusion{ + Attributes: types.ElementAttributes{}, + Location: types.Location{ + types.StringElement{ + Content: "../../test/includes/chapter-a.adoc", + }, + }, + RawText: source, + }, + }, + } + Expect(source).To(BecomePreflightDocument(expected, WithoutPreprocessing(), WithFilename("foo.adoc"))) + // verify no error/warning in logs + Expect(console).ToNot(ContainAnyMessageWithLevels(log.ErrorLevel, log.WarnLevel)) + }) + + It("should include adoc file without leveloffset in relative dir", func() { + console, reset := ConfigureLogger() + defer reset() + source := "include::../../../test/includes/chapter-a.adoc[]" + expected := types.PreflightDocument{ + Blocks: []interface{}{ + types.FileInclusion{ + Attributes: types.ElementAttributes{}, + Location: types.Location{ + types.StringElement{ + Content: "../../../test/includes/chapter-a.adoc", + }, + }, + RawText: source, + }, + }, + } + Expect(source).To(BecomePreflightDocument(expected, WithoutPreprocessing(), WithFilename("tmp/foo.adoc"))) + // verify no error/warning in logs + Expect(console).ToNot(ContainAnyMessageWithLevels(log.ErrorLevel, log.WarnLevel)) + }) + It("should include adoc file with leveloffset attribute", func() { source := "include::../../test/includes/chapter-a.adoc[leveloffset=+1]" expected := types.PreflightDocument{ @@ -1414,11 +1531,11 @@ var _ = Describe("file inclusions - preflight without preprocessing", func() { Content: "../../test/includes/chapter-a.adoc", }, }, - RawText: `include::../../test/includes/chapter-a.adoc[leveloffset=+1]`, + RawText: source, }, }, } - Expect(source).To(BecomePreflightDocumentWithoutPreprocessing(expected)) + Expect(source).To(BecomePreflightDocument(expected, WithoutPreprocessing())) }) Context("file inclusions in delimited blocks", func() { @@ -1446,7 +1563,7 @@ var _ = Describe("file inclusions - preflight without preprocessing", func() { }, }, } - Expect(source).To(BecomePreflightDocumentWithoutPreprocessing(expected)) + Expect(source).To(BecomePreflightDocument(expected, WithoutPreprocessing())) }) It("should include adoc file within listing block", func() { @@ -1472,7 +1589,7 @@ include::../../test/includes/chapter-a.adoc[] }, }, } - Expect(source).To(BecomePreflightDocumentWithoutPreprocessing(expected)) + Expect(source).To(BecomePreflightDocument(expected, WithoutPreprocessing())) }) It("should include adoc file within example block", func() { @@ -1498,7 +1615,7 @@ include::../../test/includes/chapter-a.adoc[] }, }, } - Expect(source).To(BecomePreflightDocumentWithoutPreprocessing(expected)) + Expect(source).To(BecomePreflightDocument(expected, WithoutPreprocessing())) }) It("should include adoc file within quote block", func() { @@ -1524,7 +1641,7 @@ ____` }, }, } - Expect(source).To(BecomePreflightDocumentWithoutPreprocessing(expected)) + Expect(source).To(BecomePreflightDocument(expected, WithoutPreprocessing())) }) It("should include adoc file within verse block", func() { @@ -1553,7 +1670,7 @@ ____` }, }, } - Expect(source).To(BecomePreflightDocumentWithoutPreprocessing(expected)) + Expect(source).To(BecomePreflightDocument(expected, WithoutPreprocessing())) }) It("should include adoc file within sidebar block", func() { @@ -1579,7 +1696,7 @@ include::../../test/includes/chapter-a.adoc[] }, }, } - Expect(source).To(BecomePreflightDocumentWithoutPreprocessing(expected)) + Expect(source).To(BecomePreflightDocument(expected, WithoutPreprocessing())) }) It("should include adoc file within passthrough block", func() { @@ -1606,7 +1723,7 @@ include::../../test/includes/chapter-a.adoc[] }, }, } - Expect(source).To(BecomePreflightDocumentWithoutPreprocessing(expected)) + Expect(source).To(BecomePreflightDocument(expected, WithoutPreprocessing())) }) }) @@ -1633,7 +1750,7 @@ include::../../test/includes/chapter-a.adoc[] }, }, } - Expect(source).To(BecomePreflightDocumentWithoutPreprocessing(expected)) + Expect(source).To(BecomePreflightDocument(expected, WithoutPreprocessing())) }) It("file inclusion with multiple unquoted lines", func() { @@ -1655,7 +1772,7 @@ include::../../test/includes/chapter-a.adoc[] }, }, } - Expect(source).To(BecomePreflightDocumentWithoutPreprocessing(expected)) + Expect(source).To(BecomePreflightDocument(expected, WithoutPreprocessing())) }) It("file inclusion with multiple unquoted ranges", func() { @@ -1679,7 +1796,7 @@ include::../../test/includes/chapter-a.adoc[] }, }, } - Expect(source).To(BecomePreflightDocumentWithoutPreprocessing(expected)) + Expect(source).To(BecomePreflightDocument(expected, WithoutPreprocessing())) }) It("file inclusion with invalid unquoted range - case 1", func() { @@ -1699,7 +1816,7 @@ include::../../test/includes/chapter-a.adoc[] }, }, } - Expect(source).To(BecomePreflightDocumentWithoutPreprocessing(expected)) + Expect(source).To(BecomePreflightDocument(expected, WithoutPreprocessing())) }) It("file inclusion with invalid unquoted range - case 2", func() { @@ -1723,7 +1840,7 @@ include::../../test/includes/chapter-a.adoc[] }, }, } - Expect(source).To(BecomePreflightDocumentWithoutPreprocessing(expected)) + Expect(source).To(BecomePreflightDocument(expected, WithoutPreprocessing())) }) It("file inclusion with invalid unquoted range - case 3", func() { @@ -1743,7 +1860,7 @@ include::../../test/includes/chapter-a.adoc[] }, }, } - Expect(source).To(BecomePreflightDocumentWithoutPreprocessing(expected)) + Expect(source).To(BecomePreflightDocument(expected, WithoutPreprocessing())) }) }) @@ -1768,7 +1885,7 @@ include::../../test/includes/chapter-a.adoc[] }, }, } - Expect(source).To(BecomePreflightDocumentWithoutPreprocessing(expected)) + Expect(source).To(BecomePreflightDocument(expected, WithoutPreprocessing())) }) It("file inclusion with multiple quoted lines", func() { @@ -1790,7 +1907,7 @@ include::../../test/includes/chapter-a.adoc[] }, }, } - Expect(source).To(BecomePreflightDocumentWithoutPreprocessing(expected)) + Expect(source).To(BecomePreflightDocument(expected, WithoutPreprocessing())) }) It("file inclusion with multiple quoted ranges", func() { @@ -1814,7 +1931,7 @@ include::../../test/includes/chapter-a.adoc[] }, }, } - Expect(source).To(BecomePreflightDocumentWithoutPreprocessing(expected)) + Expect(source).To(BecomePreflightDocument(expected, WithoutPreprocessing())) }) It("file inclusion with invalid quoted range - case 1", func() { @@ -1836,7 +1953,7 @@ include::../../test/includes/chapter-a.adoc[] }, }, } - Expect(source).To(BecomePreflightDocumentWithoutPreprocessing(expected)) + Expect(source).To(BecomePreflightDocument(expected, WithoutPreprocessing())) }) It("file inclusion with invalid quoted range - case 2", func() { @@ -1856,7 +1973,7 @@ include::../../test/includes/chapter-a.adoc[] }, }, } - Expect(source).To(BecomePreflightDocumentWithoutPreprocessing(expected)) + Expect(source).To(BecomePreflightDocument(expected, WithoutPreprocessing())) }) }) @@ -1884,7 +2001,7 @@ include::../../test/includes/chapter-a.adoc[] }, }, } - Expect(source).To(BecomePreflightDocumentWithoutPreprocessing(expected)) + Expect(source).To(BecomePreflightDocument(expected, WithoutPreprocessing())) }) It("file inclusion with multiple tags", func() { @@ -1913,7 +2030,7 @@ include::../../test/includes/chapter-a.adoc[] }, }, } - Expect(source).To(BecomePreflightDocumentWithoutPreprocessing(expected)) + Expect(source).To(BecomePreflightDocument(expected, WithoutPreprocessing())) }) }) diff --git a/pkg/renderer/html5/file_inclusion_test.go b/pkg/renderer/html5/file_inclusion_test.go index 491b4100..1e1a38ea 100644 --- a/pkg/renderer/html5/file_inclusion_test.go +++ b/pkg/renderer/html5/file_inclusion_test.go @@ -10,6 +10,37 @@ import ( var _ = Describe("file inclusions", func() { + It("should include adoc file without leveloffset from local file", func() { + console, reset := ConfigureLogger() + defer reset() + source := "include::../../../test/includes/grandchild-include.adoc[]" + expected := `
+

first line of grandchild

+
+
+

last line of grandchild

+
` + Expect(source).To(RenderHTML5Element(expected, WithFilename("foo.adoc"))) + // verify no error/warning in logs + Expect(console).ToNot(ContainAnyMessageWithLevels(log.ErrorLevel, log.WarnLevel)) + }) + + It("should include adoc file without leveloffset from relative file", func() { + console, reset := ConfigureLogger() + defer reset() + source := "include::../../../../test/includes/grandchild-include.adoc[]" + expected := `
+

first line of grandchild

+
+
+

last line of grandchild

+
` + + Expect(source).To(RenderHTML5Element(expected, WithFilename("tmp/foo.adoc"))) + // verify no error/warning in logs + Expect(console).ToNot(ContainAnyMessageWithLevels(log.ErrorLevel, log.WarnLevel)) + }) + It("include adoc file with leveloffset attribute", func() { source := `= Master Document diff --git a/pkg/types/types_suite_test.go b/pkg/types/types_suite_test.go index d97d551c..d3ecedc8 100644 --- a/pkg/types/types_suite_test.go +++ b/pkg/types/types_suite_test.go @@ -1,12 +1,12 @@ package types_test import ( - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" - "testing" _ "github.com/bytesparadise/libasciidoc/testsupport" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" ) func TestTypes(t *testing.T) { diff --git a/testsupport/comparison.go b/testsupport/comparison.go index 78095e1c..c435a703 100644 --- a/testsupport/comparison.go +++ b/testsupport/comparison.go @@ -27,6 +27,6 @@ func compare(actual interface{}, expected interface{}) comparison { } GinkgoT().Logf("actual:\n%s", c.actual) GinkgoT().Logf("expected:\n%s", c.expected) - GinkgoT().Logf("diff:\n%s", c.diffs) + // GinkgoT().Logf("diff:\n%s", c.diffs) return c } diff --git a/testsupport/console_matcher.go b/testsupport/console_matcher.go index a8b42f4f..a9b92c8e 100644 --- a/testsupport/console_matcher.go +++ b/testsupport/console_matcher.go @@ -18,20 +18,34 @@ import ( // end of the test. func ConfigureLogger() (io.Reader, func()) { fmtr := log.StandardLogger().Formatter - // level := log.StandardLogger().Level - buf := bytes.NewBuffer(nil) - // log.SetLevel(log.WarnLevel) - log.SetOutput(buf) + t := tee{ + buf: bytes.NewBuffer(nil), + out: os.Stdout, + } + log.SetOutput(t) log.SetFormatter(&log.JSONFormatter{ DisableTimestamp: true, }) - return buf, func() { + return t, func() { log.SetOutput(os.Stdout) log.SetFormatter(fmtr) - // log.SetLevel(level) } } +type tee struct { + buf io.ReadWriter + out io.Writer +} + +func (t tee) Write(p []byte) (n int, err error) { + t.out.Write(p) + return t.buf.Write(p) +} + +func (t tee) Read(p []byte) (n int, err error) { + return t.buf.Read(p) +} + // --------------------------- // ContainMessageWithLevel // --------------------------- diff --git a/testsupport/html5_rendering_matcher.go b/testsupport/html5_rendering_matcher.go index 93decbfd..e67b5dc9 100644 --- a/testsupport/html5_rendering_matcher.go +++ b/testsupport/html5_rendering_matcher.go @@ -22,15 +22,29 @@ import ( // -------------------- // RenderHTML5Element a custom matcher to verify that a block renders as the expectation -func RenderHTML5Element(expected string, opts ...renderer.Option) gomegatypes.GomegaMatcher { - return &html5ElementMatcher{ +func RenderHTML5Element(expected string, options ...interface{}) gomegatypes.GomegaMatcher { + m := &html5ElementMatcher{ expected: expected, - opts: opts, + filename: "test.adoc", + opts: []renderer.Option{}, } + for _, o := range options { + if configure, ok := o.(FilenameOption); ok { + configure(m) + } else if opt, ok := o.(renderer.Option); ok { + m.opts = append(m.opts, opt) + } + } + return m +} + +func (m *html5ElementMatcher) setFilename(f string) { + m.filename = f } type html5ElementMatcher struct { opts []renderer.Option + filename string expected string actual string comparison comparison @@ -42,7 +56,7 @@ func (m *html5ElementMatcher) Match(actual interface{}) (success bool, err error return false, errors.Errorf("RenderHTML5Element matcher expects a string (actual: %T)", actual) } r := strings.NewReader(content) - doc, err := parser.ParseDocument("test.adoc", r) + doc, err := parser.ParseDocument(m.filename, r) if err != nil { return false, err } @@ -78,14 +92,25 @@ func (m *html5ElementMatcher) NegatedFailureMessage(_ interface{}) (message stri // -------------------- // RenderHTML5Body a custom matcher to verify that a block renders as the expectation -func RenderHTML5Body(expected string, opts ...renderer.Option) gomegatypes.GomegaMatcher { - return &html5BodyMatcher{ +func RenderHTML5Body(expected string, options ...interface{}) gomegatypes.GomegaMatcher { + m := &html5BodyMatcher{ expected: expected, - opts: opts, + filename: "test.adoc", + } + for _, o := range options { + if configure, ok := o.(FilenameOption); ok { + configure(m) + } } + return m +} + +func (m *html5BodyMatcher) setFilename(f string) { + m.filename = f } type html5BodyMatcher struct { + filename string expected string actual string opts []renderer.Option @@ -98,7 +123,7 @@ func (m *html5BodyMatcher) Match(actual interface{}) (success bool, err error) { } contentReader := strings.NewReader(content) resultWriter := bytes.NewBuffer(nil) - _, err = libasciidoc.ConvertToHTML(context.Background(), contentReader, resultWriter, renderer.IncludeHeaderFooter(false)) + _, err = libasciidoc.ConvertToHTML(context.Background(), m.filename, contentReader, resultWriter, renderer.IncludeHeaderFooter(false)) if err != nil { return false, err } @@ -119,17 +144,27 @@ func (m *html5BodyMatcher) NegatedFailureMessage(_ interface{}) (message string) // -------------------- // RenderHTML5Title a custom matcher to verify that a block renders as the expectation -func RenderHTML5Title(expected interface{}, opts ...renderer.Option) gomegatypes.GomegaMatcher { - return &html5TitleMatcher{ +func RenderHTML5Title(expected interface{}, options ...interface{}) gomegatypes.GomegaMatcher { + m := &html5TitleMatcher{ expected: expected, - opts: opts, + filename: "test.adoc", + } + for _, o := range options { + if configure, ok := o.(FilenameOption); ok { + configure(m) + } } + return m +} + +func (m *html5TitleMatcher) setFilename(f string) { + m.filename = f } type html5TitleMatcher struct { + filename string expected interface{} actual interface{} - opts []renderer.Option } func (m *html5TitleMatcher) Match(actual interface{}) (success bool, err error) { @@ -139,7 +174,7 @@ func (m *html5TitleMatcher) Match(actual interface{}) (success bool, err error) } contentReader := strings.NewReader(content) resultWriter := bytes.NewBuffer(nil) - metadata, err := libasciidoc.ConvertToHTML(context.Background(), contentReader, resultWriter, renderer.IncludeHeaderFooter(false)) + metadata, err := libasciidoc.ConvertToHTML(context.Background(), m.filename, contentReader, resultWriter, renderer.IncludeHeaderFooter(false)) if err != nil { return false, err } @@ -173,17 +208,27 @@ func (m *html5TitleMatcher) NegatedFailureMessage(_ interface{}) (message string // --------------------- // RenderHTML5Document a custom matcher to verify that a block renders as the expectation -func RenderHTML5Document(expected string, opts ...renderer.Option) gomegatypes.GomegaMatcher { - return &html5DocumentMatcher{ +func RenderHTML5Document(expected string, options ...interface{}) gomegatypes.GomegaMatcher { + m := &html5DocumentMatcher{ expected: expected, - opts: opts, + filename: "test.adoc", + } + for _, o := range options { + if configure, ok := o.(FilenameOption); ok { + configure(m) + } } + return m +} + +func (m *html5DocumentMatcher) setFilename(f string) { + m.filename = f } type html5DocumentMatcher struct { + filename string expected string actual string - opts []renderer.Option } func (m *html5DocumentMatcher) Match(actual interface{}) (success bool, err error) { @@ -194,7 +239,7 @@ func (m *html5DocumentMatcher) Match(actual interface{}) (success bool, err erro contentReader := strings.NewReader(content) resultWriter := bytes.NewBuffer(nil) lastUpdated := time.Now() - _, err = libasciidoc.ConvertToHTML(context.Background(), contentReader, resultWriter, renderer.IncludeHeaderFooter(true)) + _, err = libasciidoc.ConvertToHTML(context.Background(), m.filename, contentReader, resultWriter, renderer.IncludeHeaderFooter(true)) if err != nil { return false, err } diff --git a/testsupport/log_init.go b/testsupport/log_init.go index 76b6f999..9b2dc585 100644 --- a/testsupport/log_init.go +++ b/testsupport/log_init.go @@ -12,7 +12,7 @@ func init() { logsupport.Setup() if debugMode() { log.SetLevel(log.DebugLevel) - log.Warn("Running test with logs in DEBUG level") + log.Info("Running test with logs in DEBUG level") } } diff --git a/testsupport/matcher_options.go b/testsupport/matcher_options.go new file mode 100644 index 00000000..83066708 --- /dev/null +++ b/testsupport/matcher_options.go @@ -0,0 +1,25 @@ +package testsupport + +type filenameMatcher interface { + setFilename(string) +} + +// FilenameOption an option to set the name of the file being treated by the matcher +type FilenameOption func(m filenameMatcher) + +// WithFilename configures the filename, which can be absolute or relative +func WithFilename(filename string) FilenameOption { + return func(m filenameMatcher) { + m.setFilename(filename) + } +} + +// BecomePreflightDocumentOption an option to configure the BecomePreflightDocument matcher +type BecomePreflightDocumentOption func(m *preflightDocumentMatcher) + +// WithoutPreprocessing disables document preprocessing +func WithoutPreprocessing() BecomePreflightDocumentOption { + return func(m *preflightDocumentMatcher) { + m.preprocessing = false + } +} diff --git a/testsupport/preflight_document_matcher.go b/testsupport/preflight_document_matcher.go index 6b87b797..cdf5efe3 100644 --- a/testsupport/preflight_document_matcher.go +++ b/testsupport/preflight_document_matcher.go @@ -11,28 +11,34 @@ import ( ) // BecomePreflightDocument a custom matcher to verify that a preflight document matches the expectation -func BecomePreflightDocument(expected interface{}) types.GomegaMatcher { - return &preflightDocumentMatcher{ +func BecomePreflightDocument(expected interface{}, options ...interface{}) types.GomegaMatcher { + m := &preflightDocumentMatcher{ expected: expected, preprocessing: true, + filename: "test.adoc", } -} - -// BecomePreflightDocumentWithoutPreprocessing a custom matcher to verify that a preflight document matches the expectation -func BecomePreflightDocumentWithoutPreprocessing(expected interface{}) types.GomegaMatcher { - return &preflightDocumentMatcher{ - expected: expected, - preprocessing: false, + for _, o := range options { + if configure, ok := o.(BecomePreflightDocumentOption); ok { + configure(m) + } else if configure, ok := o.(FilenameOption); ok { + configure(m) + } } + return m } type preflightDocumentMatcher struct { + filename string preprocessing bool expected interface{} actual interface{} comparison comparison } +func (m *preflightDocumentMatcher) setFilename(f string) { + m.filename = f +} + func (m *preflightDocumentMatcher) Match(actual interface{}) (success bool, err error) { content, ok := actual.(string) if !ok { @@ -40,9 +46,9 @@ func (m *preflightDocumentMatcher) Match(actual interface{}) (success bool, err } r := strings.NewReader(content) if !m.preprocessing { - m.actual, err = parser.ParseReader("", r, parser.Entrypoint("PreflightDocument")) + m.actual, err = parser.ParseReader(m.filename, r, parser.Entrypoint("PreflightDocument")) } else { - m.actual, err = parser.ParsePreflightDocument("test.adoc", r) + m.actual, err = parser.ParsePreflightDocument(m.filename, r) } if err != nil { return false, err diff --git a/testsupport/preflight_document_matcher_test.go b/testsupport/preflight_document_matcher_test.go index d7448588..f91857e4 100644 --- a/testsupport/preflight_document_matcher_test.go +++ b/testsupport/preflight_document_matcher_test.go @@ -70,7 +70,7 @@ var _ = Describe("preflight document assertions", func() { It("should return error when invalid type is input", func() { // given - matcher := testsupport.BecomePreflightDocumentWithoutPreprocessing("") + matcher := testsupport.BecomePreflightDocument("", testsupport.WithoutPreprocessing()) // when _, err := matcher.Match(1) // not a string // then @@ -98,7 +98,7 @@ var _ = Describe("preflight document assertions", func() { It("should match", func() { // given - matcher := testsupport.BecomePreflightDocumentWithoutPreprocessing(expected) + matcher := testsupport.BecomePreflightDocument(expected, testsupport.WithoutPreprocessing()) // when result, err := matcher.Match("hello, world!") // then @@ -108,7 +108,7 @@ var _ = Describe("preflight document assertions", func() { It("should not match", func() { // given - matcher := testsupport.BecomePreflightDocumentWithoutPreprocessing(expected) + matcher := testsupport.BecomePreflightDocument(expected, testsupport.WithoutPreprocessing()) actual := "foo" // when result, err := matcher.Match(actual) diff --git a/testsupport/testsupport_suite_test.go b/testsupport/testsupport_suite_test.go index 5474ac3b..bde73d0e 100644 --- a/testsupport/testsupport_suite_test.go +++ b/testsupport/testsupport_suite_test.go @@ -3,6 +3,8 @@ package testsupport_test import ( "testing" + _ "github.com/bytesparadise/libasciidoc/testsupport" + . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" )