generated from dave/wails-template
Fix breaking changes and add test for
This commit is contained in:
49
addon.go
49
addon.go
@@ -37,6 +37,9 @@ func (a *Addon) GetRemoteRelease() (body []byte, err error) {
|
||||
func (a *Addon) HasBreakingChanges(lhsToc, rhsToc string) bool {
|
||||
lhsLines := strings.Split(lhsToc, "\n")
|
||||
rhsLines := strings.Split(rhsToc, "\n")
|
||||
if len(lhsLines) != len(rhsLines) {
|
||||
return true
|
||||
}
|
||||
for i, lhsLine := range lhsLines {
|
||||
rhsLine := rhsLines[i]
|
||||
lhsLine = strings.TrimSpace(lhsLine)
|
||||
@@ -81,6 +84,36 @@ func (a *Addon) Update(body []byte) (hasBreakingChanges bool, err error) {
|
||||
return false, fmt.Errorf("error creating zip reader: %w", err)
|
||||
}
|
||||
log.Printf("Found %d files", len(zipReader.File))
|
||||
|
||||
// Yes it is quite ugly iterating twice doing the same work
|
||||
// Maybe I fix it later but definitely not now
|
||||
for _, file := range zipReader.File {
|
||||
if strings.HasSuffix(file.Name, ".toc") {
|
||||
localToc, err := os.ReadFile(a.GetLocalTocPath())
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
localToc = []byte{}
|
||||
} else {
|
||||
return false, fmt.Errorf("error reading local toc: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
fileHandle, err := file.Open()
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("error opening file: %w", err)
|
||||
}
|
||||
fileData, err := io.ReadAll(fileHandle)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("error reading file: %w", err)
|
||||
}
|
||||
|
||||
if a.HasBreakingChanges(string(localToc), string(fileData)) {
|
||||
Warning.Printf("Has breaking changes")
|
||||
hasBreakingChanges = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, file := range zipReader.File {
|
||||
if file.FileInfo().IsDir() {
|
||||
continue
|
||||
@@ -95,21 +128,6 @@ func (a *Addon) Update(body []byte) (hasBreakingChanges bool, err error) {
|
||||
return false, fmt.Errorf("error reading file: %w", err)
|
||||
}
|
||||
|
||||
if strings.HasSuffix(file.Name, ".toc") {
|
||||
localToc, err := os.ReadFile(a.GetLocalTocPath())
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
localToc = []byte{}
|
||||
} else {
|
||||
return false, fmt.Errorf("error reading local toc: %w", err)
|
||||
}
|
||||
}
|
||||
if a.HasBreakingChanges(string(localToc), string(fileData)) {
|
||||
log.Printf("Has breaking changes")
|
||||
hasBreakingChanges = true
|
||||
}
|
||||
}
|
||||
|
||||
localPath := filepath.Join(settings.GamePath, "Interface", "AddOns", file.Name)
|
||||
log.Printf("Updating file %s", localPath)
|
||||
err = UpdateFile(localPath, fileData)
|
||||
@@ -117,6 +135,7 @@ func (a *Addon) Update(body []byte) (hasBreakingChanges bool, err error) {
|
||||
return false, fmt.Errorf("error updating file: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
if hasBreakingChanges {
|
||||
Warning.Printf("Has breaking changes")
|
||||
}
|
||||
|
80
addon_test.go
Normal file
80
addon_test.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Compare identical TOC strings returns false for breaking changes
|
||||
func TestHasBreakingChangesWithIdenticalTOCs(t *testing.T) {
|
||||
addon := &Addon{}
|
||||
|
||||
toc := `## Interface: 70300
|
||||
## Title: Channeler
|
||||
## Version: 1.6.0
|
||||
## Notes: Automatically sets up chat channels
|
||||
## Author: Cyka
|
||||
|
||||
#core
|
||||
Channeler.lua`
|
||||
|
||||
result := addon.HasBreakingChanges(toc, toc)
|
||||
|
||||
if result {
|
||||
t.Errorf("Expected HasBreakingChanges to return false for identical TOCs, got true")
|
||||
}
|
||||
}
|
||||
|
||||
// Compare empty TOC strings returns false
|
||||
func TestHasBreakingChangesWithEmptyTOCs(t *testing.T) {
|
||||
addon := &Addon{}
|
||||
|
||||
result := addon.HasBreakingChanges("", "")
|
||||
|
||||
if result {
|
||||
t.Errorf("Expected HasBreakingChanges to return false for empty TOCs, got true")
|
||||
}
|
||||
}
|
||||
|
||||
// Compare TOC strings with different content returns true for breaking changes
|
||||
func TestCompareTocStringsDifferentContent(t *testing.T) {
|
||||
addon := &Addon{}
|
||||
lhsToc := "## Interface: 70300\n## Title: Channeler\nChanneler.lua"
|
||||
rhsToc := "## Interface: 70300\n## Title: Channeler\nDifferentFile.lua"
|
||||
result := addon.HasBreakingChanges(lhsToc, rhsToc)
|
||||
if !result {
|
||||
t.Errorf("Expected breaking changes, but got none")
|
||||
}
|
||||
}
|
||||
|
||||
// Compare TOC strings with different content returns true for breaking changes
|
||||
func TestCompareTocStringsNewFile(t *testing.T) {
|
||||
addon := &Addon{}
|
||||
lhsToc := "## Interface: 70300\n## Title: Channeler\nChanneler.lua"
|
||||
rhsToc := "## Interface: 70300\n## Title: Channeler\nChanneler.lua\nNewFile.lua"
|
||||
result := addon.HasBreakingChanges(lhsToc, rhsToc)
|
||||
if !result {
|
||||
t.Errorf("Expected breaking changes, but got none")
|
||||
}
|
||||
}
|
||||
|
||||
// Compare TOC strings with same content but different whitespace returns false
|
||||
func TestCompareTocStringsSameContentDifferentWhitespace(t *testing.T) {
|
||||
addon := &Addon{}
|
||||
lhsToc := "## Interface: 70300\n## Title: Channeler\nChanneler.lua"
|
||||
rhsToc := "## Interface: 70300 \n## Title: Channeler \nChanneler.lua"
|
||||
result := addon.HasBreakingChanges(lhsToc, rhsToc)
|
||||
if result {
|
||||
t.Errorf("Expected no breaking changes, but got some")
|
||||
}
|
||||
}
|
||||
|
||||
// Compare TOC strings with matching header lines (##) returns false
|
||||
func TestCompareTocStringsMatchingHeaderLines(t *testing.T) {
|
||||
addon := &Addon{}
|
||||
lhsToc := "## Interface: 70300\n## Title: Channeler\n## Version: 1.6.0"
|
||||
rhsToc := "## Interface: 70300\n## Title: Channeler\n## Version: 1.6.0"
|
||||
result := addon.HasBreakingChanges(lhsToc, rhsToc)
|
||||
if result {
|
||||
t.Errorf("Expected no breaking changes, but got some")
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user