diff options
author | satotake <doublequotation@gmail.com> | 2021-05-23 17:42:01 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-23 10:42:01 +0200 |
commit | 785a31b5b84643f4769f9bd363599cbcce86f098 (patch) | |
tree | 06faf195e70dab43a035235173bc173aa5567735 /hugolib/menu_test.go | |
parent | bc1e05286a96d08ad02ad200d6a4076bb01c486e (diff) |
navigation: Cache and copy Menu for sorting
.Site.Menus is mutated when it is sorted for now and this causes concurrency problem (#7594)
In this patch, each related sort function copies Menu before sorting to prevent
race condition.
Pages already have such a sort and cache logic and this patch is identical to it.
Closes #7594
Diffstat (limited to 'hugolib/menu_test.go')
-rw-r--r-- | hugolib/menu_test.go | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/hugolib/menu_test.go b/hugolib/menu_test.go index 8717fa7db..33dd14911 100644 --- a/hugolib/menu_test.go +++ b/hugolib/menu_test.go @@ -105,6 +105,94 @@ Menu Main: {{ partial "menu.html" (dict "page" . "menu" "main") }}`, "/sect3/|Sect3s|Sect3s|0|-|-|") } +// related issue #7594 +func TestMenuSort(t *testing.T) { + b := newTestSitesBuilder(t).WithSimpleConfigFile() + + b.WithTemplatesAdded("index.html", ` +{{ range $k, $v := .Site.Menus.main }} +Default1|{{ $k }}|{{ $v.Weight }}|{{ $v.Name }}|{{ .URL }}|{{ $v.Page }}{{ end }} +{{ range $k, $v := .Site.Menus.main.ByWeight }} +ByWeight|{{ $k }}|{{ $v.Weight }}|{{ $v.Name }}|{{ .URL }}|{{ $v.Page }}{{ end }} +{{ range $k, $v := (.Site.Menus.main.ByWeight).Reverse }} +Reverse|{{ $k }}|{{ $v.Weight }}|{{ $v.Name }}|{{ .URL }}|{{ $v.Page }}{{ end }} +{{ range $k, $v := .Site.Menus.main }} +Default2|{{ $k }}|{{ $v.Weight }}|{{ $v.Name }}|{{ .URL }}|{{ $v.Page }}{{ end }} +{{ range $k, $v := .Site.Menus.main.ByWeight }} +ByWeight|{{ $k }}|{{ $v.Weight }}|{{ $v.Name }}|{{ .URL }}|{{ $v.Page }}{{ end }} +{{ range $k, $v := .Site.Menus.main }} +Default3|{{ $k }}|{{ $v.Weight }}|{{ $v.Name }}|{{ .URL }}|{{ $v.Page }}{{ end }} +`) + + b.WithContent("_index.md", ` +--- +title: Home +menu: + main: + weight: 100 +---`) + + b.WithContent("blog/A.md", ` +--- +title: "A" +menu: + main: + weight: 10 +--- +`) + + b.WithContent("blog/B.md", ` +--- +title: "B" +menu: + main: + weight: 20 +--- +`) + b.WithContent("blog/C.md", ` +--- +title: "C" +menu: + main: + weight: 30 +--- +`) + + b.Build(BuildCfg{}) + + b.AssertFileContent("public/index.html", + `Default1|0|10|A|/blog/a/|Page(/blog/A.md) + Default1|1|20|B|/blog/b/|Page(/blog/B.md) + Default1|2|30|C|/blog/c/|Page(/blog/C.md) + Default1|3|100|Home|/|Page(/_index.md) + + ByWeight|0|10|A|/blog/a/|Page(/blog/A.md) + ByWeight|1|20|B|/blog/b/|Page(/blog/B.md) + ByWeight|2|30|C|/blog/c/|Page(/blog/C.md) + ByWeight|3|100|Home|/|Page(/_index.md) + + Reverse|0|100|Home|/|Page(/_index.md) + Reverse|1|30|C|/blog/c/|Page(/blog/C.md) + Reverse|2|20|B|/blog/b/|Page(/blog/B.md) + Reverse|3|10|A|/blog/a/|Page(/blog/A.md) + + Default2|0|10|A|/blog/a/|Page(/blog/A.md) + Default2|1|20|B|/blog/b/|Page(/blog/B.md) + Default2|2|30|C|/blog/c/|Page(/blog/C.md) + Default2|3|100|Home|/|Page(/_index.md) + + ByWeight|0|10|A|/blog/a/|Page(/blog/A.md) + ByWeight|1|20|B|/blog/b/|Page(/blog/B.md) + ByWeight|2|30|C|/blog/c/|Page(/blog/C.md) + ByWeight|3|100|Home|/|Page(/_index.md) + + Default3|0|10|A|/blog/a/|Page(/blog/A.md) + Default3|1|20|B|/blog/b/|Page(/blog/B.md) + Default3|2|30|C|/blog/c/|Page(/blog/C.md) + Default3|3|100|Home|/|Page(/_index.md)`, + ) +} + func TestMenuFrontMatter(t *testing.T) { b := newTestSitesBuilder(t).WithSimpleConfigFile() |