Fix breaking changes and add test for

This commit is contained in:
2025-01-11 23:51:39 +01:00
parent d5d9819233
commit 51416782e8
2 changed files with 114 additions and 15 deletions

View File

@@ -37,6 +37,9 @@ func (a *Addon) GetRemoteRelease() (body []byte, err error) {
func (a *Addon) HasBreakingChanges(lhsToc, rhsToc string) bool { func (a *Addon) HasBreakingChanges(lhsToc, rhsToc string) bool {
lhsLines := strings.Split(lhsToc, "\n") lhsLines := strings.Split(lhsToc, "\n")
rhsLines := strings.Split(rhsToc, "\n") rhsLines := strings.Split(rhsToc, "\n")
if len(lhsLines) != len(rhsLines) {
return true
}
for i, lhsLine := range lhsLines { for i, lhsLine := range lhsLines {
rhsLine := rhsLines[i] rhsLine := rhsLines[i]
lhsLine = strings.TrimSpace(lhsLine) 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) return false, fmt.Errorf("error creating zip reader: %w", err)
} }
log.Printf("Found %d files", len(zipReader.File)) 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 { for _, file := range zipReader.File {
if file.FileInfo().IsDir() { if file.FileInfo().IsDir() {
continue continue
@@ -95,21 +128,6 @@ func (a *Addon) Update(body []byte) (hasBreakingChanges bool, err error) {
return false, fmt.Errorf("error reading file: %w", err) 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) localPath := filepath.Join(settings.GamePath, "Interface", "AddOns", file.Name)
log.Printf("Updating file %s", localPath) log.Printf("Updating file %s", localPath)
err = UpdateFile(localPath, fileData) 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) return false, fmt.Errorf("error updating file: %w", err)
} }
} }
if hasBreakingChanges { if hasBreakingChanges {
Warning.Printf("Has breaking changes") Warning.Printf("Has breaking changes")
} }

80
addon_test.go Normal file
View 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")
}
}