From 29e710836007c8946d636a68b7dfaea6ee44028b Mon Sep 17 00:00:00 2001
From: tjpcc <tjp@ctrl-c.club>
Date: Thu, 7 Sep 2023 15:14:11 -0600
Subject: fix loop exit conditions on extended gophermap parsing

---
 gopher/gophermap/extended.go                       | 23 ++++++-----
 gopher/gophermap/extended_test.go                  | 46 ++++++++++++++++++----
 .../gophermap/testdata/customlist_output.gophermap |  2 +
 gopher/gophermap/testdata/uptime                   |  1 +
 gopher/gophermap/testdata/uptime_output.gophermap  |  3 ++
 5 files changed, 57 insertions(+), 18 deletions(-)
 create mode 100644 gopher/gophermap/testdata/uptime
 create mode 100644 gopher/gophermap/testdata/uptime_output.gophermap

(limited to 'gopher')

diff --git a/gopher/gophermap/extended.go b/gopher/gophermap/extended.go
index 8b04104..e1db9dc 100644
--- a/gopher/gophermap/extended.go
+++ b/gopher/gophermap/extended.go
@@ -33,6 +33,8 @@ func ParseExtended(input io.Reader, location *url.URL) (*ExtendedMapDocument, er
 
 outer:
 	for num := 1; ; num += 1 {
+		var item gopher.MapItem
+		var spl []string
 		line, err := rdr.ReadString('\n')
 		isEOF := errors.Is(err, io.EOF)
 		if err != nil && !isEOF {
@@ -48,41 +50,41 @@ outer:
 					Type:    CommentType,
 					Display: strings.TrimPrefix(line[1:], " "),
 				})
-				continue outer
+				goto next
 			case '!':
 				doc.Lines = append(doc.Lines, gopher.MapItem{
 					Type:    TitleType,
 					Display: line[1:],
 				})
-				continue outer
+				goto next
 			case '-':
 				doc.Lines = append(doc.Lines, gopher.MapItem{
 					Type:     HiddenType,
 					Selector: line[1:],
 				})
-				continue outer
+				goto next
 			case ':':
 				doc.Lines = append(doc.Lines, gopher.MapItem{
 					Type:    ExtensionType,
 					Display: line[1:],
 				})
-				continue outer
+				goto next
 			case '=':
 				doc.Lines = append(doc.Lines, gopher.MapItem{
 					Type:     InclusionType,
 					Selector: line[1:],
 				})
-				continue outer
+				goto next
 			}
 		}
 
 		switch line {
 		case "~":
 			doc.Lines = append(doc.Lines, gopher.MapItem{Type: UserListType})
-			continue outer
+			goto next
 		case "%":
 			doc.Lines = append(doc.Lines, gopher.MapItem{Type: VHostListType})
-			continue outer
+			goto next
 		case ".":
 			doc.Lines = append(doc.Lines, gopher.MapItem{Type: EndDocType})
 			break outer
@@ -99,12 +101,12 @@ outer:
 				Hostname: location.Hostname(),
 				Port:     location.Port(),
 			})
-			continue
+			goto next
 		}
 
-		item := gopher.MapItem{Type: types.Status(line[0])}
+		item = gopher.MapItem{Type: types.Status(line[0])}
 
-		spl := strings.Split(line[1:], "\t")
+		spl = strings.Split(line[1:], "\t")
 		if len(spl) != 4 {
 			return doc, InvalidLine(num)
 		}
@@ -117,6 +119,7 @@ outer:
 		}
 		doc.Lines = append(doc.Lines, item)
 
+	next:
 		if isEOF {
 			break
 		}
diff --git a/gopher/gophermap/extended_test.go b/gopher/gophermap/extended_test.go
index 2d9e9b1..0c8b647 100644
--- a/gopher/gophermap/extended_test.go
+++ b/gopher/gophermap/extended_test.go
@@ -15,18 +15,12 @@ func TestExtendedDoc(t *testing.T) {
 		t.Fatal(err)
 	}
 
-	source := &bytes.Buffer{}
-	_, err = io.Copy(source, file)
-	_ = file.Close()
-	if err != nil {
-		t.Fatal(err)
-	}
-
-	edoc, err := ParseExtended(source, &url.URL{
+	edoc, err := ParseExtended(file, &url.URL{
 		Scheme: "gopher",
 		Host:   "localhost.localdomain:70",
 		Path:   "/customlist.gophermap",
 	})
+	_ = file.Close()
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -59,3 +53,39 @@ func TestExtendedDoc(t *testing.T) {
 		t.Fatal("output mismatch")
 	}
 }
+
+func TestUptimeOutput(t *testing.T) {
+	file, err := os.Open("testdata/uptime")
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	edoc, err := ParseExtended(file, &url.URL{
+		Scheme: "gopher",
+		Host:   "localhost.localdomain:70",
+		Path:   "/customlist.gophermap",
+	})
+	_ = file.Close()
+
+	doc, _, err := edoc.Compatible("testdata", FileSystemSettings{
+		ParseExtended: true,
+	})
+	if err != nil {
+		t.Fatal(err)
+	}
+	got := doc.String()
+
+	file, err = os.Open("testdata/uptime_output.gophermap")
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	expected, err := io.ReadAll(file)
+	_ = file.Close()
+
+	if string(expected) != got {
+		fmt.Printf("expected:\n%s", string(expected))
+		fmt.Printf("got:\n%s", got)
+		t.Fatal("output mismatch")
+	}
+}
diff --git a/gopher/gophermap/testdata/customlist_output.gophermap b/gopher/gophermap/testdata/customlist_output.gophermap
index e5cc99d..0b4e334 100644
--- a/gopher/gophermap/testdata/customlist_output.gophermap
+++ b/gopher/gophermap/testdata/customlist_output.gophermap
@@ -11,4 +11,6 @@ i	/customlist.gophermap	localhost.localdomain	70
 0file4.txt	/file4.txt	localhost.localdomain	70
 1subdir title	/subdir	localhost.localdomain	70
 1subdir2 title	/subdir2	localhost.localdomain	70
+9uptime	/uptime	localhost.localdomain	70
+1uptime_output.gophermap	/uptime_output.gophermap	localhost.localdomain	70
 .
diff --git a/gopher/gophermap/testdata/uptime b/gopher/gophermap/testdata/uptime
new file mode 100644
index 0000000..29ecd64
--- /dev/null
+++ b/gopher/gophermap/testdata/uptime
@@ -0,0 +1 @@
+ 8:44PM  up 110 days, 23:28, 1 user, load averages: 0.38, 0.54, 0.47
diff --git a/gopher/gophermap/testdata/uptime_output.gophermap b/gopher/gophermap/testdata/uptime_output.gophermap
new file mode 100644
index 0000000..b5cf09d
--- /dev/null
+++ b/gopher/gophermap/testdata/uptime_output.gophermap
@@ -0,0 +1,3 @@
+i 8:44PM  up 110 days, 23:28, 1 user, load averages: 0.38, 0.54, 0.47	/customlist.gophermap	localhost.localdomain	70
+i	/customlist.gophermap	localhost.localdomain	70
+.
-- 
cgit v1.2.3