From f0d3f1c84fd007d5bd1880bee4481e0abaf9fe2c Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Sun, 17 Jul 2022 10:54:03 +0200 Subject: [PATCH] Improve test speeds by removing useless method calls --- activityPub_test.go | 3 --- app_test.go | 7 ------- authentication_test.go | 6 +++--- blogroll_test.go | 3 --- blogstats_test.go | 7 ++++--- captcha_test.go | 6 +++--- comments_test.go | 5 ++--- contact_test.go | 5 ++--- editor_test.go | 5 ++--- errors_test.go | 5 ++--- feeds_test.go | 6 +++--- geoTrack_test.go | 3 --- geo_test.go | 3 --- indexnow_test.go | 5 ++--- indieAuthServer_test.go | 7 ++++--- indieAuth_test.go | 5 ++--- main.go | 17 +++++++---------- micropub_test.go | 6 +++--- ntfy_test.go | 3 --- postsDb_test.go | 10 ++++------ postsDeleter_test.go | 4 +--- postsScheduler_test.go | 4 +--- posts_test.go | 14 ++++++++------ privateMode_test.go | 5 ++--- reactions_test.go | 10 ++++------ sitemap_test.go | 5 ++--- ui_test.go | 17 +++++------------ webmention_test.go | 3 --- 28 files changed, 66 insertions(+), 113 deletions(-) delete mode 100644 app_test.go diff --git a/activityPub_test.go b/activityPub_test.go index ce286b5..4947e24 100644 --- a/activityPub_test.go +++ b/activityPub_test.go @@ -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() diff --git a/app_test.go b/app_test.go deleted file mode 100644 index 8e6b34b..0000000 --- a/app_test.go +++ /dev/null @@ -1,7 +0,0 @@ -package main - -func (a *goBlog) cleanup() { - if a.db != nil { - _ = a.db.close() - } -} diff --git a/authentication_test.go b/authentication_test.go index 457d0ac..691b432 100644 --- a/authentication_test.go +++ b/authentication_test.go @@ -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")) diff --git a/blogroll_test.go b/blogroll_test.go index 0ea64d7..50a3e13 100644 --- a/blogroll_test.go +++ b/blogroll_test.go @@ -35,10 +35,7 @@ func Test_blogroll(t *testing.T) { }, } - t.Cleanup(app.cleanup) - _ = app.initConfig(false) - app.initComponents(false) fc.setFakeResponse(http.StatusOK, ` diff --git a/blogstats_test.go b/blogstats_test.go index d09e361..fdd8b38 100644 --- a/blogstats_test.go +++ b/blogstats_test.go @@ -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 diff --git a/captcha_test.go b/captcha_test.go index 0252827..75830a0 100644 --- a/captcha_test.go +++ b/captcha_test.go @@ -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")) diff --git a/comments_test.go b/comments_test.go index ed49cb9..0459d81 100644 --- a/comments_test.go +++ b/comments_test.go @@ -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) { diff --git a/contact_test.go b/contact_test.go index 9656d18..583cdec 100644 --- a/contact_test.go +++ b/contact_test.go @@ -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() diff --git a/editor_test.go b/editor_test.go index 8de3912..b35135a 100644 --- a/editor_test.go +++ b/editor_test.go @@ -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"))) diff --git a/errors_test.go b/errors_test.go index 3c6c4c8..cb58c42 100644 --- a/errors_test.go +++ b/errors_test.go @@ -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) diff --git a/feeds_test.go b/feeds_test.go index b980d28..767826b 100644 --- a/feeds_test.go +++ b/feeds_test.go @@ -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) diff --git a/geoTrack_test.go b/geoTrack_test.go index 34bb162..1f4f416 100644 --- a/geoTrack_test.go +++ b/geoTrack_test.go @@ -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) diff --git a/geo_test.go b/geo_test.go index 9329c68..7a7bc5f 100644 --- a/geo_test.go +++ b/geo_test.go @@ -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", diff --git a/indexnow_test.go b/indexnow_test.go index e255a70..88d12b4 100644 --- a/indexnow_test.go +++ b/indexnow_test.go @@ -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() diff --git a/indieAuthServer_test.go b/indieAuthServer_test.go index d91880d..c85a216 100644 --- a/indieAuthServer_test.go +++ b/indieAuthServer_test.go @@ -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() diff --git a/indieAuth_test.go b/indieAuth_test.go index 9bd8510..8bed3af 100644 --- a/indieAuth_test.go +++ b/indieAuth_test.go @@ -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() diff --git a/main.go b/main.go index 5c8625d..65e8e6d 100644 --- a/main.go +++ b/main.go @@ -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) { diff --git a/micropub_test.go b/micropub_test.go index 5e799fb..8c73e17 100644 --- a/micropub_test.go +++ b/micropub_test.go @@ -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{ diff --git a/ntfy_test.go b/ntfy_test.go index efc2c2c..2a484f3 100644 --- a/ntfy_test.go +++ b/ntfy_test.go @@ -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{ diff --git a/postsDb_test.go b/postsDb_test.go index 1ee2dc6..6a2476e 100644 --- a/postsDb_test.go +++ b/postsDb_test.go @@ -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", diff --git a/postsDeleter_test.go b/postsDeleter_test.go index 29bfc0c..0e534e5 100644 --- a/postsDeleter_test.go +++ b/postsDeleter_test.go @@ -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{ diff --git a/postsScheduler_test.go b/postsScheduler_test.go index b3a461e..ce6824c 100644 --- a/postsScheduler_test.go +++ b/postsScheduler_test.go @@ -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", diff --git a/posts_test.go b/posts_test.go index 35d4833..4421217 100644 --- a/posts_test.go +++ b/posts_test.go @@ -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() diff --git a/privateMode_test.go b/privateMode_test.go index a1edfd1..ad5b87e 100644 --- a/privateMode_test.go +++ b/privateMode_test.go @@ -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")) diff --git a/reactions_test.go b/reactions_test.go index 75048f6..d5db963 100644 --- a/reactions_test.go +++ b/reactions_test.go @@ -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{ diff --git a/sitemap_test.go b/sitemap_test.go index 323ca9e..459e97b 100644 --- a/sitemap_test.go +++ b/sitemap_test.go @@ -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() diff --git a/ui_test.go b/ui_test.go index a7634e5..9ad10c9 100644 --- a/ui_test.go +++ b/ui_test.go @@ -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) diff --git a/webmention_test.go b/webmention_test.go index 46e954f..71c98b6 100644 --- a/webmention_test.go +++ b/webmention_test.go @@ -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",