diff options
author | spf13 <steve.francia@gmail.com> | 2015-05-20 02:21:21 -0400 |
---|---|---|
committer | spf13 <steve.francia@gmail.com> | 2015-05-20 02:21:21 -0400 |
commit | a584ff207b4ee951e4b25cf5cedf6d261ee1bc47 (patch) | |
tree | 4273e35cfd90493ed0d25d53450285b61c527418 /hugolib/menu_test.go | |
parent | 599d1b9786ddbaa9203aaca2c96ab33b40a583b1 (diff) |
Stop Viper from leaking across many of the tests (now tests pass regardless of order tested)
Diffstat (limited to 'hugolib/menu_test.go')
-rw-r--r-- | hugolib/menu_test.go | 165 |
1 files changed, 88 insertions, 77 deletions
diff --git a/hugolib/menu_test.go b/hugolib/menu_test.go index 4ec659f8d..8c4c26b5b 100644 --- a/hugolib/menu_test.go +++ b/hugolib/menu_test.go @@ -5,13 +5,15 @@ import ( "strings" "testing" + "path/filepath" + "github.com/BurntSushi/toml" + "github.com/kr/pretty" "github.com/spf13/afero" "github.com/spf13/hugo/hugofs" "github.com/spf13/hugo/source" "github.com/spf13/viper" "github.com/stretchr/testify/assert" - "path/filepath" ) const ( @@ -156,6 +158,8 @@ type testMenuState struct { // Issue 817 - identifier should trump everything func TestPageMenuWithIdentifier(t *testing.T) { + viper.Reset() + defer viper.Reset() toml := []source.ByteSource{ {"sect/doc1.md", tstCreateMenuPageWithIdentifierTOML("t1", "m1", "i1")}, @@ -176,13 +180,12 @@ func TestPageMenuWithIdentifier(t *testing.T) { func doTestPageMenuWithIdentifier(t *testing.T, menuPageSources []source.ByteSource) { - ts := setupMenuTests(t, menuPageSources) - defer resetMenuTestState(ts) + s := setupMenuTests(t, menuPageSources) - assert.Equal(t, 3, len(ts.site.Pages), "Not enough pages") + assert.Equal(t, 3, len(s.Pages), "Not enough pages") - me1 := ts.findTestMenuEntryByID("m1", "i1") - me2 := ts.findTestMenuEntryByID("m1", "i2") + me1 := findTestMenuEntryByID(s, "m1", "i1") + me2 := findTestMenuEntryByID(s, "m1", "i2") assert.NotNil(t, me1) assert.NotNil(t, me2) @@ -194,6 +197,9 @@ func doTestPageMenuWithIdentifier(t *testing.T, menuPageSources []source.ByteSou // Issue 817 contd - name should be second identifier in func TestPageMenuWithDuplicateName(t *testing.T) { + viper.Reset() + defer viper.Reset() + toml := []source.ByteSource{ {"sect/doc1.md", tstCreateMenuPageWithNameTOML("t1", "m1", "n1")}, {"sect/doc2.md", tstCreateMenuPageWithNameTOML("t1", "m1", "n2")}, @@ -212,13 +218,12 @@ func TestPageMenuWithDuplicateName(t *testing.T) { } func doTestPageMenuWithDuplicateName(t *testing.T, menuPageSources []source.ByteSource) { - ts := setupMenuTests(t, menuPageSources) - defer resetMenuTestState(ts) + s := setupMenuTests(t, menuPageSources) - assert.Equal(t, 3, len(ts.site.Pages), "Not enough pages") + assert.Equal(t, 3, len(s.Pages), "Not enough pages") - me1 := ts.findTestMenuEntryByName("m1", "n1") - me2 := ts.findTestMenuEntryByName("m1", "n2") + me1 := findTestMenuEntryByName(s, "m1", "n1") + me2 := findTestMenuEntryByName(s, "m1", "n2") assert.NotNil(t, me1) assert.NotNil(t, me2) @@ -229,19 +234,21 @@ func doTestPageMenuWithDuplicateName(t *testing.T, menuPageSources []source.Byte } func TestPageMenu(t *testing.T) { - ts := setupMenuTests(t, MENU_PAGE_SOURCES) - defer resetMenuTestState(ts) + viper.Reset() + defer viper.Reset() + + s := setupMenuTests(t, MENU_PAGE_SOURCES) - if len(ts.site.Pages) != 3 { - t.Fatalf("Posts not created, expected 3 got %d", len(ts.site.Pages)) + if len(s.Pages) != 3 { + t.Fatalf("Posts not created, expected 3 got %d", len(s.Pages)) } - first := ts.site.Pages[0] - second := ts.site.Pages[1] - third := ts.site.Pages[2] + first := s.Pages[0] + second := s.Pages[1] + third := s.Pages[2] - pOne := ts.findTestMenuEntryByName("p_one", "One") - pTwo := ts.findTestMenuEntryByID("p_two", "Two") + pOne := findTestMenuEntryByName(s, "p_one", "One") + pTwo := findTestMenuEntryByID(s, "p_two", "Two") for i, this := range []struct { menu string @@ -275,10 +282,12 @@ func TestPageMenu(t *testing.T) { // issue #888 func TestMenuWithHashInURL(t *testing.T) { - ts := setupMenuTests(t, MENU_PAGE_SOURCES) - defer resetMenuTestState(ts) + viper.Reset() + defer viper.Reset() + + s := setupMenuTests(t, MENU_PAGE_SOURCES) - me := ts.findTestMenuEntryByID("hash", "hash") + me := findTestMenuEntryByID(s, "hash", "hash") assert.NotNil(t, me) @@ -287,6 +296,9 @@ func TestMenuWithHashInURL(t *testing.T) { // issue #719 func TestMenuWithUnicodeURLs(t *testing.T) { + viper.Reset() + defer viper.Reset() + for _, uglyURLs := range []bool{true, false} { for _, canonifyURLs := range []bool{true, false} { doTestMenuWithUnicodeURLs(t, canonifyURLs, uglyURLs) @@ -298,10 +310,9 @@ func doTestMenuWithUnicodeURLs(t *testing.T, canonifyURLs, uglyURLs bool) { viper.Set("CanonifyURLs", canonifyURLs) viper.Set("UglyURLs", uglyURLs) - ts := setupMenuTests(t, MENU_PAGE_SOURCES) - defer resetMenuTestState(ts) + s := setupMenuTests(t, MENU_PAGE_SOURCES) - unicodeRussian := ts.findTestMenuEntryByID("unicode", "unicode-russian") + unicodeRussian := findTestMenuEntryByID(s, "unicode", "unicode-russian") expectedBase := "/%D0%BD%D0%BE%D0%B2%D0%BE%D1%81%D1%82%D0%B8-%D0%BF%D1%80%D0%BE%D0%B5%D0%BA%D1%82%D0%B0" @@ -321,31 +332,31 @@ func doTestMenuWithUnicodeURLs(t *testing.T, canonifyURLs, uglyURLs bool) { // Issue #1114 func TestSectionPagesMenu(t *testing.T) { + viper.Reset() + defer viper.Reset() + viper.Set("SectionPagesMenu", "spm") - defer viper.Set("SectionPagesMenu", "") - for _, canonify := range []bool{true, false} { - doTestSectionPagesMenu(canonify, t) - } + doTestSectionPagesMenu(true, t) + doTestSectionPagesMenu(false, t) } func doTestSectionPagesMenu(canonifyUrls bool, t *testing.T) { viper.Set("CanonifyURLs", canonifyUrls) - ts := setupMenuTests(t, MENU_PAGE_SECTIONS_SOURCES) - defer resetMenuTestState(ts) + s := setupMenuTests(t, MENU_PAGE_SECTIONS_SOURCES) - assert.Equal(t, 2, len(ts.site.Sections)) + assert.Equal(t, 2, len(s.Sections)) - firstSectionPages := ts.site.Sections["first"] + firstSectionPages := s.Sections["first"] assert.Equal(t, 2, len(firstSectionPages)) - secondSectionPages := ts.site.Sections["second-section"] + secondSectionPages := s.Sections["second-section"] assert.Equal(t, 1, len(secondSectionPages)) - nodeFirst := ts.site.newSectionListNode("first", firstSectionPages) - nodeSecond := ts.site.newSectionListNode("second-section", secondSectionPages) + nodeFirst := s.newSectionListNode("first", firstSectionPages) + nodeSecond := s.newSectionListNode("second-section", secondSectionPages) - firstSectionMenuEntry := ts.findTestMenuEntryByID("spm", "first") - secondSectionMenuEntry := ts.findTestMenuEntryByID("spm", "second-section") + firstSectionMenuEntry := findTestMenuEntryByID(s, "spm", "first") + secondSectionMenuEntry := findTestMenuEntryByID(s, "spm", "second-section") assert.NotNil(t, firstSectionMenuEntry) assert.NotNil(t, secondSectionMenuEntry) @@ -366,9 +377,11 @@ func doTestSectionPagesMenu(canonifyUrls bool, t *testing.T) { } func TestTaxonomyNodeMenu(t *testing.T) { + viper.Reset() + defer viper.Reset() + viper.Set("CanonifyURLs", true) - ts := setupMenuTests(t, MENU_PAGE_SOURCES) - defer resetMenuTestState(ts) + s := setupMenuTests(t, MENU_PAGE_SOURCES) for i, this := range []struct { menu string @@ -378,14 +391,14 @@ func TestTaxonomyNodeMenu(t *testing.T) { hasMenuCurrent bool }{ {"tax", taxRenderInfo{key: "key", singular: "one", plural: "two"}, - ts.findTestMenuEntryByID("tax", "1"), true, false}, + findTestMenuEntryByID(s, "tax", "1"), true, false}, {"tax", taxRenderInfo{key: "key", singular: "one", plural: "two"}, - ts.findTestMenuEntryByID("tax", "2"), true, false}, + findTestMenuEntryByID(s, "tax", "2"), true, false}, {"tax", taxRenderInfo{key: "key", singular: "one", plural: "two"}, &MenuEntry{Name: "Somewhere else", URL: "/somewhereelse"}, false, false}, } { - n, _ := ts.site.newTaxonomyNode(this.taxInfo) + n, _ := s.newTaxonomyNode(this.taxInfo) isMenuCurrent := n.IsMenuCurrent(this.menu, this.menuItem) hasMenuCurrent := n.HasMenuCurrent(this.menu, this.menuItem) @@ -400,7 +413,7 @@ func TestTaxonomyNodeMenu(t *testing.T) { } - menuEntryXML := ts.findTestMenuEntryByID("tax", "xml") + menuEntryXML := findTestMenuEntryByID(s, "tax", "xml") if strings.HasSuffix(menuEntryXML.URL, "/") { t.Error("RSS menu item should not be padded with trailing slash") @@ -408,10 +421,15 @@ func TestTaxonomyNodeMenu(t *testing.T) { } func TestHomeNodeMenu(t *testing.T) { - ts := setupMenuTests(t, MENU_PAGE_SOURCES) - defer resetMenuTestState(ts) + viper.Reset() + defer viper.Reset() + + viper.Set("CanonifyURLs", true) + viper.Set("UglyURLs", true) + + s := setupMenuTests(t, MENU_PAGE_SOURCES) - home := ts.site.newHomeNode() + home := s.newHomeNode() homeMenuEntry := &MenuEntry{Name: home.Title, URL: home.URL} for i, this := range []struct { @@ -423,20 +441,24 @@ func TestHomeNodeMenu(t *testing.T) { {"main", homeMenuEntry, true, false}, {"doesnotexist", homeMenuEntry, false, false}, {"main", &MenuEntry{Name: "Somewhere else", URL: "/somewhereelse"}, false, false}, - {"grandparent", ts.findTestMenuEntryByID("grandparent", "grandparentId"), false, false}, - {"grandparent", ts.findTestMenuEntryByID("grandparent", "parentId"), false, true}, - {"grandparent", ts.findTestMenuEntryByID("grandparent", "grandchildId"), true, false}, + {"grandparent", findTestMenuEntryByID(s, "grandparent", "grandparentId"), false, false}, + {"grandparent", findTestMenuEntryByID(s, "grandparent", "parentId"), false, true}, + {"grandparent", findTestMenuEntryByID(s, "grandparent", "grandchildId"), true, false}, } { isMenuCurrent := home.IsMenuCurrent(this.menu, this.menuItem) hasMenuCurrent := home.HasMenuCurrent(this.menu, this.menuItem) if isMenuCurrent != this.isMenuCurrent { - t.Errorf("[%d] Wrong result from IsMenuCurrent: %v", i, isMenuCurrent) + fmt.Println("isMenuCurrent", isMenuCurrent) + pretty.Println("this:", this) + t.Errorf("[%d] Wrong result from IsMenuCurrent: %v for %q", i, isMenuCurrent, this.menu) } if hasMenuCurrent != this.hasMenuCurrent { - t.Errorf("[%d] Wrong result for menuItem %v for HasMenuCurrent: %v", i, this.menuItem, hasMenuCurrent) + fmt.Println("hasMenuCurrent", hasMenuCurrent) + pretty.Println("this:", this) + t.Errorf("[%d] Wrong result for menu %q menuItem %v for HasMenuCurrent: %v", i, this.menu, this.menuItem, hasMenuCurrent) } } } @@ -444,16 +466,16 @@ func TestHomeNodeMenu(t *testing.T) { var testMenuIdentityMatcher = func(me *MenuEntry, id string) bool { return me.Identifier == id } var testMenuNameMatcher = func(me *MenuEntry, id string) bool { return me.Name == id } -func (ts testMenuState) findTestMenuEntryByID(mn string, id string) *MenuEntry { - return ts.findTestMenuEntry(mn, id, testMenuIdentityMatcher) +func findTestMenuEntryByID(s *Site, mn string, id string) *MenuEntry { + return findTestMenuEntry(s, mn, id, testMenuIdentityMatcher) } -func (ts testMenuState) findTestMenuEntryByName(mn string, id string) *MenuEntry { - return ts.findTestMenuEntry(mn, id, testMenuNameMatcher) +func findTestMenuEntryByName(s *Site, mn string, id string) *MenuEntry { + return findTestMenuEntry(s, mn, id, testMenuNameMatcher) } -func (ts testMenuState) findTestMenuEntry(mn string, id string, matcher func(me *MenuEntry, id string) bool) *MenuEntry { +func findTestMenuEntry(s *Site, mn string, id string, matcher func(me *MenuEntry, id string) bool) *MenuEntry { var found *MenuEntry - if menu, ok := ts.site.Menus[mn]; ok { + if menu, ok := s.Menus[mn]; ok { for _, me := range *menu { if matcher(me, id) { @@ -463,7 +485,7 @@ func (ts testMenuState) findTestMenuEntry(mn string, id string, matcher func(me found = me } - descendant := ts.findDescendantTestMenuEntry(me, id, matcher) + descendant := findDescendantTestMenuEntry(me, id, matcher) if descendant != nil { if found != nil { panic(fmt.Sprintf("Duplicate menu entry in menu %s with id/name %s", mn, id)) @@ -475,7 +497,7 @@ func (ts testMenuState) findTestMenuEntry(mn string, id string, matcher func(me return found } -func (ts testMenuState) findDescendantTestMenuEntry(parent *MenuEntry, id string, matcher func(me *MenuEntry, id string) bool) *MenuEntry { +func findDescendantTestMenuEntry(parent *MenuEntry, id string, matcher func(me *MenuEntry, id string) bool) *MenuEntry { var found *MenuEntry if parent.HasChildren() { for _, child := range parent.Children { @@ -487,7 +509,7 @@ func (ts testMenuState) findDescendantTestMenuEntry(parent *MenuEntry, id string found = child } - descendant := ts.findDescendantTestMenuEntry(child, id, matcher) + descendant := findDescendantTestMenuEntry(child, id, matcher) if descendant != nil { if found != nil { panic(fmt.Sprintf("Duplicate menu entry in menuitem %s with id/name %s", parent.KeyName(), id)) @@ -499,9 +521,7 @@ func (ts testMenuState) findDescendantTestMenuEntry(parent *MenuEntry, id string return found } -func getTestMenuState(s *Site, t *testing.T) *testMenuState { - menuState := &testMenuState{site: s, oldBaseURL: viper.Get("baseurl"), oldMenu: viper.Get("menu")} - +func setupTestMenuState(s *Site, t *testing.T) { menus, err := tomlToMap(CONF_MENU1) if err != nil { @@ -510,21 +530,14 @@ func getTestMenuState(s *Site, t *testing.T) *testMenuState { viper.Set("menu", menus["menu"]) viper.Set("baseurl", "http://foo.local/Zoo/") - - return menuState } -func setupMenuTests(t *testing.T, pageSources []source.ByteSource) *testMenuState { +func setupMenuTests(t *testing.T, pageSources []source.ByteSource) *Site { s := createTestSite(pageSources) - testState := getTestMenuState(s, t) + setupTestMenuState(s, t) testSiteSetup(s, t) - return testState -} - -func resetMenuTestState(state *testMenuState) { - viper.Set("menu", state.oldMenu) - viper.Set("baseurl", state.oldBaseURL) + return s } func createTestSite(pageSources []source.ByteSource) *Site { @@ -537,7 +550,6 @@ func createTestSite(pageSources []source.ByteSource) *Site { } func testSiteSetup(s *Site, t *testing.T) { - s.Menus = Menus{} s.initializeSiteInfo() @@ -548,7 +560,6 @@ func testSiteSetup(s *Site, t *testing.T) { if err := s.BuildSiteMeta(); err != nil { t.Fatalf("Unable to build site metadata: %s", err) } - } func tomlToMap(s string) (map[string]interface{}, error) { |