File 0011-unvis-add-some-more-error-tests.patch of Package go-mtree

From bcdb71fb56b0fce2ee21792eccbad627482e6be5 Mon Sep 17 00:00:00 2001
From: Aleksa Sarai <cyphar@cyphar.com>
Date: Sun, 21 Sep 2025 00:55:56 +1000
Subject: [PATCH 11/25] unvis: add some more error tests

These error cases were already handled correctly, but we really should
have tests for them anyway. Now that we have proper error variables
declared we can also test for specific errors as well.

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
---
 pkg/govis/unvis_test.go | 35 ++++++++++++++++++++++++++---------
 1 file changed, 26 insertions(+), 9 deletions(-)

diff --git a/pkg/govis/unvis_test.go b/pkg/govis/unvis_test.go
index 2db2f5792c8d..ec4218ccff81 100644
--- a/pkg/govis/unvis_test.go
+++ b/pkg/govis/unvis_test.go
@@ -19,6 +19,7 @@
 package govis
 
 import (
+	"strconv"
 	"testing"
 
 	"github.com/stretchr/testify/assert"
@@ -26,17 +27,33 @@ import (
 )
 
 func TestUnvisError(t *testing.T) {
-	for _, test := range []string{
+	for _, test := range []struct {
+		input string
+		err   error
+	}{
 		// Octal escape codes allow you to specify invalid ASCII values.
-		"\\777",
-		"\\420\\322\\455",
-		"\\652\\233",
+		{"\\777", errOutsideLatin1},
+		{"\\420\\322\\455", errOutsideLatin1},
+		{"\\652\\233", errOutsideLatin1},
+		// Escapes that end abruptly.
+		{"\\", errEndOfString},
+		{"\\J", errUnknownEscapeChar},
+		{"a bad slash: \\", errEndOfString},
+		{"testing -- \\x", errEndOfString},
+		{"\\xG0 test", strconv.ErrSyntax},
+		{"  abc \\Mx", errUnknownEscapeChar},
+		{"\\Mx", errUnknownEscapeChar},
+		{"\\M-", errEndOfString},
+		{"\\M-\u5000", errOutsideLatin1},
+		{"\\M^", errEndOfString},
+		{"\\^", errEndOfString},
+		{"\\^\u5000", errOutsideLatin1},
+		{"\\M", errEndOfString},
 	} {
-		t.Run(test, func(t *testing.T) {
-			_, err := Unvis(test, DefaultVisFlags)
-			require.Errorf(t, err, "invalid octal escape should give an error")
-			assert.ErrorContains(t, err, "escape base 8")
-			assert.ErrorContains(t, err, "outside latin-1 encoding")
+		t.Run(test.input, func(t *testing.T) {
+			_, err := Unvis(test.input, DefaultVisFlags)
+			require.Errorf(t, err, "invalid escape string should give an error")
+			assert.ErrorIs(t, err, test.err, "unexpected error from invalid escape string")
 		})
 	}
 }
-- 
2.51.0

openSUSE Build Service is sponsored by