ppforge/cop/collection_test.go

179 lines
3.6 KiB
Go
Raw Normal View History

2023-10-27 13:38:35 +02:00
package cop_test
import (
2023-10-27 13:38:35 +02:00
"errors"
"fmt"
"os"
2023-10-27 13:38:35 +02:00
"os/user"
"path"
2023-10-27 13:38:35 +02:00
"runtime"
"strings"
"testing"
2023-10-27 13:38:35 +02:00
"gitea.mmo.to/ppForge/ppforge/cop"
"gitea.mmo.to/ppForge/ppforge/globals"
)
2023-10-27 13:38:35 +02:00
func getCurrentDir() string {
_, filename, _, _ := runtime.Caller(0)
return path.Dir(filename)
}
func getRepoDir(start string) (string, error) {
var repoPath string
entries, err := os.ReadDir(start)
for _, v := range entries {
if strings.HasSuffix(v.Name(), ".git") {
repoPath = start
}
}
if repoPath == "" {
return getRepoDir(path.Dir(start))
}
return repoPath, err
}
func setupSuite(t *testing.T) func(t *testing.T) {
// setup
// setup testing home dir
repo, err := getRepoDir(getCurrentDir())
if err != nil {
t.Error(err)
}
testUserHome := path.Join(repo, "test", "home")
globals.ConfigDirectoryList = []string{testUserHome, ".config", "ppforge"}
globals.ConfigDirectory = path.Join(globals.ConfigDirectoryList...)
globals.CollectionOfProtocolsDir = path.Join(globals.ConfigDirectory, "cop")
globals.MockUserCurrent = func() (*user.User, error) {
mu := user.User{}
mu.HomeDir = testUserHome
return &mu, nil
}
// teardown
return func(t *testing.T) {
globals.MockUserCurrent = user.Current
}
}
func TestOpen(t *testing.T) {
2023-10-27 13:38:35 +02:00
td := setupSuite(t)
defer td(t)
fcop := cop.FileCOP{}
wd, err := getRepoDir(getCurrentDir())
if err != nil {
fmt.Printf("Os error: %s", err)
t.Fail()
}
2023-10-27 13:38:35 +02:00
fcop.Open(path.Join(wd, "test", "cop"))
2023-10-30 22:39:53 +01:00
l, err := fcop.GetProtocols()
if err != nil {
t.Fail()
}
for _, pce := range l {
fmt.Printf("%s//%s\n", pce.Path, pce.Protocol.Name)
}
}
func TestClosed(t *testing.T) {
2023-10-27 13:38:35 +02:00
td := setupSuite(t)
defer td(t)
fcop := cop.FileCOP{}
wd, err := getRepoDir(getCurrentDir())
if err != nil {
fmt.Printf("Os error: %s", err)
t.Fail()
}
2023-10-27 13:38:35 +02:00
fcop.Open(path.Join(wd, "test", "cop"))
fcop.Close()
2023-10-30 22:39:53 +01:00
l, err := fcop.GetProtocols()
if err == nil || l != nil {
fmt.Println("Protocols did not deliver error")
t.Fail()
}
err = fcop.Add(nil)
if err == nil {
fmt.Println("Add did not deliver error")
t.Fail()
}
err = fcop.Update(nil)
if err == nil {
fmt.Println("Update did not deliver error")
t.Fail()
}
2023-10-13 16:13:33 +02:00
err = fcop.Sync()
if err == nil {
fmt.Println("UpdateCollection did not deliver error")
t.Fail()
}
}
2023-10-27 13:38:35 +02:00
func TestGlobalCOP(t *testing.T) {
td := setupSuite(t)
defer td(t)
cop.Init()
gcop := cop.GlobalCOP
pcs := gcop.PCs
fmt.Printf("Length of all Protocol Collections: %d\n", len(pcs))
if len(pcs) != 1 {
fmt.Println("Length != 1")
t.Fail()
}
wd, _ := getRepoDir(getCurrentDir())
fcop, err := cop.NewFileCOP(path.Join(wd, "test", "cop"))
if err != nil {
fmt.Println("Error on loading new FileCOP", err)
t.Fail()
}
gcop.PCs = append(gcop.PCs, fcop.COP)
fmt.Printf("Length of all Protocol Collections: %d\n", len(gcop.PCs))
if len(gcop.PCs) != 2 {
fmt.Println("Amount of COPs is not 2")
t.Fail()
}
}
func TestCOPCache(t *testing.T) {
td := setupSuite(t)
defer td(t)
gcop := cop.GlobalCOP
2023-10-30 22:39:53 +01:00
// reset to zero because of other tests
gcop.PCs = []cop.COP{}
cop.Init()
c := gcop.PCs[0]
protos := c.Protocols
if len(protos) != 4 {
t.Fail()
}
2023-10-27 13:38:35 +02:00
err := gcop.WriteCache()
if err != nil {
fmt.Println("Writing the cache file failed")
t.Fail()
}
fpath := path.Join(globals.CollectionOfProtocolsDir, globals.COPCacheFileName)
if _, staterr := os.Stat(fpath); errors.Is(staterr, os.ErrNotExist) {
fmt.Println("File not existing ", fpath)
t.Fail()
}
2023-10-30 22:39:53 +01:00
// reset again and check if reading the cache works
gcop.PCs = []cop.COP{}
2023-10-27 13:38:35 +02:00
err = gcop.ReadCache()
if err != nil {
fmt.Println("Reading the cache file failed")
t.Fail()
}
2023-10-30 22:39:53 +01:00
c = gcop.PCs[0]
protos = c.Protocols
if len(protos) != 4 {
t.Fail()
}
2023-10-27 13:38:35 +02:00
}