gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

storage.go (3395B)


      1 // GoToSocial
      2 // Copyright (C) GoToSocial Authors admin@gotosocial.org
      3 // SPDX-License-Identifier: AGPL-3.0-or-later
      4 //
      5 // This program is free software: you can redistribute it and/or modify
      6 // it under the terms of the GNU Affero General Public License as published by
      7 // the Free Software Foundation, either version 3 of the License, or
      8 // (at your option) any later version.
      9 //
     10 // This program is distributed in the hope that it will be useful,
     11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 // GNU Affero General Public License for more details.
     14 //
     15 // You should have received a copy of the GNU Affero General Public License
     16 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
     17 
     18 package testrig
     19 
     20 import (
     21 	"context"
     22 	"fmt"
     23 	"os"
     24 	"path"
     25 
     26 	"codeberg.org/gruf/go-store/v2/storage"
     27 	gtsstorage "github.com/superseriousbusiness/gotosocial/internal/storage"
     28 )
     29 
     30 // NewInMemoryStorage returns a new in memory storage with the default test config
     31 func NewInMemoryStorage() *gtsstorage.Driver {
     32 	storage := storage.OpenMemory(200, false)
     33 	return &gtsstorage.Driver{
     34 		Storage: storage,
     35 	}
     36 }
     37 
     38 // StandardStorageSetup populates the storage with standard test entries from the given directory.
     39 func StandardStorageSetup(storage *gtsstorage.Driver, relativePath string) {
     40 	storedA := newTestStoredAttachments()
     41 	a := NewTestAttachments()
     42 	for k, paths := range storedA {
     43 		attachmentInfo, ok := a[k]
     44 		if !ok {
     45 			panic(fmt.Errorf("key %s not found in test attachments", k))
     46 		}
     47 		filenameOriginal := paths.Original
     48 		filenameSmall := paths.Small
     49 		pathOriginal := attachmentInfo.File.Path
     50 		pathSmall := attachmentInfo.Thumbnail.Path
     51 		bOriginal, err := os.ReadFile(fmt.Sprintf("%s/%s", relativePath, filenameOriginal))
     52 		if err != nil {
     53 			panic(err)
     54 		}
     55 		if _, err := storage.Put(context.TODO(), pathOriginal, bOriginal); err != nil {
     56 			panic(err)
     57 		}
     58 		bSmall, err := os.ReadFile(fmt.Sprintf("%s/%s", relativePath, filenameSmall))
     59 		if err != nil {
     60 			panic(err)
     61 		}
     62 		if _, err := storage.Put(context.TODO(), pathSmall, bSmall); err != nil {
     63 			panic(err)
     64 		}
     65 	}
     66 
     67 	storedE := newTestStoredEmoji()
     68 	e := NewTestEmojis()
     69 	for k, paths := range storedE {
     70 		emojiInfo, ok := e[k]
     71 		if !ok {
     72 			panic(fmt.Errorf("key %s not found in test emojis", k))
     73 		}
     74 		filenameOriginal := paths.Original
     75 		filenameStatic := paths.Static
     76 		pathOriginal := emojiInfo.ImagePath
     77 		pathStatic := emojiInfo.ImageStaticPath
     78 		bOriginal, err := os.ReadFile(fmt.Sprintf("%s/%s", relativePath, filenameOriginal))
     79 		if err != nil {
     80 			panic(err)
     81 		}
     82 		if _, err := storage.Put(context.TODO(), pathOriginal, bOriginal); err != nil {
     83 			panic(err)
     84 		}
     85 		bStatic, err := os.ReadFile(fmt.Sprintf("%s/%s", relativePath, filenameStatic))
     86 		if err != nil {
     87 			panic(err)
     88 		}
     89 		if _, err := storage.Put(context.TODO(), pathStatic, bStatic); err != nil {
     90 			panic(err)
     91 		}
     92 	}
     93 }
     94 
     95 // StandardStorageTeardown deletes everything in storage so that it's clean for the next test.
     96 func StandardStorageTeardown(storage *gtsstorage.Driver) {
     97 	defer os.RemoveAll(path.Join(os.TempDir(), "gotosocial"))
     98 
     99 	var keys []string
    100 
    101 	_ = storage.WalkKeys(context.Background(), func(ctx context.Context, key string) error {
    102 		keys = append(keys, key)
    103 		return nil
    104 	})
    105 
    106 	for _, key := range keys {
    107 		_ = storage.Delete(context.Background(), key)
    108 	}
    109 }