From 15e2c62a3e2103fea0b311ac206bbcbf25d656a3 Mon Sep 17 00:00:00 2001 From: Tim Hockin Date: Fri, 25 Sep 2015 13:56:50 -0700 Subject: [PATCH] Add a munger check for files that end in preformat --- cmd/mungedocs/mungedocs.go | 3 +++ cmd/mungedocs/preformatted.go | 10 ++++++++++ cmd/mungedocs/preformatted_test.go | 23 +++++++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/cmd/mungedocs/mungedocs.go b/cmd/mungedocs/mungedocs.go index f9afe89f52..7f98a5d9dd 100644 --- a/cmd/mungedocs/mungedocs.go +++ b/cmd/mungedocs/mungedocs.go @@ -46,6 +46,9 @@ Examples: // All of the munge operations to perform. // TODO: allow selection from command line. (e.g., just check links in the examples directory.) allMunges = []munge{ + // Simple "check something" functions must run first. + {"preformat-balance", checkPreformatBalance}, + // Functions which modify state. {"remove-whitespace", updateWhitespace}, {"table-of-contents", updateTOC}, {"unversioned-warning", updateUnversionedWarning}, diff --git a/cmd/mungedocs/preformatted.go b/cmd/mungedocs/preformatted.go index f3864278ee..16955b7017 100644 --- a/cmd/mungedocs/preformatted.go +++ b/cmd/mungedocs/preformatted.go @@ -16,6 +16,8 @@ limitations under the License. package main +import "fmt" + // Blocks of ``` need to have blank lines on both sides or they don't look // right in HTML. func updatePreformatted(filePath string, mlines mungeLines) (mungeLines, error) { @@ -39,3 +41,11 @@ func updatePreformatted(filePath string, mlines mungeLines) (mungeLines, error) } return out, nil } + +// If the file ends on a preformatted line, there must have been an imbalance. +func checkPreformatBalance(filePath string, mlines mungeLines) (mungeLines, error) { + if len(mlines) > 0 && mlines[len(mlines)-1].preformatted { + return nil, fmt.Errorf("file ends in preformatted block") + } + return mlines, nil +} diff --git a/cmd/mungedocs/preformatted_test.go b/cmd/mungedocs/preformatted_test.go index 205a89e3ad..9cedb9701b 100644 --- a/cmd/mungedocs/preformatted_test.go +++ b/cmd/mungedocs/preformatted_test.go @@ -55,3 +55,26 @@ func TestPreformatted(t *testing.T) { } } } + +func TestPreformattedImbalance(t *testing.T) { + var cases = []struct { + in string + ok bool + }{ + {"", true}, + {"```\nin\n```", true}, + {"```\nin\n```\nout", true}, + {"```", false}, + {"```\nin\n```\nout\n```", false}, + } + for i, c := range cases { + in := getMungeLines(c.in) + _, err := checkPreformatBalance("filename.md", in) + if err != nil && c.ok { + t.Errorf("case[%d]: expected success", i) + } + if err == nil && !c.ok { + t.Errorf("case[%d]: expected failure", i) + } + } +}