Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package frontend | 5 package frontend |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "flag" | 8 "flag" |
| 9 "fmt" | 9 "fmt" |
| 10 "io/ioutil" | 10 "io/ioutil" |
| 11 "net/http" | 11 "net/http" |
| 12 "net/url" | 12 "net/url" |
| 13 "path/filepath" | 13 "path/filepath" |
| 14 "reflect" | |
| 15 "regexp" | 14 "regexp" |
| 16 "strings" | 15 "strings" |
| 17 "testing" | 16 "testing" |
| 18 "time" | 17 "time" |
| 19 | 18 |
| 19 . "github.com/smartystreets/goconvey/convey" | |
|
nodir
2017/03/17 20:47:58
dot imports should be in the last section, even if
hinoka
2017/03/17 22:04:55
Done.
| |
| 20 "golang.org/x/net/context" | |
| 21 | |
| 20 "github.com/luci/gae/impl/memory" | 22 "github.com/luci/gae/impl/memory" |
| 21 "github.com/luci/luci-go/common/clock/testclock" | 23 "github.com/luci/luci-go/common/clock/testclock" |
| 22 "github.com/luci/luci-go/milo/appengine/buildbot" | 24 "github.com/luci/luci-go/milo/appengine/buildbot" |
| 23 » "github.com/luci/luci-go/milo/appengine/settings" | 25 » "github.com/luci/luci-go/milo/appengine/common" |
| 24 "github.com/luci/luci-go/milo/appengine/swarming" | 26 "github.com/luci/luci-go/milo/appengine/swarming" |
| 25 "github.com/luci/luci-go/server/auth" | 27 "github.com/luci/luci-go/server/auth" |
| 26 "github.com/luci/luci-go/server/auth/identity" | 28 "github.com/luci/luci-go/server/auth/identity" |
| 27 » luciSettings "github.com/luci/luci-go/server/settings" | 29 » "github.com/luci/luci-go/server/settings" |
| 28 » . "github.com/smartystreets/goconvey/convey" | 30 » "github.com/luci/luci-go/server/templates" |
| 29 » "golang.org/x/net/context" | |
| 30 ) | 31 ) |
| 31 | 32 |
| 33 type testPackage struct { | |
| 34 Data func() []common.TestBundle | |
| 35 DisplayName string | |
| 36 TemplateName string | |
| 37 } | |
| 38 | |
| 32 var ( | 39 var ( |
| 33 » allHandlers = []settings.TestableHandler{ | 40 » allPackages = []testPackage{ |
| 34 » » settings.TestableSettings{}, | 41 » » {buildbot.BuildTestData, "buildbot.build", "build.html"}, |
| 35 » » buildbot.TestableBuild{}, | 42 » » {buildbot.BuilderTestData, "buildbot.builder", "builder.html"}, |
| 36 » » buildbot.TestableBuilder{}, | 43 » » {swarming.BuildTestData, "swarming.build", "build.html"}, |
| 37 » » swarming.TestableBuild{}, | 44 » » {swarming.LogTestData, "swarming.log", "log.html"}, |
| 38 » » swarming.TestableLog{}, | 45 » » {frontpageTestData, "frontpage", "frontpage.html"}, |
| 39 » » testableFrontpage{}, | |
| 40 } | 46 } |
| 41 ) | 47 ) |
| 42 | 48 |
| 43 var generate = flag.Bool( | 49 var generate = flag.Bool( |
| 44 "test.generate", false, "Generate expectations instead of running tests. ") | 50 "test.generate", false, "Generate expectations instead of running tests. ") |
| 45 | 51 |
| 46 func expectFileName(name string) string { | 52 func expectFileName(name string) string { |
| 47 name = strings.Replace(name, " ", "_", -1) | 53 name = strings.Replace(name, " ", "_", -1) |
| 48 name = strings.Replace(name, "/", "_", -1) | 54 name = strings.Replace(name, "/", "_", -1) |
| 49 name = strings.Replace(name, ":", "-", -1) | 55 name = strings.Replace(name, ":", "-", -1) |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 94 func TestPages(t *testing.T) { | 100 func TestPages(t *testing.T) { |
| 95 fixZeroDurationRE := regexp.MustCompile(`(Running for:|waiting) 0s?`) | 101 fixZeroDurationRE := regexp.MustCompile(`(Running for:|waiting) 0s?`) |
| 96 fixZeroDuration := func(text string) string { | 102 fixZeroDuration := func(text string) string { |
| 97 return fixZeroDurationRE.ReplaceAllLiteralString(text, "[ZERO DU RATION]") | 103 return fixZeroDurationRE.ReplaceAllLiteralString(text, "[ZERO DU RATION]") |
| 98 } | 104 } |
| 99 | 105 |
| 100 Convey("Testing basic rendering.", t, func() { | 106 Convey("Testing basic rendering.", t, func() { |
| 101 // Load all the bundles. | 107 // Load all the bundles. |
| 102 c := context.Background() | 108 c := context.Background() |
| 103 c = memory.Use(c) | 109 c = memory.Use(c) |
| 104 » » c = settings.WithRequest(c, &http.Request{URL: &url.URL{Path: "/ foobar"}}) | 110 » » c = common.WithRequest(c, &http.Request{URL: &url.URL{Path: "/fo obar"}}) |
| 105 c, _ = testclock.UseTime(c, testclock.TestTimeUTC) | 111 c, _ = testclock.UseTime(c, testclock.TestTimeUTC) |
| 106 a := auth.Authenticator{fakeOAuthMethod{"some_client_id"}} | 112 a := auth.Authenticator{fakeOAuthMethod{"some_client_id"}} |
| 107 c = auth.SetAuthenticator(c, a) | 113 c = auth.SetAuthenticator(c, a) |
| 108 » » c = luciSettings.Use(c, luciSettings.New(&luciSettings.MemorySto rage{Expiration: time.Second})) | 114 » » c = settings.Use(c, settings.New(&settings.MemoryStorage{Expirat ion: time.Second})) |
| 109 » » err := luciSettings.Set(c, "analytics", &analyticsSettings{"UA-1 2345-01"}, "", "") | 115 » » err := settings.Set(c, "analytics", &analyticsSettings{"UA-12345 -01"}, "", "") |
| 110 So(err, ShouldBeNil) | 116 So(err, ShouldBeNil) |
| 111 » » for _, nb := range settings.GetTemplateBundles() { | 117 » » c = templates.Use(c, common.GetTemplateBundle()) |
| 112 » » » Convey(fmt.Sprintf("Testing theme %q", nb.Name), func() { | 118 » » for _, p := range allPackages { |
| 113 » » » » err := nb.Bundle.EnsureLoaded(c) | 119 » » » Convey(fmt.Sprintf("Testing handler %q", p.DisplayName), func() { |
| 114 » » » » So(err, ShouldBeNil) | 120 » » » » for _, b := range p.Data() { |
| 115 » » » » for _, h := range allHandlers { | 121 » » » » » Convey(fmt.Sprintf("Testing: %q", b.Desc ription), func() { |
| 116 » » » » » hName := reflect.TypeOf(h).String() | 122 » » » » » » args := b.Data |
| 117 » » » » » Convey(fmt.Sprintf("Testing handler %q", hName), func() { | 123 » » » » » » // This is not a path, but a fil e key, should always be "/". |
| 118 » » » » » » for _, b := range h.TestData() { | 124 » » » » » » tmplName := "pages/" + p.Templat eName |
| 119 » » » » » » » Convey(fmt.Sprintf("Test ing: %q", b.Description), func() { | 125 » » » » » » buf, err := templates.Render(c, tmplName, args) |
| 120 » » » » » » » » args := b.Data | 126 » » » » » » So(err, ShouldBeNil) |
| 121 » » » » » » » » // This is not a path, but a file key, should always be "/". | 127 » » » » » » fname := fmt.Sprintf( |
| 122 » » » » » » » » tmplName := fmt. Sprintf( | 128 » » » » » » » "%s-%s.html", p.DisplayN ame, b.Description) |
| 123 » » » » » » » » » "pages/% s", h.GetTemplateName(*nb.Theme)) | 129 » » » » » » if *generate { |
| 124 » » » » » » » » buf, err := nb.B undle.Render(c, tmplName, args) | 130 » » » » » » » mustWrite(fname, buf) |
| 125 » » » » » » » » So(err, ShouldBe Nil) | 131 » » » » » » } else { |
| 126 » » » » » » » » fname := fmt.Spr intf( | 132 » » » » » » » localBuf, err := load(fn ame) |
| 127 » » » » » » » » » "%s-%s-% s.html", nb.Name, hName, b.Description) | 133 » » » » » » » So(err, ShouldBeNil) |
| 128 » » » » » » » » if *generate { | 134 » » » » » » » So(fixZeroDuration(strin g(buf)), ShouldEqual, fixZeroDuration(string(localBuf))) |
| 129 » » » » » » » » » mustWrit e(fname, buf) | |
| 130 » » » » » » » » } else { | |
| 131 » » » » » » » » » localBuf , err := load(fname) | |
| 132 » » » » » » » » » So(err, ShouldBeNil) | |
| 133 » » » » » » » » » So(fixZe roDuration(string(buf)), ShouldEqual, fixZeroDuration(string(localBuf))) | |
| 134 » » » » » » » » } | |
| 135 » » » » » » » }) | |
| 136 } | 135 } |
| 137 }) | 136 }) |
| 138 } | 137 } |
| 139 }) | 138 }) |
| 140 } | 139 } |
| 141 }) | 140 }) |
| 142 } | 141 } |
| OLD | NEW |