Improve *TestMain to handle the exit code. (#1258)

This commit is contained in:
Googol Lee
2025-07-22 21:14:20 +02:00
committed by GitHub
parent 002cfef11b
commit b55a80f5b9
19 changed files with 45 additions and 51 deletions

View File

@@ -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)
}

View File

@@ -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) {

View File

@@ -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)
}

View File

@@ -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)
}

View File

@@ -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))

View File

@@ -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)
}

View File

@@ -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) {

View File

@@ -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

View File

@@ -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",

View File

@@ -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

View File

@@ -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() {

View File

@@ -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) {

View File

@@ -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

View File

@@ -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) {

View File

@@ -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) {

View File

@@ -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...)
}

View File

@@ -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)
}
}
}

View File

@@ -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) {

View File

@@ -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) {