From 04650ce778b87daf9c48746d753de1533cda5020 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Wed, 5 Nov 2025 20:41:17 +0100 Subject: [PATCH] all: Run modernize -fix ./... Closes #14107 --- cache/httpcache/httpcache_test.go | 2 +- commands/server.go | 1 - common/hashing/hashing_test.go | 19 +++++------ common/hreflect/convert_test.go | 2 +- common/hreflect/helpers_test.go | 15 ++++----- common/hstore/scratch_test.go | 4 +-- common/hstrings/strings_test.go | 14 ++++---- common/hsync/oncemore_test.go | 2 +- common/htime/time_test.go | 6 ++-- common/maps/cache_test.go | 2 +- common/maps/orderedintset_test.go | 14 ++++---- common/para/para_test.go | 1 - common/paths/path.go | 4 +-- common/paths/path_test.go | 10 +++--- common/paths/pathparser_test.go | 4 +-- common/predicate/predicate_test.go | 8 ++--- common/text/transform_test.go | 2 +- compare/compare_strings_test.go | 4 +-- config/allconfig/load.go | 4 +-- config/allconfig/load_test.go | 2 +- config/defaultConfigProvider_test.go | 3 +- create/content_test.go | 1 - helpers/content_test.go | 8 ++--- helpers/general_test.go | 4 +-- helpers/path.go | 8 ++--- helpers/url_test.go | 8 ++--- hugofs/hashing_fs_test.go | 4 +-- hugofs/hglob/glob_test.go | 4 +-- hugofs/walk_test.go | 3 +- hugolib/cascade_test.go | 6 ++-- hugolib/config_test.go | 1 - hugolib/content_map_test.go | 1 - hugolib/content_render_hooks_test.go | 1 - hugolib/doctree/nodeshiftree_test.go | 7 ++-- hugolib/doctree/treeshifttree_test.go | 4 +-- hugolib/filesystems/basefs.go | 4 +-- hugolib/hugo_sites.go | 4 +-- hugolib/hugo_sites_build.go | 2 -- hugolib/hugo_smoke_test.go | 5 ++- hugolib/integrationtest_builder.go | 8 ++--- hugolib/page_permalink_test.go | 1 - hugolib/page_test.go | 1 - hugolib/pagecollections_test.go | 13 ++++---- hugolib/pages_language_merge_test.go | 2 +- hugolib/pages_test.go | 4 +-- hugolib/pagesfromdata/pagesfromgotmpl_test.go | 2 +- hugolib/rebuild_test.go | 5 ++- hugolib/resource_chain_test.go | 3 +- hugolib/segments/segments_test.go | 3 +- hugolib/shortcode.go | 4 +-- hugolib/shortcode_test.go | 12 ++----- hugolib/site.go | 2 +- hugolib/site_benchmark_new_test.go | 4 +-- hugolib/site_output_test.go | 1 - hugolib/site_sections_test.go | 1 - hugolib/sitesmatrix/dimensions_test.go | 6 ++-- .../sitematrix_integration_test.go | 4 +-- hugolib/sitesmatrix/vectorstores_test.go | 32 +++++++++---------- hugolib/taxonomy_test.go | 2 +- hugolib/template_test.go | 1 - hugolib/testhelpers_test.go | 4 +-- identity/finder_test.go | 4 +-- identity/identity_test.go | 18 +++++------ internal/js/esbuild/resolve.go | 4 +-- internal/warpc/warpc.go | 1 - internal/warpc/warpc_test.go | 8 ++--- langs/i18n/i18n_test.go | 2 +- langs/language_test.go | 2 +- markup/goldmark/autoid_test.go | 17 +++++----- .../codeblocks/codeblocks_integration_test.go | 1 - markup/goldmark/goldmark_integration_test.go | 6 ++-- markup/goldmark/hugocontext/hugocontext.go | 4 +-- .../goldmark/hugocontext/hugocontext_test.go | 2 +- markup/highlight/config.go | 6 ++-- markup/internal/external.go | 2 +- .../tableofcontents/tableofcontents_test.go | 6 ++-- media/mediaType_test.go | 2 +- metrics/metrics_test.go | 3 +- modules/collect.go | 8 ++--- parser/metadecoders/decoder_test.go | 6 ++-- .../pageparser/pageparser_shortcode_test.go | 4 +-- parser/pageparser/pageparser_test.go | 7 ++-- publisher/htmlElementsCollector_test.go | 4 +-- related/inverted_index_test.go | 7 ++-- related/related_integration_test.go | 4 +-- resources/image_test.go | 10 +++--- resources/images/color_test.go | 2 -- resources/images/exif/exif_test.go | 3 +- resources/images/text.go | 4 +-- resources/page/page_markup_test.go | 12 +++---- resources/page/page_paths_test.go | 4 +-- resources/page/pages_cache_test.go | 3 +- resources/page/pages_sort_search_test.go | 2 +- resources/page/pages_sort_test.go | 3 +- resources/page/permalinks_test.go | 3 +- resources/resource_test.go | 3 +- .../cssjs/inline_imports_test.go | 2 +- source/content_directory_test.go | 1 - tpl/collections/collections_test.go | 3 +- tpl/collections/merge_test.go | 3 +- tpl/collections/querify_test.go | 9 ++---- tpl/collections/sort_test.go | 2 +- tpl/collections/vitter_test.go | 4 +-- tpl/collections/where_test.go | 12 +++---- tpl/hash/hash_test.go | 2 +- .../go_templates/htmltemplate/exec_test.go | 12 +++---- tpl/internal/go_templates/testenv/testenv.go | 4 +-- .../go_templates/testenv/testenv_test.go | 4 +-- .../go_templates/texttemplate/exec_test.go | 8 ++--- .../go_templates/texttemplate/funcs.go | 5 ++- .../texttemplate/hugo_template.go | 2 +- .../go_templates/texttemplate/parse/parse.go | 7 ++-- tpl/partials/partials_integration_test.go | 4 +-- tpl/strings/strings.go | 2 +- tpl/strings/truncate_test.go | 4 +-- tpl/time/time_test.go | 4 +-- tpl/tplimpl/templatedescriptor_test.go | 3 +- tpl/tplimpl/templatestore_integration_test.go | 16 ++++------ tpl/transform/unmarshal_test.go | 6 ++-- .../livereloadinject/livereloadinject_test.go | 3 +- transform/urlreplacers/absurlreplacer_test.go | 12 +++---- watcher/filenotify/poller_test.go | 2 +- 122 files changed, 275 insertions(+), 351 deletions(-) diff --git a/cache/httpcache/httpcache_test.go b/cache/httpcache/httpcache_test.go index 60c07d056..737dff239 100644 --- a/cache/httpcache/httpcache_test.go +++ b/cache/httpcache/httpcache_test.go @@ -52,7 +52,7 @@ func TestDefaultConfig(t *testing.T) { func TestDecodeConfigInjectsDefaultAndCompiles(t *testing.T) { c := qt.New(t) - cfg, err := DecodeConfig(config.BaseConfig{}, map[string]interface{}{}) + cfg, err := DecodeConfig(config.BaseConfig{}, map[string]any{}) c.Assert(err, qt.IsNil) c.Assert(cfg, qt.DeepEquals, DefaultConfig) diff --git a/commands/server.go b/commands/server.go index 82b18c608..db82defc9 100644 --- a/commands/server.go +++ b/commands/server.go @@ -1035,7 +1035,6 @@ func (c *serverCommand) serve() error { defer cancel() wg2, ctx := errgroup.WithContext(ctx) for _, srv := range servers { - srv := srv wg2.Go(func() error { return srv.Shutdown(ctx) }) diff --git a/common/hashing/hashing_test.go b/common/hashing/hashing_test.go index 105b6d8b5..8a165786a 100644 --- a/common/hashing/hashing_test.go +++ b/common/hashing/hashing_test.go @@ -38,7 +38,6 @@ func TestXxHashFromReaderPara(t *testing.T) { var wg sync.WaitGroup for i := range 10 { - i := i wg.Add(1) go func() { defer wg.Done() @@ -75,8 +74,8 @@ func TestXxHashFromStringHexEncoded(t *testing.T) { func BenchmarkXXHashFromReader(b *testing.B) { r := strings.NewReader("Hello World") - b.ResetTimer() - for i := 0; i < b.N; i++ { + + for b.Loop() { XXHashFromReader(r) r.Seek(0, 0) } @@ -84,16 +83,16 @@ func BenchmarkXXHashFromReader(b *testing.B) { func BenchmarkXXHashFromString(b *testing.B) { s := "Hello World" - b.ResetTimer() - for i := 0; i < b.N; i++ { + + for b.Loop() { XXHashFromString(s) } } func BenchmarkXXHashFromStringHexEncoded(b *testing.B) { s := "The quick brown fox jumps over the lazy dog" - b.ResetTimer() - for i := 0; i < b.N; i++ { + + for b.Loop() { XxHashFromStringHexEncoded(s) } } @@ -136,7 +135,7 @@ func BenchmarkHashString(b *testing.B) { for _, test := range tests { b.Run(fmt.Sprintf("n%d", len(test)), func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { HashString(test) } }) @@ -149,9 +148,7 @@ func BenchmarkHashMap(b *testing.B) { m[fmt.Sprintf("key%d", i)] = i } - b.ResetTimer() - - for i := 0; i < b.N; i++ { + for b.Loop() { HashString(m) } } diff --git a/common/hreflect/convert_test.go b/common/hreflect/convert_test.go index a130c4f1f..c64526ef6 100644 --- a/common/hreflect/convert_test.go +++ b/common/hreflect/convert_test.go @@ -278,7 +278,7 @@ func TestConvertIfPossibleMisc(t *testing.T) { func BenchmarkToInt64(b *testing.B) { v := reflect.ValueOf(int(42)) - for i := 0; i < b.N; i++ { + for b.Loop() { ToInt64(v) } } diff --git a/common/hreflect/helpers_test.go b/common/hreflect/helpers_test.go index 6a6e7b26b..9527fbb7d 100644 --- a/common/hreflect/helpers_test.go +++ b/common/hreflect/helpers_test.go @@ -156,12 +156,12 @@ func BenchmarkIsContextType(b *testing.B) { b.Run("value", func(b *testing.B) { ctx := context.Background() ctxs := make([]reflect.Type, b.N) - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { ctxs[i] = reflect.TypeOf(context.WithValue(ctx, k("key"), i)) } b.ResetTimer() - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { if !IsContextType(ctxs[i]) { b.Fatal("not context") } @@ -170,7 +170,7 @@ func BenchmarkIsContextType(b *testing.B) { b.Run("background", func(b *testing.B) { var ctxt reflect.Type = reflect.TypeOf(context.Background()) - for i := 0; i < b.N; i++ { + for b.Loop() { if !IsContextType(ctxt) { b.Fatal("not context") } @@ -189,8 +189,7 @@ func BenchmarkIsTruthFulValue(b *testing.B) { nilPointer = reflect.ValueOf((*zeroStruct)(nil)) ) - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { IsTruthfulValue(stringHugo) IsTruthfulValue(stringEmpty) IsTruthfulValue(zero) @@ -227,8 +226,7 @@ func BenchmarkGetMethodByNameForType(b *testing.B) { tp := reflect.TypeFor[*testStruct]() methods := []string{"Method1", "Method2", "Method3", "Method4", "Method5"} - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { for _, method := range methods { _ = GetMethodByNameForType(tp, method) } @@ -239,8 +237,7 @@ func BenchmarkGetMethodByName(b *testing.B) { v := reflect.ValueOf(&testStruct{}) methods := []string{"Method1", "Method2", "Method3", "Method4", "Method5"} - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { for _, method := range methods { _ = GetMethodByName(v, method) } diff --git a/common/hstore/scratch_test.go b/common/hstore/scratch_test.go index b7253ab2a..1f08d46a9 100644 --- a/common/hstore/scratch_test.go +++ b/common/hstore/scratch_test.go @@ -214,8 +214,8 @@ func TestScratchGetSortedMapValues(t *testing.T) { func BenchmarkScratchGet(b *testing.B) { scratch := NewScratch() scratch.Add("A", 1) - b.ResetTimer() - for i := 0; i < b.N; i++ { + + for b.Loop() { scratch.Get("A") } } diff --git a/common/hstrings/strings_test.go b/common/hstrings/strings_test.go index 8c485130e..205005785 100644 --- a/common/hstrings/strings_test.go +++ b/common/hstrings/strings_test.go @@ -75,7 +75,7 @@ func BenchmarkUniqueStrings(b *testing.B) { input := []string{"a", "b", "d", "e", "d", "h", "a", "i"} b.Run("Safe", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { result := UniqueStrings(input) if len(result) != 6 { b.Fatalf("invalid count: %d", len(result)) @@ -86,13 +86,13 @@ func BenchmarkUniqueStrings(b *testing.B) { b.Run("Reuse slice", func(b *testing.B) { b.StopTimer() inputs := make([][]string, b.N) - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { inputc := make([]string, len(input)) copy(inputc, input) inputs[i] = inputc } b.StartTimer() - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { inputc := inputs[i] result := UniqueStringsReuse(inputc) @@ -105,13 +105,13 @@ func BenchmarkUniqueStrings(b *testing.B) { b.Run("Reuse slice sorted", func(b *testing.B) { b.StopTimer() inputs := make([][]string, b.N) - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { inputc := make([]string, len(input)) copy(inputc, input) inputs[i] = inputc } b.StartTimer() - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { inputc := inputs[i] result := UniqueStringsSorted(inputc) @@ -123,13 +123,13 @@ func BenchmarkUniqueStrings(b *testing.B) { } func BenchmarkGetOrCompileRegexp(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { GetOrCompileRegexp(`\d+`) } } func BenchmarkCompileRegexp(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { regexp.MustCompile(`\d+`) } } diff --git a/common/hsync/oncemore_test.go b/common/hsync/oncemore_test.go index e0b60418b..67a6e04d1 100644 --- a/common/hsync/oncemore_test.go +++ b/common/hsync/oncemore_test.go @@ -67,7 +67,7 @@ func BenchmarkOnceMoreValue(b *testing.B) { return counter } - for range b.N { + for b.Loop() { omf := OnceMoreValue(f) for range 10 { omf.Value(context.Background()) diff --git a/common/htime/time_test.go b/common/htime/time_test.go index 78954887e..a84d1c3bb 100644 --- a/common/htime/time_test.go +++ b/common/htime/time_test.go @@ -112,7 +112,7 @@ func BenchmarkTimeFormatter(b *testing.B) { june06, _ := time.Parse("2006-Jan-02", "2018-Jun-06") b.Run("Native", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { got := june06.Format("Monday Jan 2 2006") if got != "Wednesday Jun 6 2018" { b.Fatalf("invalid format, got %q", got) @@ -123,7 +123,7 @@ func BenchmarkTimeFormatter(b *testing.B) { b.Run("Localized", func(b *testing.B) { f := NewTimeFormatter(translators.GetTranslator("nn")) b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { got := f.Format(june06, "Monday Jan 2 2006") if got != "onsdag juni 6 2018" { b.Fatalf("invalid format, got %q", got) @@ -134,7 +134,7 @@ func BenchmarkTimeFormatter(b *testing.B) { b.Run("Localized Custom", func(b *testing.B) { f := NewTimeFormatter(translators.GetTranslator("nn")) b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { got := f.Format(june06, ":date_medium") if got != "6. juni 2018" { b.Fatalf("invalid format, got %q", got) diff --git a/common/maps/cache_test.go b/common/maps/cache_test.go index 17e38ace8..32bff9572 100644 --- a/common/maps/cache_test.go +++ b/common/maps/cache_test.go @@ -24,7 +24,7 @@ func TestCacheSize(t *testing.T) { cache := NewCacheWithOptions[string, string](CacheOptions{Size: 10}) - for i := 0; i < 30; i++ { + for i := range 30 { cache.Set(string(rune('a'+i)), "value") } diff --git a/common/maps/orderedintset_test.go b/common/maps/orderedintset_test.go index 243d0d617..2aab63028 100644 --- a/common/maps/orderedintset_test.go +++ b/common/maps/orderedintset_test.go @@ -68,31 +68,31 @@ func BenchmarkOrderedIntSet(b *testing.B) { } b.Run("New", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { NewOrderedIntSet(1, 2, 3, 4, 5, 6, 7, 8) } }) b.Run("Has small", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { smallSet.Has(i % 32) } }) b.Run("Has medium", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { mediumSet.Has(i % 32) } }) b.Run("Next", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { mediumSet.Next(i % 32) } }) b.Run("ForEachKey small", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { smallSet.ForEachKey(func(key int) bool { return true }) @@ -100,7 +100,7 @@ func BenchmarkOrderedIntSet(b *testing.B) { }) b.Run("ForEachKey medium", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { mediumSet.ForEachKey(func(key int) bool { return true }) @@ -108,7 +108,7 @@ func BenchmarkOrderedIntSet(b *testing.B) { }) b.Run("ForEachKey large", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { largeSet.ForEachKey(func(key int) bool { return true }) diff --git a/common/para/para_test.go b/common/para/para_test.go index cf24a4e37..3d28a017d 100644 --- a/common/para/para_test.go +++ b/common/para/para_test.go @@ -52,7 +52,6 @@ func TestPara(t *testing.T) { var result []int var mu sync.Mutex for i := range n { - i := i r.Run(func() error { mu.Lock() defer mu.Unlock() diff --git a/common/paths/path.go b/common/paths/path.go index de91d6a2f..e9b9108f5 100644 --- a/common/paths/path.go +++ b/common/paths/path.go @@ -115,8 +115,8 @@ func ReplaceExtension(path string, newExt string) string { func makePathRelative(inPath string, possibleDirectories ...string) (string, error) { for _, currentPath := range possibleDirectories { - if strings.HasPrefix(inPath, currentPath) { - return strings.TrimPrefix(inPath, currentPath), nil + if after, ok := strings.CutPrefix(inPath, currentPath); ok { + return after, nil } } return inPath, errors.New("can't extract relative path, unknown prefix") diff --git a/common/paths/path_test.go b/common/paths/path_test.go index 0e0dfc2e2..ebfd5acd7 100644 --- a/common/paths/path_test.go +++ b/common/paths/path_test.go @@ -225,7 +225,7 @@ func BenchmarkSanitize(b *testing.B) { // This should not allocate any memory. b.Run("All allowed", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { got := Sanitize(allAlowedPath) if got != allAlowedPath { b.Fatal(got) @@ -235,7 +235,7 @@ func BenchmarkSanitize(b *testing.B) { // This will allocate some memory. b.Run("Spaces", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { got := Sanitize(spacePath) if got != "foo-bar" { b.Fatal(got) @@ -320,7 +320,7 @@ func BenchmarkAddLeadingSlash(b *testing.B) { // This should not allocate any memory. b.Run("With leading slash", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { got := AddLeadingSlash(withLeadingSlash) if got != withLeadingSlash { b.Fatal(got) @@ -330,7 +330,7 @@ func BenchmarkAddLeadingSlash(b *testing.B) { // This will allocate some memory. b.Run("Without leading slash", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { got := AddLeadingSlash(noLeadingSlash) if got != "/a/b/c" { b.Fatal(got) @@ -339,7 +339,7 @@ func BenchmarkAddLeadingSlash(b *testing.B) { }) b.Run("Blank string", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { got := AddLeadingSlash("") if got != "/" { b.Fatal(got) diff --git a/common/paths/pathparser_test.go b/common/paths/pathparser_test.go index d7286d903..843b286c2 100644 --- a/common/paths/pathparser_test.go +++ b/common/paths/pathparser_test.go @@ -637,7 +637,7 @@ func TestHasExt(t *testing.T) { func BenchmarkParseIdentity(b *testing.B) { parser := newTestParser() - for i := 0; i < b.N; i++ { + for b.Loop() { parser.ParseIdentity(files.ComponentFolderAssets, "/a/b.css") } } @@ -656,7 +656,7 @@ func TestSitesMatrixFromPath(t *testing.T) { func BenchmarkSitesMatrixFromPath(b *testing.B) { parser := newTestParser() p := parser.Parse(files.ComponentFolderContent, "/a/b/c.fr.md") - for i := 0; i < b.N; i++ { + for b.Loop() { parser.SitesMatrixFromPath(p) } } diff --git a/common/predicate/predicate_test.go b/common/predicate/predicate_test.go index 5a9c8365b..7cfc11d11 100644 --- a/common/predicate/predicate_test.go +++ b/common/predicate/predicate_test.go @@ -150,7 +150,7 @@ func BenchmarkPredicate(b *testing.B) { b.Run("and or no match", func(b *testing.B) { var p predicate.PR[int] = intP1 p = p.And(intP2).Or(intP3) - for i := 0; i < b.N; i++ { + for b.Loop() { _ = p(3).OK() } }) @@ -158,7 +158,7 @@ func BenchmarkPredicate(b *testing.B) { b.Run("and and no match", func(b *testing.B) { var p predicate.PR[int] = intP1 p = p.And(intP2) - for range b.N { + for b.Loop() { _ = p(3).OK() } }) @@ -166,7 +166,7 @@ func BenchmarkPredicate(b *testing.B) { b.Run("and and match", func(b *testing.B) { var p predicate.PR[int] = intP1 p = p.And(intP2) - for range b.N { + for b.Loop() { _ = p(10).OK() } }) @@ -174,7 +174,7 @@ func BenchmarkPredicate(b *testing.B) { b.Run("or or match", func(b *testing.B) { var p predicate.PR[int] = intP1 p = p.Or(intP2).Or(intP3) - for range b.N { + for b.Loop() { _ = p(2).OK() } }) diff --git a/common/text/transform_test.go b/common/text/transform_test.go index 74bb37783..e5ed3567f 100644 --- a/common/text/transform_test.go +++ b/common/text/transform_test.go @@ -65,7 +65,7 @@ func BenchmarkVisitLinesAfter(b *testing.B) { line 3` - for i := 0; i < b.N; i++ { + for b.Loop() { VisitLinesAfter(lines, func(s string) { }) } diff --git a/compare/compare_strings_test.go b/compare/compare_strings_test.go index 1a5bb0b1a..505eaa7a3 100644 --- a/compare/compare_strings_test.go +++ b/compare/compare_strings_test.go @@ -67,12 +67,12 @@ func BenchmarkStringSort(b *testing.B) { prototype := []string{"b", "Bz", "zz", "ba", "αβδ αβδ αβδ", "A", "Ba", "ba", "nnnnasdfnnn", "AAgæåz", "αβδC"} b.Run("LessStrings", func(b *testing.B) { ss := make([][]string, b.N) - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { ss[i] = make([]string, len(prototype)) copy(ss[i], prototype) } b.ResetTimer() - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { sss := ss[i] sort.Slice(sss, func(i, j int) bool { return LessStrings(sss[i], sss[j]) diff --git a/config/allconfig/load.go b/config/allconfig/load.go index 4527a4554..ec6b71e05 100644 --- a/config/allconfig/load.go +++ b/config/allconfig/load.go @@ -220,8 +220,8 @@ func (l configLoader) applyOsEnvOverrides(environ []string) error { var hugoEnv []types.KeyValueStr for _, v := range environ { key, val := config.SplitEnvVar(v) - if strings.HasPrefix(key, hugoEnvPrefix) { - delimiterAndKey := strings.TrimPrefix(key, hugoEnvPrefix) + if after, ok := strings.CutPrefix(key, hugoEnvPrefix); ok { + delimiterAndKey := after if len(delimiterAndKey) < 2 { continue } diff --git a/config/allconfig/load_test.go b/config/allconfig/load_test.go index 3c16e71e9..10a88d7b7 100644 --- a/config/allconfig/load_test.go +++ b/config/allconfig/load_test.go @@ -58,7 +58,7 @@ weight = 4 Filename: configFilename, } - for i := 0; i < b.N; i++ { + for b.Loop() { _, err := LoadConfig(d) if err != nil { b.Fatal(err) diff --git a/config/defaultConfigProvider_test.go b/config/defaultConfigProvider_test.go index 204861216..a21d573ed 100644 --- a/config/defaultConfigProvider_test.go +++ b/config/defaultConfigProvider_test.go @@ -334,7 +334,6 @@ func TestDefaultConfigProvider(t *testing.T) { } for i := range 20 { - i := i r.Run(func() error { const v = 42 k := fmt.Sprintf("k%d", i) @@ -478,7 +477,7 @@ func BenchmarkDefaultConfigProvider(b *testing.B) { b.Run("Custom", func(b *testing.B) { cfg := New() - for i := 0; i < b.N; i++ { + for b.Loop() { runMethods(b, cfg) } }) diff --git a/create/content_test.go b/create/content_test.go index 22629fdbc..deafaed09 100644 --- a/create/content_test.go +++ b/create/content_test.go @@ -74,7 +74,6 @@ func TestNewContentFromFile(t *testing.T) { c := qt.New(t) for i, cas := range cases { - cas := cas c.Run(cas.name, func(c *qt.C) { c.Parallel() diff --git a/helpers/content_test.go b/helpers/content_test.go index dd7c5a4c5..e2c755a7b 100644 --- a/helpers/content_test.go +++ b/helpers/content_test.go @@ -54,8 +54,8 @@ func TestTrimShortHTML(t *testing.T) { func BenchmarkTrimShortHTML(b *testing.B) { c := newTestContentSpec(nil) - b.ResetTimer() - for i := 0; i < b.N; i++ { + + for b.Loop() { c.TrimShortHTML([]byte("

Simple paragraph

"), "markdown") } } @@ -134,8 +134,8 @@ func TestTotalWords(t *testing.T) { } func BenchmarkTotalWords(b *testing.B) { - b.ResetTimer() - for i := 0; i < b.N; i++ { + + for b.Loop() { wordCount := helpers.TotalWords(totalWordsBenchmarkString) if wordCount != 400 { b.Fatal("Wordcount error") diff --git a/helpers/general_test.go b/helpers/general_test.go index f7bcb6642..53c1925d4 100644 --- a/helpers/general_test.go +++ b/helpers/general_test.go @@ -214,8 +214,8 @@ func TestGetTitleFunc(t *testing.T) { } func BenchmarkReaderContains(b *testing.B) { - b.ResetTimer() - for i := 0; i < b.N; i++ { + + for b.Loop() { for i, this := range containsBenchTestData { result := helpers.ReaderContains(strings.NewReader(this.v1), this.v2) if result != this.expect { diff --git a/helpers/path.go b/helpers/path.go index aee617282..f8f1e2ece 100644 --- a/helpers/path.go +++ b/helpers/path.go @@ -73,8 +73,8 @@ func MakeTitle(inpath string) string { // MakeTitleInPath converts the path given to a suitable title, trimming whitespace func MakePathRelative(inPath string, possibleDirectories ...string) (string, error) { for _, currentPath := range possibleDirectories { - if strings.HasPrefix(inPath, currentPath) { - return strings.TrimPrefix(inPath, currentPath), nil + if after, ok := strings.CutPrefix(inPath, currentPath); ok { + return after, nil } } return inPath, errors.New("can't extract relative path, unknown prefix") @@ -226,8 +226,8 @@ func ExtractRootPaths(paths []string) []string { r := make([]string, len(paths)) for i, p := range paths { root := filepath.ToSlash(p) - sections := strings.Split(root, "/") - for _, section := range sections { + sections := strings.SplitSeq(root, "/") + for section := range sections { if section != "" { root = section break diff --git a/helpers/url_test.go b/helpers/url_test.go index 7f625035c..309dee18a 100644 --- a/helpers/url_test.go +++ b/helpers/url_test.go @@ -259,8 +259,8 @@ func BenchmarkRelURL(b *testing.B) { v := config.New() v.Set("baseURL", "https://base/") p := newTestPathSpecFromCfgAndLang(v, "") - b.ResetTimer() - for i := 0; i < b.N; i++ { + + for b.Loop() { _ = p.RelURL("https://base/foo/bar", false) } } @@ -271,12 +271,12 @@ func BenchmarkAbsURL(b *testing.B) { p := newTestPathSpecFromCfgAndLang(v, "") b.ResetTimer() b.Run("relurl", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { _ = p.AbsURL("foo/bar", false) } }) b.Run("absurl", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { _ = p.AbsURL("https://base/foo/bar", false) } }) diff --git a/hugofs/hashing_fs_test.go b/hugofs/hashing_fs_test.go index 74781ba8d..0d436a87e 100644 --- a/hugofs/hashing_fs_test.go +++ b/hugofs/hashing_fs_test.go @@ -59,9 +59,7 @@ func BenchmarkHashingFs(b *testing.B) { ofs := NewHashingFs(fs, observer) content := []byte(strings.Repeat("lorem ipsum ", 1000)) - b.ResetTimer() - - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { f, err := ofs.Create(fmt.Sprintf("file%d", i)) if err != nil { b.Fatal(err) diff --git a/hugofs/hglob/glob_test.go b/hugofs/hglob/glob_test.go index e69e0e622..72b675242 100644 --- a/hugofs/hglob/glob_test.go +++ b/hugofs/hglob/glob_test.go @@ -81,7 +81,7 @@ func BenchmarkGetGlob(b *testing.B) { if err != nil { b.Fatal(err) } - for i := 0; i < b.N; i++ { + for b.Loop() { _ = g.Match(search) } }) @@ -92,7 +92,7 @@ func BenchmarkGetGlob(b *testing.B) { runBench("Filenames cache, mixed case searches", defaultGlobCache, "abCDe") b.Run("GetGlob", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { _, err := GetGlob("**/foo") if err != nil { b.Fatal(err) diff --git a/hugofs/walk_test.go b/hugofs/walk_test.go index d8dc0d9b0..9d37cb07f 100644 --- a/hugofs/walk_test.go +++ b/hugofs/walk_test.go @@ -182,8 +182,7 @@ func BenchmarkWalk(b *testing.B) { return nil } - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { w := NewWalkway(WalkwayConfig{Fs: fs, Root: "root", WalkFn: walkFn}) if err := w.Walk(); err != nil { diff --git a/hugolib/cascade_test.go b/hugolib/cascade_test.go index e7a4f7917..659006a2c 100644 --- a/hugolib/cascade_test.go +++ b/hugolib/cascade_test.go @@ -36,12 +36,12 @@ func BenchmarkCascade(b *testing.B) { c := qt.New(b) b.StopTimer() builders := make([]*sitesBuilder, b.N) - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { builders[i] = newCascadeTestBuilder(b, langs) } b.StartTimer() - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { builder := builders[i] err := builder.BuildE(BuildCfg{}) c.Assert(err, qt.IsNil) @@ -83,7 +83,7 @@ kind = '{section,term}' b.ResetTimer() - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { builders[i].Build() } }) diff --git a/hugolib/config_test.go b/hugolib/config_test.go index 5ee13ca30..c7c093594 100644 --- a/hugolib/config_test.go +++ b/hugolib/config_test.go @@ -721,7 +721,6 @@ themeconfigdirparam: {{ site.Params.themeconfigdirparam }} ` for _, configName := range []string{"hugo.toml", "config.toml"} { - configName := configName t.Run(configName, func(t *testing.T) { t.Parallel() diff --git a/hugolib/content_map_test.go b/hugolib/content_map_test.go index 971be7b55..6882711fe 100644 --- a/hugolib/content_map_test.go +++ b/hugolib/content_map_test.go @@ -484,7 +484,6 @@ Path: {{ .Path }}|{{.Kind }} ` for _, format := range []string{"toml", "yaml", "json"} { - format := format t.Run(format, func(t *testing.T) { t.Parallel() diff --git a/hugolib/content_render_hooks_test.go b/hugolib/content_render_hooks_test.go index c4e15a5c6..31ce545b5 100644 --- a/hugolib/content_render_hooks_test.go +++ b/hugolib/content_render_hooks_test.go @@ -332,7 +332,6 @@ Image: ![alt-"<>&](/destination-"<> 'title-"<>&') ` for _, enabled := range []bool{true, false} { - enabled := enabled t.Run(fmt.Sprint(enabled), func(t *testing.T) { t.Parallel() b := Test(t, strings.ReplaceAll(files, "ENABLE", fmt.Sprint(enabled))) diff --git a/hugolib/doctree/nodeshiftree_test.go b/hugolib/doctree/nodeshiftree_test.go index 946e6d187..74ce26043 100644 --- a/hugolib/doctree/nodeshiftree_test.go +++ b/hugolib/doctree/nodeshiftree_test.go @@ -195,7 +195,6 @@ func TestTreePara(t *testing.T) { ) for i := range 8 { - i := i r.Run(func() error { a := &testValue{ID: "/a"} lock := tree.Lock(true) @@ -302,7 +301,7 @@ type testValue struct { func BenchmarkTreeInsert(b *testing.B) { runBench := func(b *testing.B, numElements int) { - for i := 0; i < b.N; i++ { + for b.Loop() { tree := doctree.New( doctree.Config[*testValue]{ Shifter: &testShifter{}, @@ -360,7 +359,7 @@ func BenchmarkWalk(b *testing.B) { b.Run(fmt.Sprintf("Walk one dimension %d", numElements), func(b *testing.B) { tree := createTree() b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { w := &doctree.NodeShiftTreeWalker[*testValue]{ Tree: tree, Handle: handle, @@ -374,7 +373,7 @@ func BenchmarkWalk(b *testing.B) { b.Run(fmt.Sprintf("Walk all dimensions %d", numElements), func(b *testing.B) { base := createTree() b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { for d1 := range 1 { for d2 := range 2 { tree := base.Shape(sitesmatrix.Vector{d1, d2, 0}) diff --git a/hugolib/doctree/treeshifttree_test.go b/hugolib/doctree/treeshifttree_test.go index b0ae79633..f5b5802cd 100644 --- a/hugolib/doctree/treeshifttree_test.go +++ b/hugolib/doctree/treeshifttree_test.go @@ -32,7 +32,7 @@ func BenchmarkTreeShiftTreeSlice(b *testing.B) { v := sitesmatrix.Vector{10, 10, 10} t := doctree.NewTreeShiftTree[string](v) b.Run("New", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { for l1 := 0; l1 < v[0]; l1++ { for l2 := 0; l2 < v[1]; l2++ { for l3 := 0; l3 < v[2]; l3++ { @@ -43,7 +43,7 @@ func BenchmarkTreeShiftTreeSlice(b *testing.B) { } }) b.Run("Shape", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { for l1 := 0; l1 < v[0]; l1++ { for l2 := 0; l2 < v[1]; l2++ { for l3 := 0; l3 < v[2]; l3++ { diff --git a/hugolib/filesystems/basefs.go b/hugolib/filesystems/basefs.go index dd3d97e6c..9fa561906 100644 --- a/hugolib/filesystems/basefs.go +++ b/hugolib/filesystems/basefs.go @@ -183,8 +183,8 @@ func (b *BaseFs) AbsProjectContentDir(filename string) (string, string, error) { } else { contentDir := strings.TrimPrefix(strings.TrimPrefix(meta.Filename, meta.BaseDir), filePathSeparator) + filePathSeparator - if strings.HasPrefix(filename, contentDir) { - relFilename := strings.TrimPrefix(filename, contentDir) + if after, ok := strings.CutPrefix(filename, contentDir); ok { + relFilename := after absFilename := filepath.Join(meta.Filename, relFilename) return relFilename, absFilename, nil } diff --git a/hugolib/hugo_sites.go b/hugolib/hugo_sites.go index 5621cd6e6..b1ac8fb25 100644 --- a/hugolib/hugo_sites.go +++ b/hugolib/hugo_sites.go @@ -674,9 +674,9 @@ func (h *HugoSites) handleDataFile(r *source.File) error { // Crawl in data tree to insert data current = h.data dataPath := r.FileInfo().Meta().PathInfo.Unnormalized().Dir()[1:] - keyParts := strings.Split(dataPath, "/") + keyParts := strings.SplitSeq(dataPath, "/") - for _, key := range keyParts { + for key := range keyParts { if key != "" { if _, ok := current[key]; !ok { current[key] = make(map[string]any) diff --git a/hugolib/hugo_sites_build.go b/hugolib/hugo_sites_build.go index 3cd30c41d..b0157bb59 100644 --- a/hugolib/hugo_sites_build.go +++ b/hugolib/hugo_sites_build.go @@ -346,7 +346,6 @@ func (h *HugoSites) assemble(ctx context.Context, l logg.LevelLogger, bcfg *Buil g, _ := h.workersSite.Start(ctx) for _, s := range assemblers { - s := s g.Run(func() error { return s.assemblePagesStep1() }) @@ -751,7 +750,6 @@ func (h *HugoSites) postProcess(l logg.LevelLogger) error { filenames := h.Deps.BuildState.GetFilenamesWithPostPrefix() for _, filename := range filenames { - filename := filename g.Run(func() error { return handleFile(filename) }) diff --git a/hugolib/hugo_smoke_test.go b/hugolib/hugo_smoke_test.go index c6508bc04..9b4079dc9 100644 --- a/hugolib/hugo_smoke_test.go +++ b/hugolib/hugo_smoke_test.go @@ -330,7 +330,7 @@ Single: {{ .Title }}|{{ .RelPermalink}}|{{ range .OutputFormats }}{{ .Name }}: { ` - for i := 0; i < 2; i++ { + for range 2 { b := Test(t, files) b.AssertFileContent("public/index.html", `List: |/|html: /|rss: /index.xml|$`) b.AssertFileContent("public/index.xml", `List xml: |/|html: /|rss: /index.xml|$`) @@ -749,8 +749,7 @@ func BenchmarkBaseline(b *testing.B) { builders[i] = NewIntegrationTestBuilder(cfg) } - b.ResetTimer() - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { builders[i].Build() } } diff --git a/hugolib/integrationtest_builder.go b/hugolib/integrationtest_builder.go index 210a53d32..d1d787797 100644 --- a/hugolib/integrationtest_builder.go +++ b/hugolib/integrationtest_builder.go @@ -408,8 +408,8 @@ func (s *IntegrationTestBuilder) AssertFileContent(filename string, matches ...s for _, m := range matches { cm := qt.Commentf("File: %s Expect: %s Got: %s", filename, m, content) - lines := strings.Split(m, "\n") - for _, match := range lines { + lines := strings.SplitSeq(m, "\n") + for match := range lines { match = strings.TrimSpace(match) if match == "" || strings.HasPrefix(match, "#") { continue @@ -463,8 +463,8 @@ func (s *IntegrationTestBuilder) AssertFs(fs afero.Fs, matches ...string) { content := strings.TrimSpace((strings.Join(printFsLines, "\n"))) for _, m := range matches { cm := qt.Commentf("Match: %q\nIn:\n%s", m, content) - lines := strings.Split(m, "\n") - for _, match := range lines { + lines := strings.SplitSeq(m, "\n") + for match := range lines { match = strings.TrimSpace(match) var negate bool if strings.HasPrefix(match, hglob.NegationPrefix) { diff --git a/hugolib/page_permalink_test.go b/hugolib/page_permalink_test.go index d8fd99d79..03fe62cec 100644 --- a/hugolib/page_permalink_test.go +++ b/hugolib/page_permalink_test.go @@ -64,7 +64,6 @@ func TestPermalink(t *testing.T) { } for i, test := range tests { - i := i test := test t.Run(fmt.Sprintf("%s-%d", test.file, i), func(t *testing.T) { t.Parallel() diff --git a/hugolib/page_test.go b/hugolib/page_test.go index e8f8ced02..b8d56f758 100644 --- a/hugolib/page_test.go +++ b/hugolib/page_test.go @@ -1068,7 +1068,6 @@ func TestPageWithDate(t *testing.T) { func TestPageWithFrontMatterConfig(t *testing.T) { for _, dateHandler := range []string{":filename", ":fileModTime"} { - dateHandler := dateHandler t.Run(fmt.Sprintf("dateHandler=%q", dateHandler), func(t *testing.T) { t.Parallel() c := qt.New(t) diff --git a/hugolib/pagecollections_test.go b/hugolib/pagecollections_test.go index 10c973b7e..0ea757b1c 100644 --- a/hugolib/pagecollections_test.go +++ b/hugolib/pagecollections_test.go @@ -57,12 +57,11 @@ func BenchmarkGetPage(b *testing.B) { pagePaths := make([]string, b.N) - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { pagePaths[i] = fmt.Sprintf("sect%d", r.Intn(10)) } - b.ResetTimer() - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { home, _ := s.getPage(nil, "/") if home == nil { b.Fatal("Home is nil") @@ -121,12 +120,12 @@ func BenchmarkGetPageRegular(b *testing.B) { pagePaths := make([]string, b.N) - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { pagePaths[i] = path.Join(fmt.Sprintf("/sect%d", r.Intn(10)), fmt.Sprintf("page%d.md", r.Intn(100))) } b.ResetTimer() - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { page, _ := s.getPage(nil, pagePaths[i]) c.Assert(page, qt.Not(qt.IsNil)) } @@ -140,13 +139,13 @@ func BenchmarkGetPageRegular(b *testing.B) { pagePaths := make([]string, b.N) pages := make([]page.Page, b.N) - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { pagePaths[i] = fmt.Sprintf("page%d.md", r.Intn(100)) pages[i] = allPages[r.Intn(len(allPages)/3)] } b.ResetTimer() - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { page, _ := s.getPage(pages[i], pagePaths[i]) c.Assert(page, qt.Not(qt.IsNil)) } diff --git a/hugolib/pages_language_merge_test.go b/hugolib/pages_language_merge_test.go index ba1ed83de..62f040739 100644 --- a/hugolib/pages_language_merge_test.go +++ b/hugolib/pages_language_merge_test.go @@ -187,7 +187,7 @@ func BenchmarkMergeByLanguage(b *testing.B) { enSite := h.Sites[0] nnSite := h.Sites[2] - for i := 0; i < b.N; i++ { + for b.Loop() { merged := nnSite.RegularPages().MergeByLanguage(enSite.RegularPages()) if len(merged) != count { b.Fatal("Count mismatch") diff --git a/hugolib/pages_test.go b/hugolib/pages_test.go index 30e9e59d2..cbcaad48d 100644 --- a/hugolib/pages_test.go +++ b/hugolib/pages_test.go @@ -77,7 +77,7 @@ func BenchmarkPagesPrevNext(b *testing.B) { pages = variant.preparePages(pages) } b.StartTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { p := pages[rand.Intn(len(pages))] variant.run(p, pages) } @@ -109,7 +109,7 @@ func BenchmarkPagePageCollections(b *testing.B) { } } b.StartTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { p := pages[rand.Intn(len(pages))] variant.run(p) } diff --git a/hugolib/pagesfromdata/pagesfromgotmpl_test.go b/hugolib/pagesfromdata/pagesfromgotmpl_test.go index c60b56dbf..59cfa7abe 100644 --- a/hugolib/pagesfromdata/pagesfromgotmpl_test.go +++ b/hugolib/pagesfromdata/pagesfromgotmpl_test.go @@ -26,7 +26,7 @@ func BenchmarkHash(b *testing.B) { bs := BuildState{} - for i := 0; i < b.N; i++ { + for b.Loop() { bs.hash(m) } } diff --git a/hugolib/rebuild_test.go b/hugolib/rebuild_test.go index 657ed5609..53a3bef6c 100644 --- a/hugolib/rebuild_test.go +++ b/hugolib/rebuild_test.go @@ -102,7 +102,7 @@ My Other Text: {{ $r.Content }}|{{ $r.Permalink }}| func TestRebuildEditLeafBundleHeaderOnly(t *testing.T) { t.Parallel() - for i := 0; i < 3; i++ { + for range 3 { b := TestRunning(t, rebuildFilesSimple) b.AssertFileContent("public/mysection/mysectionbundle/index.html", "My Section Bundle Content Content.") @@ -1797,8 +1797,7 @@ func BenchmarkRebuildContentFileChange(b *testing.B) { builders[i].Build() } - b.ResetTimer() - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { bb := builders[i] bb.EditFileReplaceFunc("content/mysect/p123/index.md", func(s string) string { return s + "... Edited" diff --git a/hugolib/resource_chain_test.go b/hugolib/resource_chain_test.go index 36e50ae3b..a8bd2d56f 100644 --- a/hugolib/resource_chain_test.go +++ b/hugolib/resource_chain_test.go @@ -198,7 +198,7 @@ relPermalink": "/hello.min.a2d1cb24f24b322a7dad520414c523e9.html" } func BenchmarkResourceChainPostProcess(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { b.StopTimer() s := newTestSitesBuilder(b) for i := range 300 { @@ -579,7 +579,6 @@ XML: {{ $xml.body }} } for _, test := range tests { - test := test t.Run(test.name, func(t *testing.T) { if !test.shouldRun() { t.Skip() diff --git a/hugolib/segments/segments_test.go b/hugolib/segments/segments_test.go index d98df281c..f07dea969 100644 --- a/hugolib/segments/segments_test.go +++ b/hugolib/segments/segments_test.go @@ -144,8 +144,7 @@ func BenchmarkSegmentsMatch(b *testing.B) { match := compileSegments(c, fields...) - b.ResetTimer() - for range b.N { + for b.Loop() { match(SegmentQuery{Site: no, Output: "rss"}) } } diff --git a/hugolib/shortcode.go b/hugolib/shortcode.go index 20fe2bc9d..ec6e25cd6 100644 --- a/hugolib/shortcode.go +++ b/hugolib/shortcode.go @@ -85,8 +85,8 @@ func (scp *ShortcodeWithPage) InnerDeindent() template.HTML { scp.innerDeindentInit.Do(func() { b := bp.GetBuffer() text.VisitLinesAfter(string(scp.Inner), func(s string) { - if strings.HasPrefix(s, scp.indentation) { - b.WriteString(strings.TrimPrefix(s, scp.indentation)) + if after, ok := strings.CutPrefix(s, scp.indentation); ok { + b.WriteString(after) } else { b.WriteString(s) } diff --git a/hugolib/shortcode_test.go b/hugolib/shortcode_test.go index 90fda6593..cf0a77f18 100644 --- a/hugolib/shortcode_test.go +++ b/hugolib/shortcode_test.go @@ -101,8 +101,6 @@ title: "Shortcodes Galore!" {"inline", `{{< my.inline >}}Hi{{< /my.inline >}}`, regexpCheck("my.inline;inline:true;closing:true;inner:{Hi};")}, } { - test := test - t.Run(test.name, func(t *testing.T) { t.Parallel() c := qt.New(t) @@ -266,7 +264,7 @@ func BenchmarkReplaceShortcodeTokens(b *testing.B) { cnt := 0 in := make([]input, b.N*len(data)) - for i := 0; i < b.N; i++ { + for b.Loop() { for _, this := range data { replacements := make(map[string]shortcodeRenderer) for k, v := range this.replacements { @@ -280,10 +278,9 @@ func BenchmarkReplaceShortcodeTokens(b *testing.B) { } } - b.ResetTimer() cnt = 0 ctx := context.Background() - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { for j := range data { currIn := in[cnt] cnt++ @@ -345,9 +342,7 @@ title: "Markdown Shortcode" builders[i] = NewIntegrationTestBuilder(cfg) } - b.ResetTimer() - - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { builders[i].Build() } } @@ -644,7 +639,6 @@ String: {{ . | safeHTML }} func TestInlineShortcodes(t *testing.T) { for _, enableInlineShortcodes := range []bool{true, false} { - enableInlineShortcodes := enableInlineShortcodes t.Run(fmt.Sprintf("enableInlineShortcodes=%t", enableInlineShortcodes), func(t *testing.T) { t.Parallel() diff --git a/hugolib/site.go b/hugolib/site.go index be75f5c7b..ce0331200 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -363,7 +363,7 @@ func NewHugoSites(cfg deps.DepsCfg) (*HugoSites, error) { sitesVersionsRoles := make([][][]*Site, len(sites)) for i := 0; i < len(sites); i++ { sitesVersionsRoles[i] = make([][]*Site, len(versionsSorted)) - for j := 0; j < len(versionsSorted); j++ { + for j := range versionsSorted { sitesVersionsRoles[i][j] = make([]*Site, len(rolesSorted)) } } diff --git a/hugolib/site_benchmark_new_test.go b/hugolib/site_benchmark_new_test.go index 023d8e4d5..75f53cbfd 100644 --- a/hugolib/site_benchmark_new_test.go +++ b/hugolib/site_benchmark_new_test.go @@ -504,7 +504,7 @@ func BenchmarkSiteNew(b *testing.B) { } b.Run(name, func(b *testing.B) { sites := make([]*sitesBuilder, b.N) - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { sites[i] = bm.create(b) if edit { sites[i].Running() @@ -512,7 +512,7 @@ func BenchmarkSiteNew(b *testing.B) { } b.ResetTimer() - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { if edit { b.StopTimer() } diff --git a/hugolib/site_output_test.go b/hugolib/site_output_test.go index caec4c700..14997f688 100644 --- a/hugolib/site_output_test.go +++ b/hugolib/site_output_test.go @@ -30,7 +30,6 @@ import ( func TestSiteWithPageOutputs(t *testing.T) { for _, outputs := range [][]string{{"html", "json", "calendar"}, {"json"}} { - outputs := outputs t.Run(fmt.Sprintf("%v", outputs), func(t *testing.T) { t.Parallel() doTestSiteWithPageOutputs(t, outputs) diff --git a/hugolib/site_sections_test.go b/hugolib/site_sections_test.go index 357d3e77d..a64c653b0 100644 --- a/hugolib/site_sections_test.go +++ b/hugolib/site_sections_test.go @@ -286,7 +286,6 @@ PAG|{{ .Title }}|{{ $sect.InSection . }} home := s.getPageOldVersion(kinds.KindHome) for _, test := range tests { - test := test tt.Run(fmt.Sprintf("sections %s", test.sections), func(c *qt.C) { c.Parallel() sections := strings.Split(test.sections, ",") diff --git a/hugolib/sitesmatrix/dimensions_test.go b/hugolib/sitesmatrix/dimensions_test.go index 6d138a9d4..83cb5aa4c 100644 --- a/hugolib/sitesmatrix/dimensions_test.go +++ b/hugolib/sitesmatrix/dimensions_test.go @@ -49,7 +49,7 @@ func BenchmarkCompare(b *testing.B) { b.Run("Equal", func(b *testing.B) { v1 := Vector{1, 2, 3} v2 := Vector{1, 2, 3} - for range b.N { + for b.Loop() { v1.Compare(v2) } }) @@ -57,7 +57,7 @@ func BenchmarkCompare(b *testing.B) { b.Run("First different", func(b *testing.B) { v1 := Vector{1, 2, 3} v2 := Vector{2, 2, 3} - for range b.N { + for b.Loop() { v1.Compare(v2) } }) @@ -65,7 +65,7 @@ func BenchmarkCompare(b *testing.B) { b.Run("Last different", func(b *testing.B) { v1 := Vector{1, 2, 3} v2 := Vector{1, 2, 4} - for range b.N { + for b.Loop() { v1.Compare(v2) } }) diff --git a/hugolib/sitesmatrix/sitematrix_integration_test.go b/hugolib/sitesmatrix/sitematrix_integration_test.go index 9408a701d..3e93e8f96 100644 --- a/hugolib/sitesmatrix/sitematrix_integration_test.go +++ b/hugolib/sitesmatrix/sitematrix_integration_test.go @@ -1125,11 +1125,11 @@ func BenchmarkSitesMatrixContent(b *testing.B) { for _, multipleDimensions := range []bool{false, true} { b.Run(fmt.Sprintf("n%d/md%t", numPages, multipleDimensions), func(b *testing.B) { builders := make([]*hugolib.IntegrationTestBuilder, b.N) - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { builders[i] = newSitesMatrixContentBenchmarkBuilder(b, numPages, true, true) } b.ResetTimer() - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { builders[i].Build() } }) diff --git a/hugolib/sitesmatrix/vectorstores_test.go b/hugolib/sitesmatrix/vectorstores_test.go index 2f41a3bc9..1365c9055 100644 --- a/hugolib/sitesmatrix/vectorstores_test.go +++ b/hugolib/sitesmatrix/vectorstores_test.go @@ -339,37 +339,37 @@ func BenchmarkIntSetsComplement(b *testing.B) { b.ResetTimer() b.Run("sub set", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { _ = sets2.Complement(sets1) } }) b.Run("super set", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { _ = sets1.Complement(sets2) } }) b.Run("self", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { _ = sets1.Complement(sets1) } }) b.Run("self multiple", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { _ = sets1.Complement(sets1, sets1, sets1, sets1, sets1, sets1, sets1) } }) b.Run("same", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { _ = sets1.Complement(sets1Copy) } }) b.Run("one overlapping language", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { _ = setsLanguage1.Complement(sets1) } }) @@ -399,7 +399,7 @@ func BenchmarkSets(b *testing.B) { b.ResetTimer() b.Run("Build", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { _ = sitesmatrix.NewIntSetsBuilder(testDims).WithSets( maps.NewOrderedIntSet(1, 2), maps.NewOrderedIntSet(1, 2, 3), @@ -408,32 +408,32 @@ func BenchmarkSets(b *testing.B) { } }) b.Run("HasVector", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { _ = sets1.HasVector(v1) _ = sets1.HasVector(v2) } }) b.Run("HasAnyVector", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { _ = sets1.HasAnyVector(sets2) } }) b.Run("FirstVector", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { _ = sets1.VectorSample() } }) b.Run("LenVectors", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { _ = sets1.LenVectors() } }) b.Run("ForEachVector", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { allCount := 0 ok := sets1.ForEachVector(func(v sitesmatrix.Vector) bool { allCount++ @@ -452,7 +452,7 @@ func BenchmarkSets(b *testing.B) { }) b.Run("EqualsVector pointer equal", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { if !sets1.EqualsVector(sets1) { b.Fatal("Expected sets1 to equal itself") } @@ -460,7 +460,7 @@ func BenchmarkSets(b *testing.B) { }) b.Run("EqualsVector equal copy", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { if !sets1.EqualsVector(sets1Copy) { b.Fatal("Expected sets1 to equal its copy") } @@ -468,7 +468,7 @@ func BenchmarkSets(b *testing.B) { }) b.Run("EqualsVector different sets", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { if sets1.EqualsVector(sets2) { b.Fatal("Expected sets1 to not equal sets2") } @@ -476,7 +476,7 @@ func BenchmarkSets(b *testing.B) { }) b.Run("Distance", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { _ = sets1.VectorSample().Distance(v1) _ = sets1.VectorSample().Distance(v2) } diff --git a/hugolib/taxonomy_test.go b/hugolib/taxonomy_test.go index 272735367..4b3010859 100644 --- a/hugolib/taxonomy_test.go +++ b/hugolib/taxonomy_test.go @@ -877,7 +877,7 @@ GetTerms.tags: {{ range .GetTerms "tags" }}{{ .Title }}|{{ end }} for _, numPages := range []int{100, 1000, 10000, 20000} { b.Run(fmt.Sprintf("pages_%d", numPages), func(b *testing.B) { builders := createBuilders(b, numPages) - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { builders[i].Build() } }) diff --git a/hugolib/template_test.go b/hugolib/template_test.go index 31b18bdc4..56b9c3f9b 100644 --- a/hugolib/template_test.go +++ b/hugolib/template_test.go @@ -186,7 +186,6 @@ func TestTemplateLookupOrder(t *testing.T) { }, } { - this := this if this.name != "Variant 1" { continue } diff --git a/hugolib/testhelpers_test.go b/hugolib/testhelpers_test.go index 2007b658d..2fa3d5634 100644 --- a/hugolib/testhelpers_test.go +++ b/hugolib/testhelpers_test.go @@ -750,8 +750,8 @@ func (s *sitesBuilder) AssertFileContent(filename string, matches ...string) { s.T.Helper() content := s.FileContent(filename) for _, m := range matches { - lines := strings.Split(m, "\n") - for _, match := range lines { + lines := strings.SplitSeq(m, "\n") + for match := range lines { match = strings.TrimSpace(match) if match == "" { continue diff --git a/identity/finder_test.go b/identity/finder_test.go index 949a43713..85a5fd928 100644 --- a/identity/finder_test.go +++ b/identity/finder_test.go @@ -39,7 +39,7 @@ func BenchmarkFinder(b *testing.B) { f := identity.NewFinder(identity.FinderConfig{}) b.Run("Find one", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { r := f.Contains(b4, m1, -1) if r == 0 { b.Fatal("not found") @@ -48,7 +48,7 @@ func BenchmarkFinder(b *testing.B) { }) b.Run("Find none", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { r := f.Contains(b5, m1, -1) if r > 0 { b.Fatal("found") diff --git a/identity/identity_test.go b/identity/identity_test.go index dac92d86b..a14ab615d 100644 --- a/identity/identity_test.go +++ b/identity/identity_test.go @@ -33,7 +33,7 @@ func BenchmarkIdentityManager(b *testing.B) { } b.Run("identity.NewManager", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { m := identity.NewManager() if m == nil { b.Fatal("manager is nil") @@ -46,7 +46,7 @@ func BenchmarkIdentityManager(b *testing.B) { im := identity.NewManager() b.ResetTimer() - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { im.AddIdentity(ids[i]) } @@ -58,7 +58,7 @@ func BenchmarkIdentityManager(b *testing.B) { im := identity.NewManager() b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { im.AddIdentity(id) } @@ -67,33 +67,33 @@ func BenchmarkIdentityManager(b *testing.B) { b.Run("Nop StringIdentity const", func(b *testing.B) { const id = identity.StringIdentity("test") - for i := 0; i < b.N; i++ { + for b.Loop() { identity.NopManager.AddIdentity(id) } }) b.Run("Nop StringIdentity const other package", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { identity.NopManager.AddIdentity(identitytesting.TestIdentity) } }) b.Run("Nop StringIdentity var", func(b *testing.B) { id := identity.StringIdentity("test") - for i := 0; i < b.N; i++ { + for b.Loop() { identity.NopManager.AddIdentity(id) } }) b.Run("Nop pointer identity", func(b *testing.B) { id := &testIdentity{base: "a", name: "b"} - for i := 0; i < b.N; i++ { + for b.Loop() { identity.NopManager.AddIdentity(id) } }) b.Run("Nop Anonymous", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { identity.NopManager.AddIdentity(identity.Anonymous) } }) @@ -101,7 +101,7 @@ func BenchmarkIdentityManager(b *testing.B) { func BenchmarkIsNotDependent(b *testing.B) { runBench := func(b *testing.B, id1, id2 identity.Identity) { - for i := 0; i < b.N; i++ { + for b.Loop() { isNotDependent(id1, id2) } } diff --git a/internal/js/esbuild/resolve.go b/internal/js/esbuild/resolve.go index a2516dbd2..18ad8a40a 100644 --- a/internal/js/esbuild/resolve.go +++ b/internal/js/esbuild/resolve.go @@ -186,8 +186,8 @@ func createBuildPlugins(rs *resources.Spec, assetsResolver *fsResolver, depsMana isStdin := importer == stdinImporter var relDir string if !isStdin { - if strings.HasPrefix(importer, PrefixHugoVirtual) { - relDir = filepath.Dir(strings.TrimPrefix(importer, PrefixHugoVirtual)) + if after, ok := strings.CutPrefix(importer, PrefixHugoVirtual); ok { + relDir = filepath.Dir(after) } else { rel, found := fs.MakePathRelative(importer, true) diff --git a/internal/warpc/warpc.go b/internal/warpc/warpc.go index e21fefa8a..0d8d6a030 100644 --- a/internal/warpc/warpc.go +++ b/internal/warpc/warpc.go @@ -435,7 +435,6 @@ func newDispatcher[Q, R any](opts Options) (*dispatcherPool[Q, R], error) { run := func() error { g, ctx := errgroup.WithContext(ctx) for _, c := range inOuts { - c := c g.Go(func() error { var errBuff bytes.Buffer ctx := context.WithoutCancel(ctx) diff --git a/internal/warpc/warpc_test.go b/internal/warpc/warpc_test.go index 2ee4c3de5..d19334806 100644 --- a/internal/warpc/warpc_test.go +++ b/internal/warpc/warpc_test.go @@ -263,8 +263,7 @@ func BenchmarkExecuteKatex(b *testing.B) { input := katexInputTemplate - b.ResetTimer() - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { message := Message[KatexInput]{ Header: Header{ Version: currentVersion, @@ -293,7 +292,7 @@ func BenchmarkKatexStartStop(b *testing.B) { } runBench := func(b *testing.B, opts Options) { - for i := 0; i < b.N; i++ { + for b.Loop() { d, err := Start[KatexInput, KatexOutput](opts) if err != nil { b.Fatal(err) @@ -389,8 +388,7 @@ func BenchmarkExecuteGreet(b *testing.B) { Name: "Person", } - b.ResetTimer() - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { message := Message[person]{ Header: Header{ Version: currentVersion, diff --git a/langs/i18n/i18n_test.go b/langs/i18n/i18n_test.go index a23cee539..7d5a7c8b5 100644 --- a/langs/i18n/i18n_test.go +++ b/langs/i18n/i18n_test.go @@ -507,7 +507,7 @@ func BenchmarkI18nTranslate(b *testing.B) { b.Run(test.name, func(b *testing.B) { tp := prepareTranslationProvider(b, test, v) b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { f := tp.t.Func(test.lang) actual := f(context.Background(), test.id, test.args) if actual != test.expected { diff --git a/langs/language_test.go b/langs/language_test.go index 33240f3f4..e8d319f04 100644 --- a/langs/language_test.go +++ b/langs/language_test.go @@ -57,7 +57,7 @@ func BenchmarkCollator(b *testing.B) { b.Run("Single", func(b *testing.B) { coll := &Collator{c: collate.New(language.English, collate.Loose)} - for i := 0; i < b.N; i++ { + for b.Loop() { doWork(coll) } }) diff --git a/markup/goldmark/autoid_test.go b/markup/goldmark/autoid_test.go index e0770d86c..a2a7551d0 100644 --- a/markup/goldmark/autoid_test.go +++ b/markup/goldmark/autoid_test.go @@ -74,7 +74,6 @@ tabspace } for i, input := range testlines { - input := input expect := expectlines[i] c.Run(input, func(c *qt.C) { b := []byte(input) @@ -100,8 +99,8 @@ func TestSanitizeAnchorNameBlackfriday(t *testing.T) { func BenchmarkSanitizeAnchorName(b *testing.B) { input := []byte("God is good: 神真美好") - b.ResetTimer() - for i := 0; i < b.N; i++ { + + for b.Loop() { result := sanitizeAnchorName(input, goldmark_config.AutoIDTypeGitHub) if len(result) != 24 { b.Fatalf("got %d", len(result)) @@ -111,8 +110,8 @@ func BenchmarkSanitizeAnchorName(b *testing.B) { func BenchmarkSanitizeAnchorNameAsciiOnly(b *testing.B) { input := []byte("God is good: 神真美好") - b.ResetTimer() - for i := 0; i < b.N; i++ { + + for b.Loop() { result := sanitizeAnchorName(input, goldmark_config.AutoIDTypeGitHubAscii) if len(result) != 12 { b.Fatalf("got %d", len(result)) @@ -122,8 +121,8 @@ func BenchmarkSanitizeAnchorNameAsciiOnly(b *testing.B) { func BenchmarkSanitizeAnchorNameBlackfriday(b *testing.B) { input := []byte("God is good: 神真美好") - b.ResetTimer() - for i := 0; i < b.N; i++ { + + for b.Loop() { result := sanitizeAnchorName(input, goldmark_config.AutoIDTypeBlackfriday) if len(result) != 24 { b.Fatalf("got %d", len(result)) @@ -133,8 +132,8 @@ func BenchmarkSanitizeAnchorNameBlackfriday(b *testing.B) { func BenchmarkSanitizeAnchorNameString(b *testing.B) { input := "God is good: 神真美好" - b.ResetTimer() - for i := 0; i < b.N; i++ { + + for b.Loop() { result := sanitizeAnchorNameString(input, goldmark_config.AutoIDTypeGitHub) if len(result) != 24 { b.Fatalf("got %d", len(result)) diff --git a/markup/goldmark/codeblocks/codeblocks_integration_test.go b/markup/goldmark/codeblocks/codeblocks_integration_test.go index 2fd4fa170..c4da3d2e4 100644 --- a/markup/goldmark/codeblocks/codeblocks_integration_test.go +++ b/markup/goldmark/codeblocks/codeblocks_integration_test.go @@ -339,7 +339,6 @@ Common }{ {"issue-9819", "asdf\n: {#myid}"}, } { - test := test t.Run(test.name, func(t *testing.T) { t.Parallel() b := hugolib.NewIntegrationTestBuilder( diff --git a/markup/goldmark/goldmark_integration_test.go b/markup/goldmark/goldmark_integration_test.go index a6eac4e48..8dc24593f 100644 --- a/markup/goldmark/goldmark_integration_test.go +++ b/markup/goldmark/goldmark_integration_test.go @@ -284,9 +284,7 @@ D. builders[i] = hugolib.NewIntegrationTestBuilder(cfg) } - b.ResetTimer() - - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { builders[i].Build() } } @@ -345,7 +343,7 @@ FENCE b.ResetTimer() - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { builders[i].Build() } } diff --git a/markup/goldmark/hugocontext/hugocontext.go b/markup/goldmark/hugocontext/hugocontext.go index 7a556083c..8afe64b7f 100644 --- a/markup/goldmark/hugocontext/hugocontext.go +++ b/markup/goldmark/hugocontext/hugocontext.go @@ -77,8 +77,8 @@ func (n *HugoContext) Dump(source []byte, level int) { } func (n *HugoContext) parseAttrs(attrBytes []byte) { - keyPairs := bytes.Split(attrBytes, []byte(" ")) - for _, keyPair := range keyPairs { + keyPairs := bytes.SplitSeq(attrBytes, []byte(" ")) + for keyPair := range keyPairs { kv := bytes.Split(keyPair, []byte("=")) if len(kv) != 2 { continue diff --git a/markup/goldmark/hugocontext/hugocontext_test.go b/markup/goldmark/hugocontext/hugocontext_test.go index 62769f4d0..597190012 100644 --- a/markup/goldmark/hugocontext/hugocontext_test.go +++ b/markup/goldmark/hugocontext/hugocontext_test.go @@ -28,7 +28,7 @@ func TestWrap(t *testing.T) { } func BenchmarkWrap(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { Wrap([]byte("test"), 42) } } diff --git a/markup/highlight/config.go b/markup/highlight/config.go index b19e9ec1b..fff9bfeca 100644 --- a/markup/highlight/config.go +++ b/markup/highlight/config.go @@ -197,7 +197,7 @@ func parseHighlightOptions(in string) (map[string]any, error) { return opts, nil } - for _, v := range strings.Split(in, ",") { + for v := range strings.SplitSeq(in, ",") { keyVal := strings.Split(v, "=") key := strings.Trim(keyVal[0], " ") if len(keyVal) != 2 { @@ -265,8 +265,8 @@ func hlLinesToRanges(startLine int, s string) ([][2]int, error) { // 1-2 3 // 1 3-4 // 1 3-4 - fields := strings.Split(s, " ") - for _, field := range fields { + fields := strings.SplitSeq(s, " ") + for field := range fields { field = strings.TrimSpace(field) if field == "" { continue diff --git a/markup/internal/external.go b/markup/internal/external.go index dc8cc61c2..dcb8932ca 100644 --- a/markup/internal/external.go +++ b/markup/internal/external.go @@ -37,7 +37,7 @@ func ExternallyRenderContent( // Most external helpers exit w/ non-zero exit code only if severe, i.e. // halting errors occurred. -> log stderr output regardless of state of err - for _, item := range strings.Split(cmderr.String(), "\n") { + for item := range strings.SplitSeq(cmderr.String(), "\n") { item := strings.TrimSpace(item) if item != "" { if err == nil { diff --git a/markup/tableofcontents/tableofcontents_test.go b/markup/tableofcontents/tableofcontents_test.go index b07d9e3ad..d85156150 100644 --- a/markup/tableofcontents/tableofcontents_test.go +++ b/markup/tableofcontents/tableofcontents_test.go @@ -204,12 +204,12 @@ func BenchmarkToc(b *testing.B) { b.Run("Build", func(b *testing.B) { var builders []Builder - for i := 0; i < b.N; i++ { + for b.Loop() { builders = append(builders, newTestTocBuilder()) } b.ResetTimer() - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { b := builders[i] b.Build() } @@ -218,7 +218,7 @@ func BenchmarkToc(b *testing.B) { b.Run("ToHTML", func(b *testing.B) { tocs := newTocs(b.N) b.ResetTimer() - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { toc := tocs[i] toc.ToHTML(1, -1, false) } diff --git a/media/mediaType_test.go b/media/mediaType_test.go index 3b8e099b8..43504ac79 100644 --- a/media/mediaType_test.go +++ b/media/mediaType_test.go @@ -194,7 +194,7 @@ func TestToJSON(t *testing.T) { func BenchmarkTypeOps(b *testing.B) { mt := Builtin.MPEGType mts := DefaultTypes - for i := 0; i < b.N; i++ { + for b.Loop() { ff := mt.FirstSuffix _ = ff.FullSuffix _ = mt.IsZero() diff --git a/metrics/metrics_test.go b/metrics/metrics_test.go index 6e799a393..83dc1433f 100644 --- a/metrics/metrics_test.go +++ b/metrics/metrics_test.go @@ -62,8 +62,7 @@ func BenchmarkHowSimilar(b *testing.B) { s1 := "Hugo is cool and " + strings.Repeat("fun ", 10) + "!" s2 := "Hugo is cool and " + strings.Repeat("cool ", 10) + "!" - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { howSimilar(s1, s2) } } diff --git a/modules/collect.go b/modules/collect.go index bf027b372..9a86c8140 100644 --- a/modules/collect.go +++ b/modules/collect.go @@ -22,6 +22,7 @@ import ( "os" "path/filepath" "regexp" + "slices" "strings" "time" @@ -148,11 +149,8 @@ func filterDuplicateMounts(mounts []Mount) []Mount { var x []Mount for _, m1 := range mounts { var found bool - for _, m2 := range x { - if m1.Equal(m2) { - found = true - break - } + if slices.ContainsFunc(x, m1.Equal) { + found = true } if !found { x = append(x, m1) diff --git a/parser/metadecoders/decoder_test.go b/parser/metadecoders/decoder_test.go index bc5e93d16..678197f7d 100644 --- a/parser/metadecoders/decoder_test.go +++ b/parser/metadecoders/decoder_test.go @@ -216,7 +216,7 @@ c: "d" `) - for i := 0; i < b.N; i++ { + for b.Loop() { _, err := d.UnmarshalToMap(data, YAML) if err != nil { b.Fatal(err) @@ -256,7 +256,7 @@ params: for _, tt := range yamlTests { b.Run(tt.Title+" no validation", func(b *testing.B) { - for range b.N { + for b.Loop() { var v any if err := unmarshalYamlNoValidation(tt.Content, &v); err != nil { b.Fatal(err) @@ -265,7 +265,7 @@ params: }) b.Run(tt.Title+" with validation", func(b *testing.B) { - for range b.N { + for b.Loop() { var v any err := UnmarshalYaml(tt.Content, &v) if tt.IsExpectedToFailValidation { diff --git a/parser/pageparser/pageparser_shortcode_test.go b/parser/pageparser/pageparser_shortcode_test.go index 976597af2..47c33a86d 100644 --- a/parser/pageparser/pageparser_shortcode_test.go +++ b/parser/pageparser/pageparser_shortcode_test.go @@ -280,8 +280,8 @@ func BenchmarkShortcodeLexer(b *testing.B) { testInputs[i] = []byte(input.input) } var cfg Config - b.ResetTimer() - for i := 0; i < b.N; i++ { + + for b.Loop() { for _, input := range testInputs { _, err := collectWithConfig(input, true, lexMainSection, cfg) if err != nil { diff --git a/parser/pageparser/pageparser_test.go b/parser/pageparser/pageparser_test.go index c6bedbd6f..de7d2e140 100644 --- a/parser/pageparser/pageparser_test.go +++ b/parser/pageparser/pageparser_test.go @@ -40,8 +40,7 @@ This is some summary. This is some summary. This is some summary. This is some s input := []byte(start + strings.Repeat(strings.Repeat("this is text", 30)+"{{< myshortcode >}}This is some inner content.{{< /myshortcode >}}", 10)) cfg := Config{} - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { if _, err := parseBytes(input, cfg, lexIntroSection); err != nil { b.Fatal(err) } @@ -90,13 +89,13 @@ func BenchmarkHasShortcode(b *testing.B) { withShortcode := strings.Repeat("this is text", 30) + "{{< myshortcode >}}This is some inner content.{{< /myshortcode >}}" + strings.Repeat("this is text", 30) withoutShortcode := strings.Repeat("this is text", 30) + "This is some inner content." + strings.Repeat("this is text", 30) b.Run("Match", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { HasShortcode(withShortcode) } }) b.Run("NoMatch", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { HasShortcode(withoutShortcode) } }) diff --git a/publisher/htmlElementsCollector_test.go b/publisher/htmlElementsCollector_test.go index 36447258e..2e6ebdf21 100644 --- a/publisher/htmlElementsCollector_test.go +++ b/publisher/htmlElementsCollector_test.go @@ -244,7 +244,7 @@ func BenchmarkElementsCollectorWriter(b *testing.B) { ` - for i := 0; i < b.N; i++ { + for b.Loop() { w := newHTMLElementsCollectorWriter(newHTMLElementsCollector( config.BuildStats{Enable: true}, )) @@ -272,7 +272,7 @@ func BenchmarkElementsCollectorWriterPre(b *testing.B) { w := newHTMLElementsCollectorWriter(newHTMLElementsCollector( config.BuildStats{Enable: true}, )) - for i := 0; i < b.N; i++ { + for b.Loop() { fmt.Fprint(w, benchHTML) } } diff --git a/related/inverted_index_test.go b/related/inverted_index_test.go index d57237e11..c44be4a62 100644 --- a/related/inverted_index_test.go +++ b/related/inverted_index_test.go @@ -344,7 +344,7 @@ func BenchmarkRelatedNewIndex(b *testing.B) { } b.Run("singles", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { idx := NewInvertedIndex(cfg) for _, doc := range pages { idx.Add(context.Background(), doc) @@ -353,7 +353,7 @@ func BenchmarkRelatedNewIndex(b *testing.B) { }) b.Run("all", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { idx := NewInvertedIndex(cfg) docs := make([]Document, len(pages)) for i := range pages { @@ -401,9 +401,8 @@ func BenchmarkRelatedMatchesIn(b *testing.B) { idx.Add(context.Background(), newTestDoc(index, allKeywords[start:end]...)) } - b.ResetTimer() ctx := context.Background() - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { if i%10 == 0 { idx.search(ctx, q2) } else { diff --git a/related/related_integration_test.go b/related/related_integration_test.go index 6d3c6d6de..18ec51f4a 100644 --- a/related/related_integration_test.go +++ b/related/related_integration_test.go @@ -181,9 +181,7 @@ keywords: ['k%d'] builders[i] = hugolib.NewIntegrationTestBuilder(cfg) } - b.ResetTimer() - - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { builders[i].Build() } } diff --git a/resources/image_test.go b/resources/image_test.go index ee5de8bec..9b9c69f49 100644 --- a/resources/image_test.go +++ b/resources/image_test.go @@ -469,7 +469,7 @@ func BenchmarkImageExif(b *testing.B) { getImages := func(c *qt.C, b *testing.B, fs afero.Fs) []images.ImageResource { spec := newTestResourceSpec(specDescriptor{fs: fs, c: c}) imgs := make([]images.ImageResource, b.N) - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { imgs[i] = fetchResourceForSpec(spec, c, "sunset.jpg", strconv.Itoa(i)).(images.ImageResource) } return imgs @@ -487,7 +487,7 @@ func BenchmarkImageExif(b *testing.B) { images := getImages(c, b, afero.NewMemMapFs()) b.StartTimer() - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { getAndCheckExif(c, images[i]) } }) @@ -498,7 +498,7 @@ func BenchmarkImageExif(b *testing.B) { images := getImages(c, b, afero.NewMemMapFs()) b.StartTimer() - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { for range 10 { getAndCheckExif(c, images[i]) } @@ -510,14 +510,14 @@ func BenchmarkImageExif(b *testing.B) { c := qt.New(b) fs := afero.NewMemMapFs() images := getImages(c, b, fs) - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { getAndCheckExif(c, images[i]) } images = getImages(c, b, fs) b.StartTimer() - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { getAndCheckExif(c, images[i]) } }) diff --git a/resources/images/color_test.go b/resources/images/color_test.go index 3a75c068e..540f3bea8 100644 --- a/resources/images/color_test.go +++ b/resources/images/color_test.go @@ -43,7 +43,6 @@ func TestHexStringToColor(t *testing.T) { {"777", color.RGBA{R: 0x77, G: 0x77, B: 0x77, A: 0xff}}, } { - test := test c.Run(test.arg, func(c *qt.C) { c.Parallel() @@ -78,7 +77,6 @@ func TestColorToHexString(t *testing.T) { {color.RGBA{R: 0x42, G: 0x87, B: 0xf5, A: 0x80}, "#4287f580"}, } { - test := test c.Run(test.expect, func(c *qt.C) { c.Parallel() diff --git a/resources/images/exif/exif_test.go b/resources/images/exif/exif_test.go index 278bc761a..3aab7d8bb 100644 --- a/resources/images/exif/exif_test.go +++ b/resources/images/exif/exif_test.go @@ -99,8 +99,7 @@ func BenchmarkDecodeExif(b *testing.B) { d, err := NewDecoder() c.Assert(err, qt.IsNil) - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { _, err = d.Decode("", imagemeta.JPEG, f) c.Assert(err, qt.IsNil) f.Seek(0, 0) diff --git a/resources/images/text.go b/resources/images/text.go index f3943a475..11c6eac41 100644 --- a/resources/images/text.go +++ b/resources/images/text.go @@ -96,10 +96,10 @@ func (f textFilter) Draw(dst draw.Image, src image.Image, options *gift.Options) // Calculate lines, consider and include linebreaks finalLines := []string{} f.text = strings.ReplaceAll(f.text, "\r", "") - for _, line := range strings.Split(f.text, "\n") { + for line := range strings.SplitSeq(f.text, "\n") { currentLine := "" // Break each line at the maximum width. - for _, str := range strings.Fields(line) { + for str := range strings.FieldsSeq(line) { fieldStrWidth := font.MeasureString(face, str) currentLineStrWidth := font.MeasureString(face, currentLine) diff --git a/resources/page/page_markup_test.go b/resources/page/page_markup_test.go index 43eaae6f6..fc1102434 100644 --- a/resources/page/page_markup_test.go +++ b/resources/page/page_markup_test.go @@ -172,10 +172,10 @@ func TestIsProbablyHTMLToken(t *testing.T) { } func BenchmarkSummaryFromHTML(b *testing.B) { - b.StopTimer() + input := "

First paragraph

Second paragraph

" - b.StartTimer() - for i := 0; i < b.N; i++ { + + for b.Loop() { summary := ExtractSummaryFromHTML(media.Builtin.MarkdownType, input, 2, false) if s := summary.Content(); s != input { b.Fatalf("unexpected content: %q", s) @@ -190,10 +190,10 @@ func BenchmarkSummaryFromHTML(b *testing.B) { } func BenchmarkSummaryFromHTMLWithDivider(b *testing.B) { - b.StopTimer() + input := "

First paragraph

FOOO

Second paragraph

" - b.StartTimer() - for i := 0; i < b.N; i++ { + + for b.Loop() { summary := ExtractSummaryFromHTMLWithDivider(media.Builtin.MarkdownType, input, "FOOO") if s := summary.Content(); s != "

First paragraph

Second paragraph

" { b.Fatalf("unexpected content: %q", s) diff --git a/resources/page/page_paths_test.go b/resources/page/page_paths_test.go index e2765ec65..a396d9143 100644 --- a/resources/page/page_paths_test.go +++ b/resources/page/page_paths_test.go @@ -36,7 +36,7 @@ func BenchmarkPagePathsBuilderPath(b *testing.B) { defer putPagePathBuilder(pb) pb.Add("foo", "bar") - for i := 0; i < b.N; i++ { + for b.Loop() { _ = pb.Path(0) } } @@ -48,7 +48,7 @@ func BenchmarkPagePathsBuilderPathDir(b *testing.B) { pb.Add("foo", "bar") pb.prefixPath = "foo/" - for i := 0; i < b.N; i++ { + for b.Loop() { _ = pb.PathDir() } } diff --git a/resources/page/pages_cache_test.go b/resources/page/pages_cache_test.go index 9e6af1c28..6cbc71d10 100644 --- a/resources/page/pages_cache_test.go +++ b/resources/page/pages_cache_test.go @@ -80,8 +80,7 @@ func BenchmarkPageCache(b *testing.B) { } key := "key" - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { cache.getP(key, nil, pages) } } diff --git a/resources/page/pages_sort_search_test.go b/resources/page/pages_sort_search_test.go index 8f115109c..9c5fdd550 100644 --- a/resources/page/pages_sort_search_test.go +++ b/resources/page/pages_sort_search_test.go @@ -99,7 +99,7 @@ func BenchmarkSearchPage(b *testing.B) { pages = variant.preparePages(pages) } b.StartTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { j := rand.Intn(numPages) k := variant.search(pages[j], pages) if k != j { diff --git a/resources/page/pages_sort_test.go b/resources/page/pages_sort_test.go index 70c7bc8a8..f437f54ed 100644 --- a/resources/page/pages_sort_test.go +++ b/resources/page/pages_sort_test.go @@ -236,8 +236,7 @@ func TestPageSortByParamNumeric(t *testing.T) { func BenchmarkSortByWeightAndReverse(b *testing.B) { p := createSortTestPages(300) - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { p = p.ByWeight().Reverse() } } diff --git a/resources/page/permalinks_test.go b/resources/page/permalinks_test.go index 90d678400..9b3c315ed 100644 --- a/resources/page/permalinks_test.go +++ b/resources/page/permalinks_test.go @@ -313,8 +313,7 @@ func BenchmarkPermalinkExpand(b *testing.B) { b.Fatal(err) } - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { s, err := expander.Expand("posts", page) if err != nil { b.Fatal(err) diff --git a/resources/resource_test.go b/resources/resource_test.go index d07770456..0d1e921de 100644 --- a/resources/resource_test.go +++ b/resources/resource_test.go @@ -43,8 +43,7 @@ func BenchmarkHashImage(b *testing.B) { } defer f.Close() - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { _, _, err := hashImage(f) if err != nil { b.Fatal(err) diff --git a/resources/resource_transformers/cssjs/inline_imports_test.go b/resources/resource_transformers/cssjs/inline_imports_test.go index 9bcb7f9a3..3578e2612 100644 --- a/resources/resource_transformers/cssjs/inline_imports_test.go +++ b/resources/resource_transformers/cssjs/inline_imports_test.go @@ -158,7 +158,7 @@ LOCAL_STYLE logger := loggers.NewDefault() - for i := 0; i < b.N; i++ { + for b.Loop() { b.StopTimer() imp := newImportResolver( strings.NewReader(mainStyles), diff --git a/source/content_directory_test.go b/source/content_directory_test.go index fea83fd52..d4e276fed 100644 --- a/source/content_directory_test.go +++ b/source/content_directory_test.go @@ -56,7 +56,6 @@ func TestIgnoreDotFilesAndDirectories(t *testing.T) { } for i, test := range tests { - test := test c.Run(fmt.Sprintf("[%d] %s", i, test.path), func(c *qt.C) { c.Parallel() v := config.New() diff --git a/tpl/collections/collections_test.go b/tpl/collections/collections_test.go index 0face752c..8099ea91d 100644 --- a/tpl/collections/collections_test.go +++ b/tpl/collections/collections_test.go @@ -195,7 +195,6 @@ func TestDictionary(t *testing.T) { {[]any{5, "b"}, false}, {[]any{"a", "b", "c"}, false}, } { - i := i test := test c.Run(fmt.Sprint(i), func(c *qt.C) { c.Parallel() @@ -812,7 +811,7 @@ func BenchmarkD2(b *testing.B) { runBenchmark := func(seed, n, max int) { name := fmt.Sprintf("n=%d,max=%d", n, max) b.Run(name, func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { ns.D(seed, n, max) } }) diff --git a/tpl/collections/merge_test.go b/tpl/collections/merge_test.go index a8ef0afea..f6589b6d4 100644 --- a/tpl/collections/merge_test.go +++ b/tpl/collections/merge_test.go @@ -137,7 +137,6 @@ func TestMerge(t *testing.T) { {"all nil", []any{nil, nil}, nil, true}, } { - test := test i := i t.Run(test.name, func(t *testing.T) { @@ -162,7 +161,7 @@ func TestMerge(t *testing.T) { func BenchmarkMerge(b *testing.B) { ns := newNs() - for i := 0; i < b.N; i++ { + for b.Loop() { ns.Merge( map[string]any{"a": 42, "c": 3, "e": 11}, map[string]any{"a": 1, "b": 2}, diff --git a/tpl/collections/querify_test.go b/tpl/collections/querify_test.go index 17556e4cb..9d4f72923 100644 --- a/tpl/collections/querify_test.go +++ b/tpl/collections/querify_test.go @@ -85,8 +85,7 @@ func BenchmarkQuerify(b *testing.B) { ns := newNs() params := []any{"a", "b", "c", "d", "f", " &"} - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { _, err := ns.Querify(params...) if err != nil { b.Fatal(err) @@ -98,8 +97,7 @@ func BenchmarkQuerifySlice(b *testing.B) { ns := newNs() params := []string{"a", "b", "c", "d", "f", " &"} - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { _, err := ns.Querify(params) if err != nil { b.Fatal(err) @@ -111,8 +109,7 @@ func BenchmarkQuerifyMap(b *testing.B) { ns := newNs() params := map[string]any{"a": "b", "c": "d", "f": " &"} - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { _, err := ns.Querify(params) if err != nil { b.Fatal(err) diff --git a/tpl/collections/sort_test.go b/tpl/collections/sort_test.go index cc4581921..4fc95ac8a 100644 --- a/tpl/collections/sort_test.go +++ b/tpl/collections/sort_test.go @@ -265,7 +265,7 @@ func TestSort(t *testing.T) { func BenchmarkSortMap(b *testing.B) { ns := newNs() m := map[string]int{"1": 10, "2": 20, "3": 30, "4": 40, "5": 50} - for i := 0; i < b.N; i++ { + for b.Loop() { ns.Sort(context.Background(), m) } } diff --git a/tpl/collections/vitter_test.go b/tpl/collections/vitter_test.go index 961d1d367..d0fb469fe 100644 --- a/tpl/collections/vitter_test.go +++ b/tpl/collections/vitter_test.go @@ -42,7 +42,7 @@ func TestInspectCounts(t *testing.T) { const k = 10 const iters = 10000 counts := make([]int, max) - for i := 0; i < iters; i++ { + for range iters { testD(prng, t, k, max, func(n int) { counts[n]++ }) @@ -87,7 +87,7 @@ func BenchmarkD(b *testing.B) { // TODO: count rng calls? for _, want := range []int{1, 100, 10000} { b.Run(fmt.Sprintf("want=%d", want), func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { _d(prng, want, 1000000, func(int) {}) } }) diff --git a/tpl/collections/where_test.go b/tpl/collections/where_test.go index 6d53fc1ff..3b55ffdbd 100644 --- a/tpl/collections/where_test.go +++ b/tpl/collections/where_test.go @@ -886,19 +886,19 @@ func BenchmarkWhereOps(b *testing.B) { } b.Run("eq", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { runOps(b, "eq", "bar") } }) b.Run("ne", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { runOps(b, "ne", "baz") } }) b.Run("like", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { runOps(b, "like", "^bar") } }) @@ -912,7 +912,7 @@ func BenchmarkWhereMap(b *testing.B) { seq[fmt.Sprintf("key%d", i)] = "value" } - for i := 0; i < b.N; i++ { + for b.Loop() { _, err := ns.Where(context.Background(), seq, "key", "eq", "value") if err != nil { b.Fatal(err) @@ -931,8 +931,8 @@ func BenchmarkWhereSliceOfStructPointersWithMethod(b *testing.B) { seq = append(seq, &TstX{A: "baz", B: "qux"}) } } - b.ResetTimer() - for range b.N { + + for b.Loop() { _, err := ns.Where(context.Background(), seq, "TstRv2", "eq", "bar") if err != nil { b.Fatal(err) diff --git a/tpl/hash/hash_test.go b/tpl/hash/hash_test.go index ff5d59a9a..d493aa16e 100644 --- a/tpl/hash/hash_test.go +++ b/tpl/hash/hash_test.go @@ -43,7 +43,7 @@ func BenchmarkXxHash(b *testing.B) { runBench := func(name, input string, b *testing.B, fn func(v any)) { b.Run(fmt.Sprintf("%s_%d", name, len(input)), func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { fn(input) } }) diff --git a/tpl/internal/go_templates/htmltemplate/exec_test.go b/tpl/internal/go_templates/htmltemplate/exec_test.go index e349ca7cd..2790470fd 100644 --- a/tpl/internal/go_templates/htmltemplate/exec_test.go +++ b/tpl/internal/go_templates/htmltemplate/exec_test.go @@ -716,7 +716,7 @@ func count(n int) chan string { } c := make(chan string) go func() { - for i := 0; i < n; i++ { + for i := range n { c <- "abcdefghijklmnop"[i : i+1] } close(c) @@ -1604,8 +1604,8 @@ func TestInterfaceValues(t *testing.T) { "Nil": nil, "Zero": 0, }) - if strings.HasPrefix(tt.out, "ERROR:") { - e := strings.TrimSpace(strings.TrimPrefix(tt.out, "ERROR:")) + if after, ok := strings.CutPrefix(tt.out, "ERROR:"); ok { + e := strings.TrimSpace(after) if err == nil || !strings.Contains(err.Error(), e) { t.Errorf("%s: Execute: %v, want error %q", tt.text, err, e) } @@ -1741,7 +1741,7 @@ func TestEscapeRace(t *testing.T) { t.Fatal(err) } const count = 20 - for i := 0; i < count; i++ { + for i := range count { _, err := tmpl.New(fmt.Sprintf("x%d.html", i)).Parse(`{{ template "templ.html" .}}`) if err != nil { t.Fatal(err) @@ -1749,11 +1749,11 @@ func TestEscapeRace(t *testing.T) { } var wg sync.WaitGroup - for i := 0; i < 10; i++ { + for range 10 { wg.Add(1) go func() { defer wg.Done() - for j := 0; j < count; j++ { + for j := range count { sub := tmpl.Lookup(fmt.Sprintf("x%d.html", j)) if err := sub.Execute(io.Discard, nil); err != nil { t.Error(err) diff --git a/tpl/internal/go_templates/testenv/testenv.go b/tpl/internal/go_templates/testenv/testenv.go index a46c8f5a8..e87ed1c9b 100644 --- a/tpl/internal/go_templates/testenv/testenv.go +++ b/tpl/internal/go_templates/testenv/testenv.go @@ -121,7 +121,7 @@ func GoToolPath(t testing.TB) string { // Add all environment variables that affect the Go command to test metadata. // Cached test results will be invalidate when these variables change. // See golang.org/issue/32285. - for _, envVar := range strings.Fields(cfg.KnownEnv) { + for envVar := range strings.FieldsSeq(cfg.KnownEnv) { os.Getenv(envVar) } return path @@ -415,7 +415,7 @@ func WriteImportcfg(t testing.TB, dstPath string, packageFiles map[string]string t.Fatalf("%v: %v\n%s", cmd, err, cmd.Stderr) } - for _, line := range strings.Split(string(out), "\n") { + for line := range strings.SplitSeq(string(out), "\n") { if line == "" { continue } diff --git a/tpl/internal/go_templates/testenv/testenv_test.go b/tpl/internal/go_templates/testenv/testenv_test.go index afe159531..87bcb1c71 100644 --- a/tpl/internal/go_templates/testenv/testenv_test.go +++ b/tpl/internal/go_templates/testenv/testenv_test.go @@ -172,8 +172,8 @@ func TestCleanCmdEnvPWD(t *testing.T) { cmd = testenv.CleanCmdEnv(cmd) for _, env := range cmd.Env { - if strings.HasPrefix(env, "PWD=") { - pwd := strings.TrimPrefix(env, "PWD=") + if after, ok := strings.CutPrefix(env, "PWD="); ok { + pwd := after if pwd != dir { t.Errorf("unexpected PWD: want %s, got %s", dir, pwd) } diff --git a/tpl/internal/go_templates/texttemplate/exec_test.go b/tpl/internal/go_templates/texttemplate/exec_test.go index 742e85605..b2cf3e7c6 100644 --- a/tpl/internal/go_templates/texttemplate/exec_test.go +++ b/tpl/internal/go_templates/texttemplate/exec_test.go @@ -804,7 +804,7 @@ func count(n int) chan string { } c := make(chan string) go func() { - for i := 0; i < n; i++ { + for i := range n { c <- "abcdefghijklmnop"[i : i+1] } close(c) @@ -1714,8 +1714,8 @@ func TestInterfaceValues(t *testing.T) { "Nil": nil, "Zero": 0, }) - if strings.HasPrefix(tt.out, "ERROR:") { - e := strings.TrimSpace(strings.TrimPrefix(tt.out, "ERROR:")) + if after, ok := strings.CutPrefix(tt.out, "ERROR:"); ok { + e := strings.TrimSpace(after) if err == nil || !strings.Contains(err.Error(), e) { t.Errorf("%s: Execute: %v, want error %q", tt.text, err, e) } @@ -1940,7 +1940,7 @@ func TestIssue39807(t *testing.T) { wg.Add(1) go func() { defer wg.Done() - for j := 0; j < numTemplates; j++ { + for range numTemplates { _, err := tplFoo.AddParseTree(tplBar.Name(), tplBar.Tree) if err != nil { t.Error(err) diff --git a/tpl/internal/go_templates/texttemplate/funcs.go b/tpl/internal/go_templates/texttemplate/funcs.go index 7d63cf8b7..5318798ad 100644 --- a/tpl/internal/go_templates/texttemplate/funcs.go +++ b/tpl/internal/go_templates/texttemplate/funcs.go @@ -8,6 +8,7 @@ import ( "errors" "fmt" "io" + "maps" "net/url" "reflect" "strings" @@ -103,9 +104,7 @@ func addValueFuncs(out map[string]reflect.Value, in FuncMap) { // addFuncs adds to values the functions in funcs. It does no checking of the input - // call addValueFuncs first. func addFuncs(out, in FuncMap) { - for name, fn := range in { - out[name] = fn - } + maps.Copy(out, in) } // goodFunc reports whether the function or method has the right result signature. diff --git a/tpl/internal/go_templates/texttemplate/hugo_template.go b/tpl/internal/go_templates/texttemplate/hugo_template.go index 6b0e259c2..37cd234d8 100644 --- a/tpl/internal/go_templates/texttemplate/hugo_template.go +++ b/tpl/internal/go_templates/texttemplate/hugo_template.go @@ -405,7 +405,7 @@ func (s *state) evalCall(dot, fun reflect.Value, isBuiltin bool, node parse.Node } // Added for Hugo - for i := 0; i < len(first); i++ { + for i := range first { argv[i] = s.validateType(first[i], typ.In(i)) } diff --git a/tpl/internal/go_templates/texttemplate/parse/parse.go b/tpl/internal/go_templates/texttemplate/parse/parse.go index e05a33d6f..cc8a93360 100644 --- a/tpl/internal/go_templates/texttemplate/parse/parse.go +++ b/tpl/internal/go_templates/texttemplate/parse/parse.go @@ -12,6 +12,7 @@ import ( "bytes" "fmt" "runtime" + "slices" "strconv" "strings" ) @@ -821,10 +822,8 @@ func (t *Tree) popVars(n int) { // variable is not defined. func (t *Tree) useVar(pos Pos, name string) Node { v := t.newVariable(pos, name) - for _, varName := range t.vars { - if varName == v.Ident[0] { - return v - } + if slices.Contains(t.vars, v.Ident[0]) { + return v } t.errorf("undefined variable %q", v.Ident[0]) return nil diff --git a/tpl/partials/partials_integration_test.go b/tpl/partials/partials_integration_test.go index a77b025ed..048ccbf90 100644 --- a/tpl/partials/partials_integration_test.go +++ b/tpl/partials/partials_integration_test.go @@ -243,9 +243,7 @@ ABCDE builders[i] = hugolib.NewIntegrationTestBuilder(cfg) } - b.ResetTimer() - - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { builders[i].Build() } } diff --git a/tpl/strings/strings.go b/tpl/strings/strings.go index eb5aee3cb..f80fdd98b 100644 --- a/tpl/strings/strings.go +++ b/tpl/strings/strings.go @@ -87,7 +87,7 @@ func (ns *Namespace) CountWords(s any) (int, error) { } counter := 0 - for _, word := range strings.Fields(tpl.StripHTML(ss)) { + for word := range strings.FieldsSeq(tpl.StripHTML(ss)) { runeCount := utf8.RuneCountInString(word) if len(word) == runeCount { counter++ diff --git a/tpl/strings/truncate_test.go b/tpl/strings/truncate_test.go index 633257456..6a4db6c4a 100644 --- a/tpl/strings/truncate_test.go +++ b/tpl/strings/truncate_test.go @@ -114,13 +114,13 @@ func TestTruncate(t *testing.T) { func BenchmarkTruncate(b *testing.B) { b.Run("Plain text", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { ns.Truncate(10, "I am a test sentence") } }) b.Run("With link", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { ns.Truncate(10, "I have a Markdown link inside") } }) diff --git a/tpl/time/time_test.go b/tpl/time/time_test.go index 622f1b12d..a0989d78f 100644 --- a/tpl/time/time_test.go +++ b/tpl/time/time_test.go @@ -248,7 +248,7 @@ func BenchmarkInWithCaching(b *testing.B) { ns := time.New(htime.NewTimeFormatter(translators.GetTranslator("en")), gtime.UTC, bb.H.Deps) - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { timeZoneName := timeZoneNames[i%len(timeZoneNames)] _, err := ns.In(timeZoneName, gtime.Now()) if err != nil { @@ -258,7 +258,7 @@ func BenchmarkInWithCaching(b *testing.B) { } func BenchmarkInWithoutCaching(b *testing.B) { - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { timeZoneName := timeZoneNames[i%len(timeZoneNames)] location, err := gtime.LoadLocation(timeZoneName) if err != nil { diff --git a/tpl/tplimpl/templatedescriptor_test.go b/tpl/tplimpl/templatedescriptor_test.go index ed8763c87..986e05481 100644 --- a/tpl/tplimpl/templatedescriptor_test.go +++ b/tpl/tplimpl/templatedescriptor_test.go @@ -95,8 +95,7 @@ func BenchmarkCompareDescriptors(b *testing.B) { }, } - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { for _, pair := range pairs { _ = dh.compareDescriptors(CategoryLayout, pair.d1, pair.d2, nil, nil) } diff --git a/tpl/tplimpl/templatestore_integration_test.go b/tpl/tplimpl/templatestore_integration_test.go index 90fc9614b..3dcc70103 100644 --- a/tpl/tplimpl/templatestore_integration_test.go +++ b/tpl/tplimpl/templatestore_integration_test.go @@ -740,7 +740,7 @@ baseof: {{ block "main" . }}{{ end }} Category: tplimpl.CategoryLayout, Desc: tplimpl.TemplateDescriptor{Kind: kinds.KindPage, LayoutFromTemplate: "single", OutputFormat: "html"}, } - for i := 0; i < b.N; i++ { + for b.Loop() { store.LookupPagesLayout(q) } }) @@ -751,7 +751,7 @@ baseof: {{ block "main" . }}{{ end }} Category: tplimpl.CategoryLayout, Desc: tplimpl.TemplateDescriptor{Kind: kinds.KindPage, LayoutFromTemplate: "single", OutputFormat: "html"}, } - for i := 0; i < b.N; i++ { + for b.Loop() { store.LookupPagesLayout(q) } }) @@ -761,8 +761,7 @@ func BenchmarkNewTemplateStore(b *testing.B) { bb := hugolib.Test(b, newSetupTestSites) store := bb.H.TemplateStore - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { newStore, err := store.NewFromOpts() if err != nil { b.Fatal(err) @@ -1416,8 +1415,7 @@ p3 p := bb.H.Sites[0].RegularPages()[0] bb.Assert(p, qt.Not(qt.IsNil)) - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { err := store.ExecuteWithContext(context.Background(), ti, io.Discard, p) if err != nil { b.Fatal(err) @@ -1440,7 +1438,7 @@ disableKinds = ["taxonomy", "term", "home"] store := bb.H.TemplateStore - for i := 0; i < b.N; i++ { + for b.Loop() { fi := store.LookupPartial("p3.html") if fi == nil { b.Fatal("not found") @@ -1486,14 +1484,14 @@ s2. b.Run("toplevelpage", func(b *testing.B) { toplevelpage, _ := bb.H.Sites[0].GetPage("/toplevelpage") - for i := 0; i < b.N; i++ { + for b.Loop() { runOne(toplevelpage) } }) b.Run("nestedpage", func(b *testing.B) { toplevelpage, _ := bb.H.Sites[0].GetPage("/a/b/c/nested") - for i := 0; i < b.N; i++ { + for b.Loop() { runOne(toplevelpage) } }) diff --git a/tpl/transform/unmarshal_test.go b/tpl/transform/unmarshal_test.go index 934b29ee1..3d60dcda7 100644 --- a/tpl/transform/unmarshal_test.go +++ b/tpl/transform/unmarshal_test.go @@ -206,8 +206,7 @@ func BenchmarkUnmarshalString(b *testing.B) { ctx := context.Background() - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { result, err := ns.Unmarshal(ctx, jsons[rand.Intn(numJsons)]) if err != nil { b.Fatal(err) @@ -235,8 +234,7 @@ func BenchmarkUnmarshalResource(b *testing.B) { ctx := context.Background() - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { result, err := ns.Unmarshal(ctx, jsons[rand.Intn(numJsons)]) if err != nil { b.Fatal(err) diff --git a/transform/livereloadinject/livereloadinject_test.go b/transform/livereloadinject/livereloadinject_test.go index 5475fc5b6..843346106 100644 --- a/transform/livereloadinject/livereloadinject_test.go +++ b/transform/livereloadinject/livereloadinject_test.go @@ -136,8 +136,7 @@ func BenchmarkLiveReloadInject(b *testing.B) { } tr := transform.New(New(lrurl)) - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { in.Seek(0, 0) tr.Apply(io.Discard, in) } diff --git a/transform/urlreplacers/absurlreplacer_test.go b/transform/urlreplacers/absurlreplacer_test.go index f95ee4fc1..b8652afed 100644 --- a/transform/urlreplacers/absurlreplacer_test.go +++ b/transform/urlreplacers/absurlreplacer_test.go @@ -125,8 +125,7 @@ var ( func BenchmarkAbsURL(b *testing.B) { tr := transform.New(NewAbsURLTransformer(testBaseURL)) - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { apply(b.Errorf, tr, absURLlBenchTests) } } @@ -134,8 +133,7 @@ func BenchmarkAbsURL(b *testing.B) { func BenchmarkAbsURLSrcset(b *testing.B) { tr := transform.New(NewAbsURLTransformer(testBaseURL)) - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { apply(b.Errorf, tr, srcsetTests) } } @@ -143,8 +141,7 @@ func BenchmarkAbsURLSrcset(b *testing.B) { func BenchmarkXMLAbsURLSrcset(b *testing.B) { tr := transform.New(NewAbsURLInXMLTransformer(testBaseURL)) - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { apply(b.Errorf, tr, srcsetXMLTests) } } @@ -191,8 +188,7 @@ func TestAbsXMLURLSrcSet(t *testing.T) { func BenchmarkXMLAbsURL(b *testing.B) { tr := transform.New(NewAbsURLInXMLTransformer(testBaseURL)) - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { apply(b.Errorf, tr, xmlAbsURLBenchTests) } } diff --git a/watcher/filenotify/poller_test.go b/watcher/filenotify/poller_test.go index 77feb459d..39c92f01e 100644 --- a/watcher/filenotify/poller_test.go +++ b/watcher/filenotify/poller_test.go @@ -185,7 +185,7 @@ func TestCheckChange(t *testing.T) { func BenchmarkPoller(b *testing.B) { runBench := func(b *testing.B, item *itemToWatch) { b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { evs, err := item.checkForChanges() if err != nil { b.Fatal(err) -- 2.39.5