Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not delete text that fails preformat-balance #19280

Merged
merged 1 commit into from
Jan 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions cmd/mungedocs/mungedocs.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,12 @@ func (f fileProcessor) visit(path string) error {
}
fmt.Printf("%s:\n", munge.name)
if *verbose {
fmt.Printf("INPUT: <<<%v>>>\n", mungeLines)
fmt.Printf("MUNGED: <<<%v>>>\n", after)
if len(mungeLines) <= 20 {
fmt.Printf("INPUT: <<<%v>>>\n", mungeLines)
fmt.Printf("MUNGED: <<<%v>>>\n", after)
} else {
fmt.Printf("not printing failed chunk: too many lines\n")
}
}
if err != nil {
fmt.Println(err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/mungedocs/preformatted.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func updatePreformatted(filePath string, mlines mungeLines) (mungeLines, error)
// 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, fmt.Errorf("unbalanced triple backtick delimiters")
}
return mlines, nil
}
19 changes: 18 additions & 1 deletion cmd/mungedocs/preformatted_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,29 @@ func TestPreformattedImbalance(t *testing.T) {
}
for i, c := range cases {
in := getMungeLines(c.in)
_, err := checkPreformatBalance("filename.md", in)
out, 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)
}
// Even in case of misformat, return all the text,
// so that the user's work is not lost.
if !equalMungeLines(out, in) {
t.Errorf("case[%d]: expected munged text to be identical to input text", i)
}
}
}

func equalMungeLines(a, b mungeLines) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}