Reactivate tests and write additional ones for protocol
Move two functions into globals as they are benefitial in other tests aswell and probably later in the implementation too.
This commit is contained in:
parent
e318067d90
commit
680d64fc21
|
@ -6,8 +6,6 @@ import (
|
|||
"os"
|
||||
"os/user"
|
||||
"path"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gitea.mmo.to/ppForge/ppforge/cop"
|
||||
|
@ -15,29 +13,10 @@ import (
|
|||
"gitea.mmo.to/ppForge/ppforge/protocol"
|
||||
)
|
||||
|
||||
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())
|
||||
repo, err := globals.GetRepoDir(globals.GetCurrentDir())
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
@ -63,7 +42,7 @@ func TestOpen(t *testing.T) {
|
|||
defer td(t)
|
||||
|
||||
fcop := cop.FileCOP{}
|
||||
wd, err := getRepoDir(getCurrentDir())
|
||||
wd, err := globals.GetRepoDir(globals.GetCurrentDir())
|
||||
if err != nil {
|
||||
fmt.Printf("Os error: %s", err)
|
||||
t.Fail()
|
||||
|
@ -83,7 +62,7 @@ func TestClosed(t *testing.T) {
|
|||
defer td(t)
|
||||
|
||||
fcop := cop.FileCOP{}
|
||||
wd, err := getRepoDir(getCurrentDir())
|
||||
wd, err := globals.GetRepoDir(globals.GetCurrentDir())
|
||||
if err != nil {
|
||||
fmt.Printf("Os error: %s", err)
|
||||
t.Fail()
|
||||
|
@ -118,7 +97,7 @@ func TestGlobalCOP(t *testing.T) {
|
|||
fmt.Println("Length != 1")
|
||||
t.Fail()
|
||||
}
|
||||
wd, _ := getRepoDir(getCurrentDir())
|
||||
wd, err := globals.GetRepoDir(globals.GetCurrentDir())
|
||||
fcop, err := cop.NewFileCOP(path.Join(wd, "test", "cop"))
|
||||
if err != nil {
|
||||
fmt.Println("Error on loading new FileCOP", err)
|
||||
|
|
|
@ -1,8 +1,12 @@
|
|||
package cop
|
||||
|
||||
/*
|
||||
The COP struct is the base for all collections of protocols, it does not implement the COPer interface itself, because it shouldn't be instantiated at all, only in it's specializations.
|
||||
*/
|
||||
|
||||
import "gitea.mmo.to/ppForge/ppforge/protocol"
|
||||
|
||||
// COP represents a Collection of Protocols
|
||||
// COP represents the base for Collection of Protocols
|
||||
type COP struct {
|
||||
Type string
|
||||
Protocols map[string]ProtocolCollectionEntry
|
||||
|
@ -14,3 +18,6 @@ type ProtocolCollectionEntry struct {
|
|||
Path string
|
||||
Protocol protocol.ProtocolMeta
|
||||
}
|
||||
|
||||
// A collection entry has a path, which is meant to be a filesystem path, but doesn't necessarily have to be.
|
||||
// If a collection is implemented as a database, this could also be the unique id. It is there to uniquely identify the entry.
|
||||
|
|
|
@ -1,6 +1,13 @@
|
|||
package globals
|
||||
|
||||
import "os/user"
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"os/user"
|
||||
"path"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Global variables, Mocks, etc.
|
||||
// This package shall not have any dependency towards the application itself!
|
||||
|
@ -31,3 +38,29 @@ type Marshaler interface {
|
|||
fromJSON() error
|
||||
}
|
||||
*/
|
||||
|
||||
// GetCurrentDir returns the current directory of the called function
|
||||
func GetCurrentDir() string {
|
||||
_, filename, _, _ := runtime.Caller(0)
|
||||
return path.Dir(filename)
|
||||
}
|
||||
|
||||
// GetRepoDir tries to find the parent git repository path
|
||||
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
|
||||
}
|
||||
}
|
||||
// linux
|
||||
if start == "/" {
|
||||
return "", errors.New("Parent repository dir couldn't be found")
|
||||
}
|
||||
// windows, how?
|
||||
if repoPath == "" {
|
||||
return GetRepoDir(path.Dir(start))
|
||||
}
|
||||
return repoPath, err
|
||||
}
|
||||
|
|
|
@ -2,8 +2,8 @@ package protocol
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
)
|
||||
|
||||
|
@ -37,6 +37,13 @@ func (prot *Protocol) AppendField(field Field) int {
|
|||
return i + 1
|
||||
}
|
||||
|
||||
// AppendFields to protocol
|
||||
func (prot *Protocol) AppendFields(fields ...Field) {
|
||||
for _, e := range fields {
|
||||
prot.AppendField(e)
|
||||
}
|
||||
}
|
||||
|
||||
// AddField to protocol
|
||||
func (prot *Protocol) AddField(index int, field *Field) {
|
||||
if len(prot.Structure) == index {
|
||||
|
@ -69,7 +76,7 @@ func (prot *Protocol) UpdateFieldByElement(element int, field *Field) {
|
|||
}
|
||||
|
||||
// RemoveFieldByName from the protocol
|
||||
func (prot *Protocol) RemoveFieldByName(name string) {
|
||||
func (prot *Protocol) RemoveFieldByName(name string) error {
|
||||
element := -1
|
||||
for i, f := range prot.Structure {
|
||||
if f.Name == name {
|
||||
|
@ -77,9 +84,10 @@ func (prot *Protocol) RemoveFieldByName(name string) {
|
|||
}
|
||||
}
|
||||
if element == -1 {
|
||||
return
|
||||
return errors.New("Element not found")
|
||||
}
|
||||
prot.RemoveFieldByElement(element)
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveFieldByElement from the protocol
|
||||
|
@ -110,7 +118,7 @@ func (prot *Protocol) Save(path string) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = os.WriteFile(path, data, fs.ModeAppend)
|
||||
err = os.WriteFile(path, data, 0644)
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
|
@ -2,7 +2,10 @@ package protocol
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"path"
|
||||
"testing"
|
||||
|
||||
"gitea.mmo.to/ppForge/ppforge/globals"
|
||||
)
|
||||
|
||||
func TestUpdateMetaData(t *testing.T) {
|
||||
|
@ -57,10 +60,7 @@ func GenerateFields() []Field {
|
|||
func GenerateStructure() *Protocol {
|
||||
p := NewProtocolStructure()
|
||||
slc := GenerateFields()
|
||||
for i, e := range slc {
|
||||
p.AppendField(e)
|
||||
fmt.Printf("%d %s\n", i, e.ToJson())
|
||||
}
|
||||
p.AppendFields(slc...)
|
||||
for i, e := range p.Structure {
|
||||
fmt.Printf("%d %s\n", i, e.ToJson())
|
||||
}
|
||||
|
@ -86,13 +86,27 @@ func TestFieldAppend(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
/*
|
||||
func TestFieldAdd(t *testing.T) {
|
||||
f := NewField(
|
||||
"testfield",
|
||||
"Description",
|
||||
"",
|
||||
8,
|
||||
nil,
|
||||
false,
|
||||
false,
|
||||
)
|
||||
p := NewProtocolStructure()
|
||||
p.AddField(0, f)
|
||||
p.AddField(0, f)
|
||||
}
|
||||
|
||||
func TestUpdateFieldByName(t *testing.T) {
|
||||
p := GenerateStructure()
|
||||
f := NewField(
|
||||
"UpdatedField", "", "", 42, nil, false, true,
|
||||
)
|
||||
UpdateFieldByName(p, "testfield3", f)
|
||||
p.UpdateFieldByName("testfield3", f)
|
||||
for i, e := range p.Structure {
|
||||
fmt.Printf("%d %s\n", i, e.ToJson())
|
||||
}
|
||||
|
@ -101,7 +115,6 @@ func TestFieldAppend(t *testing.T) {
|
|||
t.Fail()
|
||||
}
|
||||
} //
|
||||
*/
|
||||
func TestUpdateFieldByElement(t *testing.T) {
|
||||
p := GenerateStructure()
|
||||
f := NewField(
|
||||
|
@ -117,10 +130,9 @@ func TestUpdateFieldByElement(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
/*
|
||||
func TestRemoveFieldByName(t *testing.T) {
|
||||
p := GenerateStructure()
|
||||
RemoveFieldByName(p, "testfield3")
|
||||
p.RemoveFieldByName("testfield3")
|
||||
for i, e := range p.Structure {
|
||||
fmt.Printf("%d %s\n", i, e.ToJson())
|
||||
}
|
||||
|
@ -129,7 +141,7 @@ func TestUpdateFieldByElement(t *testing.T) {
|
|||
t.Fail()
|
||||
}
|
||||
} //
|
||||
*/
|
||||
|
||||
func TestRemoveFieldByElement(t *testing.T) {
|
||||
p := GenerateStructure()
|
||||
p.RemoveFieldByElement(3)
|
||||
|
@ -146,3 +158,22 @@ func TestToJson(t *testing.T) {
|
|||
p := GenerateStructure()
|
||||
fmt.Print(p.ToJSON())
|
||||
}
|
||||
|
||||
func TestSaveAndLoad(t *testing.T) {
|
||||
p := GenerateStructure()
|
||||
wd, err := globals.GetRepoDir(globals.GetCurrentDir())
|
||||
if err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
path := path.Join(wd, "test", "protocolsavetest.protocoljson")
|
||||
p.Save(path)
|
||||
p2 := NewProtocolStructure()
|
||||
p2.Load(path)
|
||||
if len(p2.Structure) == 0 {
|
||||
t.Fail()
|
||||
}
|
||||
p3, err := LoadNew(path)
|
||||
if len(p3.Structure) == 0 {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue