From b55a80f5b9d1a25c086a34b0522f233a5b52a9b5 Mon Sep 17 00:00:00 2001 From: Googol Lee Date: Tue, 22 Jul 2025 21:14:20 +0200 Subject: [PATCH] Improve `*TestMain` to handle the exit code. (#1258) --- api/database/migrations/migrations_test.go | 3 +-- api/graphql/auth/auth_test.go | 3 +-- api/graphql/models/actions/actions_test.go | 3 +-- api/graphql/models/models_test.go | 3 +-- api/routes/photos.go | 2 +- api/routes/routes_test.go | 3 +-- api/scanner/exif/exif_test.go | 3 +-- api/scanner/media_type/counterpart_test.go | 4 +-- api/scanner/media_type/magic_test.go | 6 ++--- api/scanner/media_type/media_type_test.go | 3 +-- .../periodic_scanner/periodic_scanner_test.go | 3 +-- .../cleanup_tasks/cleanup_media_test.go | 2 +- .../counterpart_files_task_test.go | 2 +- api/scanner/scanner_test.go | 3 +-- api/scanner/test_media/orient/ensure_test.go | 3 +-- api/test_utils/env.go | 7 +++-- api/test_utils/env_test.go | 15 +++++++++-- api/test_utils/integration_setup.go | 26 ++++++------------- api/utils/utils_test.go | 2 +- 19 files changed, 45 insertions(+), 51 deletions(-) diff --git a/api/database/migrations/migrations_test.go b/api/database/migrations/migrations_test.go index 737760b2..6371f024 100644 --- a/api/database/migrations/migrations_test.go +++ b/api/database/migrations/migrations_test.go @@ -1,12 +1,11 @@ package migrations_test import ( - "os" "testing" "github.com/photoview/photoview/api/test_utils" ) func TestMain(m *testing.M) { - os.Exit(test_utils.IntegrationTestRun(m)) + test_utils.IntegrationTestRun(m) } diff --git a/api/graphql/auth/auth_test.go b/api/graphql/auth/auth_test.go index 92e5970d..41eb00de 100644 --- a/api/graphql/auth/auth_test.go +++ b/api/graphql/auth/auth_test.go @@ -1,7 +1,6 @@ package auth_test import ( - "os" "testing" "github.com/photoview/photoview/api/graphql/auth" @@ -10,7 +9,7 @@ import ( ) func TestMain(m *testing.M) { - os.Exit(test_utils.UnitTestRun(m)) + test_utils.UnitTestRun(m) } func TestTokenFromBearer(t *testing.T) { diff --git a/api/graphql/models/actions/actions_test.go b/api/graphql/models/actions/actions_test.go index 7e6f46b2..7103384c 100644 --- a/api/graphql/models/actions/actions_test.go +++ b/api/graphql/models/actions/actions_test.go @@ -1,12 +1,11 @@ package actions_test import ( - "os" "testing" "github.com/photoview/photoview/api/test_utils" ) func TestMain(m *testing.M) { - os.Exit(test_utils.IntegrationTestRun(m)) + test_utils.IntegrationTestRun(m) } diff --git a/api/graphql/models/models_test.go b/api/graphql/models/models_test.go index 615307e2..bc20c815 100644 --- a/api/graphql/models/models_test.go +++ b/api/graphql/models/models_test.go @@ -1,12 +1,11 @@ package models_test import ( - "os" "testing" "github.com/photoview/photoview/api/test_utils" ) func TestMain(m *testing.M) { - os.Exit(test_utils.IntegrationTestRun(m)) + test_utils.IntegrationTestRun(m) } diff --git a/api/routes/photos.go b/api/routes/photos.go index e1b1fab8..7ba776c4 100644 --- a/api/routes/photos.go +++ b/api/routes/photos.go @@ -34,7 +34,7 @@ func RegisterPhotoRoutes(db *gorm.DB, router *mux.Router) { if success, response, status, err := authenticateMedia(media, db, r); !success { if err != nil { - log.Warn(r.Context(), "error authenticating photo", "error", err) + log.Warn(r.Context(), "Unauthorized access to photo", "reason", err) } w.WriteHeader(status) w.Write([]byte(response)) diff --git a/api/routes/routes_test.go b/api/routes/routes_test.go index 8201e496..8eccb45c 100644 --- a/api/routes/routes_test.go +++ b/api/routes/routes_test.go @@ -1,12 +1,11 @@ package routes_test import ( - "os" "testing" "github.com/photoview/photoview/api/test_utils" ) func TestMain(m *testing.M) { - os.Exit(test_utils.IntegrationTestRun(m)) + test_utils.IntegrationTestRun(m) } diff --git a/api/scanner/exif/exif_test.go b/api/scanner/exif/exif_test.go index a96640f1..8088b63c 100644 --- a/api/scanner/exif/exif_test.go +++ b/api/scanner/exif/exif_test.go @@ -2,7 +2,6 @@ package exif_test import ( "fmt" - "os" "path" "testing" "time" @@ -15,7 +14,7 @@ import ( ) func TestMain(m *testing.M) { - os.Exit(test_utils.IntegrationTestRun(m)) + test_utils.IntegrationTestRun(m) } func TestExifParsers(t *testing.T) { diff --git a/api/scanner/media_type/counterpart_test.go b/api/scanner/media_type/counterpart_test.go index 46fa2113..53223fa7 100644 --- a/api/scanner/media_type/counterpart_test.go +++ b/api/scanner/media_type/counterpart_test.go @@ -10,7 +10,7 @@ import ( ) func TestFindWebCounterpart(t *testing.T) { - mediaPath := test_utils.PathFromAPIRoot("./scanner/test_media/real_media") + mediaPath := test_utils.PathFromAPIRoot("scanner", "test_media", "real_media") tests := []struct { input string @@ -38,7 +38,7 @@ func TestFindWebCounterpart(t *testing.T) { } func TestFindRawCounterpart(t *testing.T) { - mediaPath := test_utils.PathFromAPIRoot("./scanner/test_media/real_media") + mediaPath := test_utils.PathFromAPIRoot("scanner", "test_media", "real_media") tests := []struct { input string diff --git a/api/scanner/media_type/magic_test.go b/api/scanner/media_type/magic_test.go index fe6ff39a..31040bea 100644 --- a/api/scanner/media_type/magic_test.go +++ b/api/scanner/media_type/magic_test.go @@ -11,7 +11,7 @@ import ( ) func TestMagic(t *testing.T) { - mediaPath := test_utils.PathFromAPIRoot("./scanner/test_media/real_media") + mediaPath := test_utils.PathFromAPIRoot("scanner", "test_media", "real_media") tests := []struct { filepath string @@ -69,7 +69,7 @@ func TestMagicNoInit(t *testing.T) { libmagic.err = nil }) - mediaPath := test_utils.PathFromAPIRoot("./scanner/test_media/real_media") + mediaPath := test_utils.PathFromAPIRoot("scanner", "test_media", "real_media") file := filepath.Join(mediaPath, "file.pdf") got := GetMediaType(file) @@ -79,7 +79,7 @@ func TestMagicNoInit(t *testing.T) { } func getMediaFiles() []string { - mediaPath := test_utils.PathFromAPIRoot("./scanner/test_media/real_media") + mediaPath := test_utils.PathFromAPIRoot("scanner", "test_media", "real_media") var files []string for _, f := range []string{ "file.pdf", diff --git a/api/scanner/media_type/media_type_test.go b/api/scanner/media_type/media_type_test.go index 45f68775..97e9ffff 100644 --- a/api/scanner/media_type/media_type_test.go +++ b/api/scanner/media_type/media_type_test.go @@ -1,14 +1,13 @@ package media_type import ( - "os" "testing" "github.com/photoview/photoview/api/test_utils" ) func TestMain(m *testing.M) { - os.Exit(test_utils.UnitTestRun(m)) + test_utils.UnitTestRun(m) } type boolImage bool diff --git a/api/scanner/periodic_scanner/periodic_scanner_test.go b/api/scanner/periodic_scanner/periodic_scanner_test.go index 04e6af1e..2dd48a42 100644 --- a/api/scanner/periodic_scanner/periodic_scanner_test.go +++ b/api/scanner/periodic_scanner/periodic_scanner_test.go @@ -2,7 +2,6 @@ package periodic_scanner import ( "errors" - "os" "sync" "testing" "time" @@ -24,7 +23,7 @@ func (m *MockScannerQueue) AddAllToQueue() error { } func TestMain(m *testing.M) { - os.Exit(test_utils.UnitTestRun(m)) + test_utils.UnitTestRun(m) } func resetPeriodicScanner() { diff --git a/api/scanner/scanner_tasks/cleanup_tasks/cleanup_media_test.go b/api/scanner/scanner_tasks/cleanup_tasks/cleanup_media_test.go index 5869882e..28bad483 100644 --- a/api/scanner/scanner_tasks/cleanup_tasks/cleanup_media_test.go +++ b/api/scanner/scanner_tasks/cleanup_tasks/cleanup_media_test.go @@ -14,7 +14,7 @@ import ( ) func TestMain(m *testing.M) { - os.Exit(test_utils.IntegrationTestRun(m)) + test_utils.IntegrationTestRun(m) } func TestCleanupMedia(t *testing.T) { diff --git a/api/scanner/scanner_tasks/processing_tasks/counterpart_files_task_test.go b/api/scanner/scanner_tasks/processing_tasks/counterpart_files_task_test.go index 0cb5a860..011a71f4 100644 --- a/api/scanner/scanner_tasks/processing_tasks/counterpart_files_task_test.go +++ b/api/scanner/scanner_tasks/processing_tasks/counterpart_files_task_test.go @@ -13,7 +13,7 @@ import ( ) func TestCounterpartFilesTaskMediaFound(t *testing.T) { - mediaPath := test_utils.PathFromAPIRoot("scanner/test_media/real_media") + mediaPath := test_utils.PathFromAPIRoot("scanner", "test_media", "real_media") tests := []struct { name string diff --git a/api/scanner/scanner_test.go b/api/scanner/scanner_test.go index adb06ef4..cfa9da96 100644 --- a/api/scanner/scanner_test.go +++ b/api/scanner/scanner_test.go @@ -2,7 +2,6 @@ package scanner_test import ( "fmt" - "os" "path/filepath" "slices" "strings" @@ -16,7 +15,7 @@ import ( ) func TestMain(m *testing.M) { - os.Exit(test_utils.IntegrationTestRun(m)) + test_utils.IntegrationTestRun(m) } func TestFullScan(t *testing.T) { diff --git a/api/scanner/test_media/orient/ensure_test.go b/api/scanner/test_media/orient/ensure_test.go index 4e34d488..dbf4bbab 100644 --- a/api/scanner/test_media/orient/ensure_test.go +++ b/api/scanner/test_media/orient/ensure_test.go @@ -1,7 +1,6 @@ package orient import ( - "os" "strings" "testing" @@ -10,7 +9,7 @@ import ( ) func TestMain(m *testing.M) { - os.Exit(test_utils.UnitTestRun(m)) + test_utils.UnitTestRun(m) } func TestEnsureExifOrient(t *testing.T) { diff --git a/api/test_utils/env.go b/api/test_utils/env.go index 20d24329..4f7bef13 100644 --- a/api/test_utils/env.go +++ b/api/test_utils/env.go @@ -6,13 +6,16 @@ import ( ) // PathFromAPIRoot returns the real path in the API project root. -func PathFromAPIRoot(rootRelatedPath string) string { +func PathFromAPIRoot(rootRelatedPaths ...string) string { _, file, _, ok := runtime.Caller(0) if !ok { panic("Can't get the path of current function. It should not happen.") } base := filepath.Dir(file) + args := make([]string, 0, len(rootRelatedPaths)+2) + args = append(args, []string{base, ".."}...) + args = append(args, rootRelatedPaths...) - return filepath.Join(base, "..", rootRelatedPath) + return filepath.Join(args...) } diff --git a/api/test_utils/env_test.go b/api/test_utils/env_test.go index 7b09629c..c4099459 100644 --- a/api/test_utils/env_test.go +++ b/api/test_utils/env_test.go @@ -6,7 +6,18 @@ import ( ) func TestPathFromAPIRoot(t *testing.T) { - if got, want := PathFromAPIRoot("./server.go"), "/api/server.go"; !strings.HasSuffix(got, want) { - t.Fatalf(`PathFromAPIRoot("./server.go") = %q, want a suffix: %q`, got, want) + tests := []struct { + paths []string + want string + }{ + {[]string{"server.go"}, "/api/server.go"}, + {[]string{"scanner", "..", "server.go"}, "/api/server.go"}, + {[]string{"scanner", "scanner_test.go"}, "/api/scanner/scanner_test.go"}, + } + + for _, tc := range tests { + if got, want := PathFromAPIRoot(tc.paths...), tc.want; !strings.HasSuffix(got, want) { + t.Fatalf("PathFromAPIRoot(%v) = %q, want a suffix: %q", tc.paths, got, want) + } } } diff --git a/api/test_utils/integration_setup.go b/api/test_utils/integration_setup.go index a39f30a7..18aff8ce 100644 --- a/api/test_utils/integration_setup.go +++ b/api/test_utils/integration_setup.go @@ -3,8 +3,7 @@ package test_utils import ( "flag" "log" - "path" - "runtime" + "os" "testing" "github.com/joho/godotenv" @@ -16,39 +15,30 @@ import ( var test_dbm TestDBManager = TestDBManager{} -func UnitTestRun(m *testing.M) int { +func UnitTestRun(m *testing.M) { flag.Parse() - return m.Run() + os.Exit(m.Run()) } -func IntegrationTestRun(m *testing.M) int { +func IntegrationTestRun(m *testing.M) { flag.Parse() - _, file, _, ok := runtime.Caller(0) - if !ok { - log.Fatal("could not get runtime file path") - } - if flags.Database { - - envPath := path.Join(path.Dir(file), "..", "testing.env") + envPath := PathFromAPIRoot("testing.env") if err := godotenv.Load(envPath); err != nil { log.Println("No testing.env file found") } } + defer test_dbm.Close() - faceModelsPath := path.Join(path.Dir(file), "..", "data", "models") + faceModelsPath := PathFromAPIRoot("data", "models") utils.ConfigureTestFaceRecognitionModelsPath(faceModelsPath) terminateWorkers := executable_worker.Initialize() defer terminateWorkers() - result := m.Run() - - test_dbm.Close() - - return result + os.Exit(m.Run()) } func FilesystemTest(t *testing.T) { diff --git a/api/utils/utils_test.go b/api/utils/utils_test.go index 57c3fa6e..aeb8d69f 100644 --- a/api/utils/utils_test.go +++ b/api/utils/utils_test.go @@ -10,7 +10,7 @@ import ( ) func TestMain(m *testing.M) { - os.Exit(test_utils.IntegrationTestRun(m)) + test_utils.IntegrationTestRun(m) } func TestIsDirSymlink(t *testing.T) {