From 680d64fc21890760fd2f003b16a597cae506852b Mon Sep 17 00:00:00 2001 From: "Marcel M. Otte" Date: Fri, 10 Nov 2023 12:54:43 +0100 Subject: [PATCH] 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. --- cop/collection_test.go | 29 ++---------- cop/cop.go | 9 +++- globals/globals.go | 35 +++++++++++++- protocol/protocol.go | 16 +++++-- protocol/protocol_test.go | 97 ++++++++++++++++++++++++++------------- 5 files changed, 122 insertions(+), 64 deletions(-) diff --git a/cop/collection_test.go b/cop/collection_test.go index 4c6df2f..4c615cf 100644 --- a/cop/collection_test.go +++ b/cop/collection_test.go @@ -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) diff --git a/cop/cop.go b/cop/cop.go index e57a267..40b02a6 100644 --- a/cop/cop.go +++ b/cop/cop.go @@ -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. diff --git a/globals/globals.go b/globals/globals.go index 27bbc8b..4d8d008 100644 --- a/globals/globals.go +++ b/globals/globals.go @@ -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 +} diff --git a/protocol/protocol.go b/protocol/protocol.go index 9fb0ac3..9fca27f 100644 --- a/protocol/protocol.go +++ b/protocol/protocol.go @@ -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 } diff --git a/protocol/protocol_test.go b/protocol/protocol_test.go index 695251c..e2b2e0b 100644 --- a/protocol/protocol_test.go +++ b/protocol/protocol_test.go @@ -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,22 +86,35 @@ func TestFieldAppend(t *testing.T) { } } -/* - func TestUpdateFieldByName(t *testing.T) { - p := GenerateStructure() - f := NewField( - "UpdatedField", "", "", 42, nil, false, true, - ) - UpdateFieldByName(p, "testfield3", f) - for i, e := range p.Structure { - fmt.Printf("%d %s\n", i, e.ToJson()) - } - if p.Structure[3].Desc != "" || p.Structure[3].Size != 42 { - t.Log("Update field by Ref failed!") - t.Fail() - } - } // -*/ +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, + ) + p.UpdateFieldByName("testfield3", f) + for i, e := range p.Structure { + fmt.Printf("%d %s\n", i, e.ToJson()) + } + if p.Structure[3].Desc != "" || p.Structure[3].Size != 42 { + t.Log("Update field by Ref failed!") + t.Fail() + } +} // func TestUpdateFieldByElement(t *testing.T) { p := GenerateStructure() f := NewField( @@ -117,19 +130,18 @@ func TestUpdateFieldByElement(t *testing.T) { } } -/* - func TestRemoveFieldByName(t *testing.T) { - p := GenerateStructure() - RemoveFieldByName(p, "testfield3") - for i, e := range p.Structure { - fmt.Printf("%d %s\n", i, e.ToJson()) - } - if p.Structure[3].Name != "testfield4" { - t.Log("Remove by name failed!") - t.Fail() - } - }// -*/ +func TestRemoveFieldByName(t *testing.T) { + p := GenerateStructure() + p.RemoveFieldByName("testfield3") + for i, e := range p.Structure { + fmt.Printf("%d %s\n", i, e.ToJson()) + } + if p.Structure[3].Name != "testfield4" { + t.Log("Remove by name failed!") + 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() + } +}