hugo

Unnamed repository; edit this file 'description' to name the repository.

git clone git://git.shimmy1996.com/hugo.git
commit 6a5acd753abbd899547c89b369de71df639cce19
parent 805b21555e283c28a669943a36af6d6d699e9198
Author: Cameron Moore <moorereason@gmail.com>
Date:   Wed, 25 May 2022 21:14:37 -0500

metrics: Fix divide by zero error

Under certain conditions, `howSimilarString` could reach a divide-by-
zero situation which causes bogus values to print in the cache potential
column of the template hints output.  This situation essentially causes
a `int(math.NaN())` value to be returned and hilarity ensues thereafter.

Diffstat:
Mmetrics/metrics.go | 4++++
Mmetrics/metrics_test.go | 1+
2 files changed, 5 insertions(+), 0 deletions(-)
diff --git a/metrics/metrics.go b/metrics/metrics.go
@@ -281,6 +281,10 @@ func howSimilarStrings(a, b string) int {
 		}
 	}
 
+	if common == 0 && common == len(af) {
+		return 100
+	}
+
 	return int(math.Floor((float64(common) / float64(len(af)) * 100)))
 }
 
diff --git a/metrics/metrics_test.go b/metrics/metrics_test.go
@@ -43,6 +43,7 @@ func TestSimilarPercentage(t *testing.T) {
 	c.Assert(howSimilar(template.HTML("Hugo Rules"), template.HTML("Hugo Rules")), qt.Equals, 100)
 	c.Assert(howSimilar(map[string]any{"a": 32, "b": 33}, map[string]any{"a": 32, "b": 33}), qt.Equals, 100)
 	c.Assert(howSimilar(map[string]any{"a": 32, "b": 33}, map[string]any{"a": 32, "b": 34}), qt.Equals, 0)
+	c.Assert(howSimilar("\n", ""), qt.Equals, 100)
 }
 
 type testStruct struct {