From 77159b4b9b58190f50201be48dc514d969cf063a Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Sun, 17 Apr 2016 19:55:38 +0200 Subject: [PATCH] Add some schemaless BaseURL tests See #2085 --- hugolib/site_test.go | 80 ++++++++++++++++++++++++-------------------- 1 file changed, 43 insertions(+), 37 deletions(-) diff --git a/hugolib/site_test.go b/hugolib/site_test.go index dfef52f4..f0c7a8e2 100644 --- a/hugolib/site_test.go +++ b/hugolib/site_test.go @@ -628,54 +628,60 @@ func TestAbsURLify(t *testing.T) { {filepath.FromSlash("sect/doc1.html"), []byte("link")}, {filepath.FromSlash("content/blue/doc2.html"), []byte("---\nf: t\n---\nmore content")}, } - for _, canonify := range []bool{true, false} { - viper.Set("CanonifyURLs", canonify) - viper.Set("BaseURL", "http://auth/bub") - s := &Site{ - Source: &source.InMemorySource{ByteSource: sources}, - targets: targetList{page: &target.PagePub{UglyURLs: true}}, - } - t.Logf("Rendering with BaseURL %q and CanonifyURLs set %v", viper.GetString("baseURL"), canonify) - s.initializeSiteInfo() + for _, baseURL := range []string{"http://auth/bub", "http://base", "//base"} { + for _, canonify := range []bool{true, false} { + viper.Set("CanonifyURLs", canonify) + viper.Set("BaseURL", baseURL) + s := &Site{ + Source: &source.InMemorySource{ByteSource: sources}, + targets: targetList{page: &target.PagePub{UglyURLs: true}}, + } + t.Logf("Rendering with BaseURL %q and CanonifyURLs set %v", viper.GetString("baseURL"), canonify) + s.initializeSiteInfo() - s.prepTemplates("blue/single.html", templateWithURLAbs) + s.prepTemplates("blue/single.html", templateWithURLAbs) - if err := s.createPages(); err != nil { - t.Fatalf("Unable to create pages: %s", err) - } + if err := s.createPages(); err != nil { + t.Fatalf("Unable to create pages: %s", err) + } - if err := s.buildSiteMeta(); err != nil { - t.Fatalf("Unable to build site metadata: %s", err) - } + if err := s.buildSiteMeta(); err != nil { + t.Fatalf("Unable to build site metadata: %s", err) + } - if err := s.renderPages(); err != nil { - t.Fatalf("Unable to render pages. %s", err) - } + if err := s.renderPages(); err != nil { + t.Fatalf("Unable to render pages. %s", err) + } - tests := []struct { - file, expected string - }{ - {"content/blue/doc2.html", "Going"}, - {"sect/doc1.html", "link"}, - } + tests := []struct { + file, expected string + }{ + {"content/blue/doc2.html", "Going"}, + {"sect/doc1.html", "link"}, + } - for _, test := range tests { + for _, test := range tests { - file, err := hugofs.Destination().Open(filepath.FromSlash(test.file)) - if err != nil { - t.Fatalf("Unable to locate rendered content: %s", test.file) - } + file, err := hugofs.Destination().Open(filepath.FromSlash(test.file)) + if err != nil { + t.Fatalf("Unable to locate rendered content: %s", test.file) + } - content := helpers.ReaderToString(file) + content := helpers.ReaderToString(file) - expected := test.expected + expected := test.expected - if !canonify { - expected = strings.Replace(expected, viper.GetString("baseurl"), "", -1) - } + if strings.Contains(expected, "%s") { + expected = fmt.Sprintf(expected, baseURL) + } + + if !canonify { + expected = strings.Replace(expected, baseURL, "", -1) + } - if content != expected { - t.Errorf("AbsURLify content expected:\n%q\ngot\n%q", expected, content) + if content != expected { + t.Errorf("AbsURLify with baseURL %q content expected:\n%q\ngot\n%q", baseURL, expected, content) + } } } } -- 2.30.2