package cop_test import ( "errors" "fmt" "os" "os/user" "path" "runtime" "strings" "testing" "gitea.mmo.to/ppForge/ppforge/cop" "gitea.mmo.to/ppForge/ppforge/globals" ) 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) { td := setupSuite(t) defer td(t) fcop := cop.FileCOP{} wd, err := getRepoDir(getCurrentDir()) if err != nil { fmt.Printf("Os error: %s", err) t.Fail() } fcop.Open(path.Join(wd, "test", "cop")) 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) { td := setupSuite(t) defer td(t) fcop := cop.FileCOP{} wd, err := getRepoDir(getCurrentDir()) if err != nil { fmt.Printf("Os error: %s", err) t.Fail() } fcop.Open(path.Join(wd, "test", "cop")) fcop.Close() 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() } err = fcop.Sync() if err == nil { fmt.Println("UpdateCollection did not deliver error") t.Fail() } } 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 // 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() } 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() } // reset again and check if reading the cache works gcop.PCs = []cop.COP{} err = gcop.ReadCache() if err != nil { fmt.Println("Reading the cache file failed") t.Fail() } c = gcop.PCs[0] protos = c.Protocols if len(protos) != 4 { t.Fail() } }