@@ -19,13 +19,15 @@ import (
1919 _ "github.com/jinzhu/gorm/dialects/sqlite"
2020
2121 "github.com/photoprism/photoprism/internal/config/customize"
22+ "github.com/photoprism/photoprism/internal/event"
2223 "github.com/photoprism/photoprism/internal/service/hub"
2324 "github.com/photoprism/photoprism/internal/thumb"
2425 "github.com/photoprism/photoprism/pkg/authn"
2526 "github.com/photoprism/photoprism/pkg/capture"
2627 "github.com/photoprism/photoprism/pkg/clean"
2728 "github.com/photoprism/photoprism/pkg/dsn"
2829 "github.com/photoprism/photoprism/pkg/fs"
30+ "github.com/photoprism/photoprism/pkg/log/status"
2931 "github.com/photoprism/photoprism/pkg/rnd"
3032 "github.com/photoprism/photoprism/pkg/txt/report"
3133)
@@ -337,8 +339,9 @@ func NewTestConfig(dbName string) *Config {
337339
338340 var tp string
339341 var err error
342+
340343 if tp , err = os .MkdirTemp (storagePath , "test-photoprism-*" ); err != nil {
341- log .Fatalf ("config: %s" , err .Error ())
344+ log .Panicf ("config: %s" , clean .Error (err ))
342345 }
343346
344347 tp = filepath .Join (tp , fs .TestdataDir )
@@ -352,22 +355,22 @@ func NewTestConfig(dbName string) *Config {
352355
353356 s := customize .NewSettings (c .DefaultTheme (), c .DefaultLocale (), c .DefaultTimezone ().String ())
354357
355- if err : = fs .MkdirAll (c .ConfigPath ()); err != nil {
356- log .Panicf ("config: %s" , err .Error ())
358+ if err = fs .MkdirAll (c .ConfigPath ()); err != nil {
359+ log .Panicf ("config: %s" , clean .Error (err ))
357360 }
358361
359362 // Save settings next to the test config path, reusing any existing
360363 // `.yaml`/`.yml` variant so the tests mirror production behavior.
361- if err : = s .Save (fs .ConfigFilePath (c .ConfigPath (), "settings" , fs .ExtYml )); err != nil {
362- log .Panicf ("config: %s" , err .Error ())
364+ if err = s .Save (fs .ConfigFilePath (c .ConfigPath (), "settings" , fs .ExtYml )); err != nil {
365+ log .Panicf ("config: %s" , clean .Error (err ))
363366 }
364367
365- if err : = c .Init (); err != nil {
366- log .Panicf ("config: %s" , err .Error ())
368+ if err = c .Init (); err != nil {
369+ log .Panicf ("config: %s" , clean .Error (err ))
367370 }
368371
369- if err : = c .InitializeTestData (); err != nil {
370- log .Errorf ("config: %s" , err .Error ())
372+ if err = c .InitializeTestData (); err != nil {
373+ log .Errorf ("config: %s" , clean .Error (err ))
371374 }
372375
373376 c .RegisterDb ()
@@ -635,20 +638,28 @@ func (c *Config) AssertTestData(t *testing.T) {
635638 }
636639}
637640
638- // CleanupTestFolder uses RemoveAll to remove the storage path above testdata.
641+ // CleanupTestFolder removes the isolated storage directory created by NewTestConfig.
642+ //
643+ // It only deletes paths matching the isolated layout "test-photoprism-*/testdata" so a
644+ // misconfigured StoragePath can never remove a real storage directory. A failed removal
645+ // is logged as a warning rather than aborting, so a teardown hiccup does not turn a
646+ // passing test run into a hard exit.
639647func (c * Config ) CleanupTestFolder () {
640648 if c .options == nil {
641- log . Warn ( "config: c.options is nil in CleanupTestFolder" )
649+ event . SystemWarn ([] string { "config" , "test" , " c.options is nil in CleanupTestFolder"} )
642650 return
643651 }
652+
644653 td := c .StoragePath ()
645- if strings .HasSuffix (td , "/testdata" ) && strings .Contains (td , "test-photoprism" ) {
646- td = strings .TrimSuffix (td , "/testdata" )
647- if err := os .RemoveAll (td ); err != nil {
648- log .Fatalf ("config: %s (cleantestfolder)" , err .Error ())
654+ parent := filepath .Dir (td )
655+
656+ if filepath .Base (td ) == fs .TestdataDir && strings .HasPrefix (filepath .Base (parent ), "test-photoprism" ) {
657+ if err := os .RemoveAll (parent ); err != nil {
658+ event .SystemWarn ([]string {"config" , "test" , "cleanup %s" , "%s" }, parent , clean .Error (err ))
659+ return
649660 }
650- log . Debugf ( "config: cleaned up %s" , td )
661+ event . SystemDebug ([] string { "config" , "test" , "cleanup %s" , status . Succeeded }, parent )
651662 } else {
652- log . Warnf ( "config: %s not cleaned up" , td )
663+ event . SystemWarn ([] string { "config" , "test" , "cleanup %s" , "failed" } , td )
653664 }
654665}
0 commit comments