Improve test speeds by removing useless method calls

This commit is contained in:
Jan-Lukas Else 2022-07-17 10:54:03 +02:00
parent 72ee1c833c
commit f0d3f1c84f
28 changed files with 66 additions and 113 deletions

View File

@ -50,10 +50,7 @@ func Test_webfinger(t *testing.T) {
}
app.cfg.Server.PublicAddress = "https://example.com"
t.Cleanup(app.cleanup)
_ = app.initConfig(false)
app.initComponents(false)
app.prepareWebfinger()

View File

@ -1,7 +0,0 @@
package main
func (a *goBlog) cleanup() {
if a.db != nil {
_ = a.db.close()
}
}

View File

@ -29,10 +29,10 @@ func Test_authMiddleware(t *testing.T) {
},
}
t.Cleanup(app.cleanup)
_ = app.initConfig(false)
app.initComponents(false)
app.initMarkdown()
app.initSessions()
_ = app.initTemplateStrings()
app.d = http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
_, _ = rw.Write([]byte("ABC Test"))

View File

@ -35,10 +35,7 @@ func Test_blogroll(t *testing.T) {
},
}
t.Cleanup(app.cleanup)
_ = app.initConfig(false)
app.initComponents(false)
fc.setFakeResponse(http.StatusOK, `
<opml version="2.0">

View File

@ -32,10 +32,11 @@ func Test_blogStats(t *testing.T) {
}
app.cfg.DefaultBlog = "en"
t.Cleanup(app.cleanup)
_ = app.initConfig(false)
app.initComponents(false)
_ = app.initCache()
app.initMarkdown()
_ = app.initTemplateStrings()
app.initSessions()
// Insert post

View File

@ -23,10 +23,10 @@ func Test_captchaMiddleware(t *testing.T) {
cfg: createDefaultTestConfig(t),
}
t.Cleanup(app.cleanup)
_ = app.initConfig(false)
app.initComponents(false)
app.initMarkdown()
app.initSessions()
_ = app.initTemplateStrings()
app.d = alice.New(app.checkIsCaptcha, app.captchaMiddleware).ThenFunc(func(rw http.ResponseWriter, r *http.Request) {
_, _ = rw.Write([]byte("ABC Test"))

View File

@ -29,11 +29,10 @@ func Test_comments(t *testing.T) {
}
app.cfg.DefaultBlog = "en"
t.Cleanup(app.cleanup)
err := app.initConfig(false)
require.NoError(t, err)
app.initComponents(false)
_ = app.initTemplateStrings()
app.initSessions()
t.Run("Successful comment", func(t *testing.T) {

View File

@ -43,10 +43,9 @@ func Test_contact(t *testing.T) {
}
app.cfg.DefaultBlog = "en"
t.Cleanup(app.cleanup)
_ = app.initConfig(false)
app.initComponents(false)
app.initSessions()
_ = app.initTemplateStrings()
// Make contact form request
rec := httptest.NewRecorder()

View File

@ -16,10 +16,9 @@ func Test_editorPreview(t *testing.T) {
cfg: createDefaultTestConfig(t),
}
t.Cleanup(app.cleanup)
_ = app.initConfig(false)
app.initComponents(false)
app.initMarkdown()
_ = app.initTemplateStrings()
h := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
app.serveEditorPreview(rw, r.WithContext(context.WithValue(r.Context(), blogKey, "default")))

View File

@ -15,10 +15,9 @@ func Test_errors(t *testing.T) {
cfg: createDefaultTestConfig(t),
}
t.Cleanup(app.cleanup)
_ = app.initConfig(false)
app.initComponents(false)
app.initMarkdown()
app.initSessions()
t.Run("Test 404, no HTML", func(t *testing.T) {
h := http.HandlerFunc(app.serve404)

View File

@ -16,10 +16,10 @@ func Test_feeds(t *testing.T) {
cfg: createDefaultTestConfig(t),
}
t.Cleanup(app.cleanup)
_ = app.initConfig(false)
app.initComponents(false)
app.initMarkdown()
_ = app.initCache()
app.initSessions()
app.d = app.buildRouter()
handlerClient := newHandlerClient(app.d)

View File

@ -21,10 +21,7 @@ func Test_geoTrack(t *testing.T) {
},
}
t.Cleanup(app.cleanup)
_ = app.initConfig(false)
app.initComponents(false)
// First test (just with track)

View File

@ -17,10 +17,7 @@ func Test_geo(t *testing.T) {
cfg: createDefaultTestConfig(t),
}
t.Cleanup(app.cleanup)
_ = app.initConfig(false)
app.initComponents(false)
p := &post{
Blog: "en",

View File

@ -20,10 +20,9 @@ func Test_indexNow(t *testing.T) {
}
app.cfg.IndexNow = &configIndexNow{Enabled: true}
t.Cleanup(app.cleanup)
_ = app.initConfig(false)
app.initComponents(false)
_ = app.initCache()
app.initIndexNow()
// Create http router
app.d = app.buildRouter()

View File

@ -35,10 +35,11 @@ func Test_indieAuthServer(t *testing.T) {
}
app.cfg.Cache.Enable = false
t.Cleanup(app.cleanup)
_ = app.initConfig(false)
app.initComponents(false)
app.initIndieAuth()
_ = app.initCache()
app.initSessions()
_ = app.initTemplateStrings()
app.d = app.buildRouter()

View File

@ -17,10 +17,9 @@ func Test_checkIndieAuth(t *testing.T) {
cfg: createDefaultTestConfig(t),
}
t.Cleanup(app.cleanup)
_ = app.initConfig(false)
app.initComponents(false)
app.initMarkdown()
app.initSessions()
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()

17
main.go
View File

@ -153,7 +153,7 @@ func main() {
}
// Initialize components
app.initComponents(true)
app.initComponents()
// Start cron hooks
app.startHourlyHooks()
@ -169,12 +169,11 @@ func main() {
app.shutdown.Wait()
}
func (app *goBlog) initComponents(logging bool) {
func (app *goBlog) initComponents() {
var err error
// Log start
if logging {
log.Println("Initialize components...")
}
log.Println("Initialize components...")
app.initMarkdown()
if err = app.initTemplateAssets(); err != nil { // Needs minify
app.logErrAndQuit("Failed to init template assets:", err.Error())
@ -209,10 +208,8 @@ func (app *goBlog) initComponents(logging bool) {
app.startPostsScheduler()
app.initPostsDeleter()
app.initIndexNow()
// Log finish
if logging {
log.Println("Initialized components")
}
log.Println("Initialized components")
}
func (a *goBlog) logErrAndQuit(v ...any) {

View File

@ -15,10 +15,10 @@ func Test_micropubQuery(t *testing.T) {
cfg: createDefaultTestConfig(t),
}
t.Cleanup(app.cleanup)
_ = app.initConfig(false)
app.initComponents(false)
_ = app.initCache()
app.initMarkdown()
app.initSessions()
// Create a test post with tags
err := app.createPost(&post{

View File

@ -18,10 +18,7 @@ func Test_ntfySending(t *testing.T) {
httpClient: fakeClient.Client,
}
t.Cleanup(app.cleanup)
_ = app.initConfig(false)
app.initComponents(false)
t.Run("Default", func(t *testing.T) {
app.cfg.Notifications = &configNotifications{

View File

@ -25,10 +25,9 @@ func Test_postsDb(t *testing.T) {
},
}
t.Cleanup(app.cleanup)
_ = app.initConfig(false)
app.initComponents(false)
app.initMarkdown()
_ = app.initCache()
now := toLocalSafe(time.Now().String())
nowPlus1Hour := toLocalSafe(time.Now().Add(1 * time.Hour).String())
@ -384,10 +383,9 @@ func Test_postDeletesParams(t *testing.T) {
cfg: createDefaultTestConfig(t),
}
t.Cleanup(app.cleanup)
_ = app.initConfig(false)
app.initComponents(false)
app.initMarkdown()
_ = app.initCache()
err := app.createPost(&post{
Path: "/test/abc",

View File

@ -12,10 +12,8 @@ func Test_checkDeletedPosts(t *testing.T) {
cfg: createDefaultTestConfig(t),
}
t.Cleanup(app.cleanup)
_ = app.initConfig(false)
app.initComponents(false)
_ = app.initCache()
// Create a post
err := app.createPost(&post{

View File

@ -22,10 +22,8 @@ func Test_postsScheduler(t *testing.T) {
},
}
t.Cleanup(app.cleanup)
_ = app.initConfig(false)
app.initComponents(false)
_ = app.initCache()
err := app.db.savePost(&post{
Path: "/test/abc",

View File

@ -17,10 +17,11 @@ func Test_serveDate(t *testing.T) {
cfg: createDefaultTestConfig(t),
}
t.Cleanup(app.cleanup)
_ = app.initConfig(false)
app.initComponents(false)
app.initMarkdown()
_ = app.initCache()
app.initSessions()
_ = app.initTemplateStrings()
app.d = app.buildRouter()
@ -108,10 +109,11 @@ func Test_servePost(t *testing.T) {
Password: "test",
})
t.Cleanup(app.cleanup)
_ = app.initConfig(false)
app.initComponents(false)
app.initMarkdown()
_ = app.initCache()
app.initSessions()
_ = app.initTemplateStrings()
app.d = app.buildRouter()

View File

@ -36,10 +36,9 @@ func Test_privateMode(t *testing.T) {
},
}
t.Cleanup(app.cleanup)
_ = app.initConfig(false)
app.initComponents(false)
app.initSessions()
_ = app.initTemplateStrings()
handler := alice.New(middleware.WithValue(blogKey, "en"), app.privateModeHandler).ThenFunc(func(rw http.ResponseWriter, r *http.Request) {
_, _ = rw.Write([]byte("Awesome"))

View File

@ -16,10 +16,8 @@ func Test_reactionsLowLevel(t *testing.T) {
cfg: createDefaultTestConfig(t),
}
t.Cleanup(app.cleanup)
_ = app.initConfig(false)
app.initComponents(false)
_ = app.initCache()
err := app.saveReaction("🖕", "/testpost")
assert.ErrorContains(t, err, "not allowed")
@ -99,10 +97,10 @@ func Test_reactionsHighLevel(t *testing.T) {
cfg: createDefaultTestConfig(t),
}
t.Cleanup(app.cleanup)
_ = app.initConfig(false)
app.initComponents(false)
app.initMarkdown()
app.initSessions()
_ = app.initCache()
// Send unsuccessful reaction
form := url.Values{

View File

@ -17,10 +17,9 @@ func Test_sitemap(t *testing.T) {
cfg: createDefaultTestConfig(t),
}
t.Cleanup(app.cleanup)
_ = app.initConfig(false)
app.initComponents(false)
app.initMarkdown()
_ = app.initCache()
app.d = app.buildRouter()

View File

@ -21,10 +21,8 @@ func Test_renderPostTax(t *testing.T) {
cfg: createDefaultTestConfig(t),
}
t.Cleanup(app.cleanup)
_ = app.initConfig(false)
app.initComponents(false)
app.initMarkdown()
p := &post{
Parameters: map[string][]string{
@ -50,10 +48,8 @@ func Test_renderOldContentWarning(t *testing.T) {
cfg: createDefaultTestConfig(t),
}
t.Cleanup(app.cleanup)
_ = app.initConfig(false)
app.initComponents(false)
_ = app.initTemplateStrings()
p := &post{
Published: "2018-01-01",
@ -79,10 +75,10 @@ func Test_renderInteractions(t *testing.T) {
}
app.cfg.Server.PublicAddress = "https://example.com"
t.Cleanup(app.cleanup)
_ = app.initConfig(false)
app.initComponents(false)
_ = app.initCache()
app.initMarkdown()
_ = app.initTemplateStrings()
app.d = app.buildRouter()
@ -150,10 +146,7 @@ func Test_renderAuthor(t *testing.T) {
app.cfg.User.Picture = "https://example.com/picture.jpg"
app.cfg.User.Name = "John Doe"
t.Cleanup(app.cleanup)
_ = app.initConfig(false)
app.initComponents(false)
buf := &bytes.Buffer{}
hb := newHtmlBuilder(buf)

View File

@ -19,10 +19,7 @@ func Test_webmentions(t *testing.T) {
},
}
t.Cleanup(app.cleanup)
_ = app.initConfig(false)
app.initComponents(false)
_ = app.db.insertWebmention(&mention{
Source: "https://example.net/test",