2023-10-27 13:38:35 +02:00
|
|
|
package cop_test
|
2023-09-17 12:49:41 +02:00
|
|
|
|
|
|
|
import (
|
2023-10-27 13:38:35 +02:00
|
|
|
"errors"
|
2023-09-17 12:49:41 +02:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2023-10-27 13:38:35 +02:00
|
|
|
"os/user"
|
2023-09-17 12:49:41 +02:00
|
|
|
"path"
|
2023-10-27 13:38:35 +02:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
2023-09-17 12:49:41 +02:00
|
|
|
"testing"
|
2023-10-27 13:38:35 +02:00
|
|
|
|
|
|
|
"gitea.mmo.to/ppForge/ppforge/cop"
|
|
|
|
"gitea.mmo.to/ppForge/ppforge/globals"
|
2023-09-17 12:49:41 +02:00
|
|
|
)
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-17 12:49:41 +02:00
|
|
|
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())
|
2023-09-17 12:49:41 +02:00
|
|
|
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-09-17 12:49:41 +02:00
|
|
|
l, err := fcop.Protocols()
|
|
|
|
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())
|
2023-09-17 12:49:41 +02:00
|
|
|
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-09-17 12:49:41 +02:00
|
|
|
fcop.Close()
|
|
|
|
l, err := fcop.Protocols()
|
|
|
|
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()
|
2023-09-17 12:49:41 +02:00
|
|
|
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
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
err = gcop.ReadCache()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Reading the cache file failed")
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|