testmark-go

halt parsing if code block not found

commit aefd8a6e14c46062986f790582f02f7625221201 · parent 5c5ae707f1 · browse this tree
Calvin Behling, 2022-12-01 21:08:49 UTC

diff --git a/read.go b/read.go
index f92561f..77658dc 100644
--- a/read.go
+++ b/read.go
@@ -77,16 +77,13 @@ func Parse(data []byte) (*Document, error) {
 			inCodeBlock = !inCodeBlock
 			goto next
 		}
-		// If we're in a code block, just fly by.
 		if inCodeBlock {
+			// If we're in a code block, just fly by.
 			goto next
 		}
-		// If we were expecting a code block just now, we didn't get it.
 		if expectCodeBlock {
-			// ... Log a complaint?  I don't think halting with a parse error would be helpful.
-			// But definitely don't wait around for arbitrarily distant code blocks.
-			expectCodeBlock = false
-			hunkInProgress = DocHunk{LineStart: -1}
+			// If we were expecting a code block just now, we didn't get it.
+			return &doc, fmt.Errorf("invalid markdown comment on line %d. Missing code block for hunk %s", i+1, hunkInProgress.Name)
 		}
 		// Look for testmark block indicators.
 		if bytes.HasPrefix(line, sigilTestmark) {
diff --git a/read_test.go b/read_test.go
index a6ab320..d12a758 100644
--- a/read_test.go
+++ b/read_test.go
@@ -91,9 +91,7 @@ func TestParseTrailingExtraLineBreak(t *testing.T) {
 	fmt.Fprintln(buf, "foo")
 	fmt.Fprintln(buf, "```")
 	doc, err := testmark.Parse(buf.Bytes())
-	if err != nil {
-		t.Fatal(err)
-	}
+	assert(t, err.Error(), `invalid markdown comment on line 2. Missing code block for hunk extra/newline`)
 	hunk, exists := doc.HunksByName["extra/newline"]
 	if exists {
 		t.Errorf("testmark should ignore these")