Skip to content

Commit

Permalink
fix: follow golangci-lint
Browse files Browse the repository at this point in the history
Signed-off-by: Zxilly <[email protected]>
  • Loading branch information
Zxilly committed Jul 7, 2024
1 parent b44bec5 commit c4fa3c1
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 50 deletions.
24 changes: 12 additions & 12 deletions internal/diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,23 @@ const (
changeTypeChange changeType = "change"
)

type DiffBase struct {
type Base struct {
Name string `json:"name"`
From int64 `json:"from"`
To int64 `json:"to"`
ChangeType changeType `json:"change_type"`
}

func diffBaseCmp(a, b DiffBase) int {
func diffBaseCmp(a, b Base) int {
return -cmp.Compare(a.To-a.From, b.To-b.From)
}

type diffPackage struct {
DiffBase
Base
}

type diffSection struct {
DiffBase
Base
OldFileSize int64 `json:"old_file_size"`
OldKnownSize int64 `json:"old_known_size"`
NewFileSize int64 `json:"new_file_size"`
Expand All @@ -59,19 +59,19 @@ func processPackages(newPackages, oldPackages map[string]commonPackage) (ret []d
fromSize = oldV.Size
}
ret = append(ret, diffPackage{
DiffBase: DiffBase{Name: k, From: fromSize, To: v.Size, ChangeType: typ},
Base: Base{Name: k, From: fromSize, To: v.Size, ChangeType: typ},
})
}

for k, v := range oldPackages {
if _, ok := newPackages[k]; !ok {
ret = append(ret, diffPackage{
DiffBase: DiffBase{Name: k, From: v.Size, To: 0, ChangeType: changeTypeRemove},
Base: Base{Name: k, From: v.Size, To: 0, ChangeType: changeTypeRemove},
})
}
}

return
return ret
}

func processSections(newSections, oldSections []commonSection) (ret []diffSection) {
Expand Down Expand Up @@ -100,7 +100,7 @@ func processSections(newSections, oldSections []commonSection) (ret []diffSectio
}

ret = append(ret, diffSection{
DiffBase: DiffBase{Name: k, From: fromSize, To: v.UnknownSize(), ChangeType: typ},
Base: Base{Name: k, From: fromSize, To: v.UnknownSize(), ChangeType: typ},
OldFileSize: fromFileSize,
OldKnownSize: fromKnownSize,
NewFileSize: v.FileSize,
Expand All @@ -111,14 +111,14 @@ func processSections(newSections, oldSections []commonSection) (ret []diffSectio
for k, v := range oldSectionsMap {
if _, ok := newSectionsMap[k]; !ok {
ret = append(ret, diffSection{
DiffBase: DiffBase{Name: k, From: v.UnknownSize(), To: 0, ChangeType: changeTypeRemove},
Base: Base{Name: k, From: v.UnknownSize(), To: 0, ChangeType: changeTypeRemove},
OldFileSize: v.FileSize,
OldKnownSize: v.KnownSize,
})
}
}

return
return ret
}

func newDiffResult(newResult, oldResult *commonResult) diffResult {
Expand All @@ -133,10 +133,10 @@ func newDiffResult(newResult, oldResult *commonResult) diffResult {
}

slices.SortFunc(ret.Packages, func(a, b diffPackage) int {
return diffBaseCmp(a.DiffBase, b.DiffBase)
return diffBaseCmp(a.Base, b.Base)
})
slices.SortFunc(ret.Sections, func(a, b diffSection) int {
return diffBaseCmp(a.DiffBase, b.DiffBase)
return diffBaseCmp(a.Base, b.Base)
})

return ret
Expand Down
24 changes: 12 additions & 12 deletions internal/diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ func TestProcessPackages(t *testing.T) {
}

expected := []diffPackage{
{DiffBase{Name: "pkg1", From: 100, To: 150, ChangeType: changeTypeChange}},
{DiffBase{Name: "pkg3", From: 0, To: 300, ChangeType: changeTypeAdd}},
{DiffBase{Name: "pkg2", From: 200, To: 0, ChangeType: changeTypeRemove}},
{Base{Name: "pkg1", From: 100, To: 150, ChangeType: changeTypeChange}},
{Base{Name: "pkg3", From: 0, To: 300, ChangeType: changeTypeAdd}},
{Base{Name: "pkg2", From: 200, To: 0, ChangeType: changeTypeRemove}},
}

result := processPackages(newPackages, oldPackages)
Expand All @@ -37,9 +37,9 @@ func TestProcessSections(t *testing.T) {
}

expected := []diffSection{
{DiffBase: DiffBase{Name: "sec1", From: 50, To: 75, ChangeType: changeTypeChange}, OldFileSize: 100, OldKnownSize: 50, NewFileSize: 150, NewKnownSize: 75},
{DiffBase: DiffBase{Name: "sec3", From: 0, To: 150, ChangeType: changeTypeAdd}, OldFileSize: 0, OldKnownSize: 0, NewFileSize: 300, NewKnownSize: 150},
{DiffBase: DiffBase{Name: "sec2", From: 100, To: 0, ChangeType: changeTypeRemove}, OldFileSize: 200, OldKnownSize: 100, NewFileSize: 0, NewKnownSize: 0},
{Base: Base{Name: "sec1", From: 50, To: 75, ChangeType: changeTypeChange}, OldFileSize: 100, OldKnownSize: 50, NewFileSize: 150, NewKnownSize: 75},
{Base: Base{Name: "sec3", From: 0, To: 150, ChangeType: changeTypeAdd}, OldFileSize: 0, OldKnownSize: 0, NewFileSize: 300, NewKnownSize: 150},
{Base: Base{Name: "sec2", From: 100, To: 0, ChangeType: changeTypeRemove}, OldFileSize: 200, OldKnownSize: 100, NewFileSize: 0, NewKnownSize: 0},
}

result := processSections(newSections, oldSections)
Expand Down Expand Up @@ -70,14 +70,14 @@ func TestNewDiffResult(t *testing.T) {

expected := diffResult{
Packages: []diffPackage{
{DiffBase{Name: "pkg3", From: 0, To: 300, ChangeType: changeTypeAdd}},
{DiffBase{Name: "pkg1", From: 100, To: 150, ChangeType: changeTypeChange}},
{DiffBase{Name: "pkg2", From: 200, To: 0, ChangeType: changeTypeRemove}},
{Base{Name: "pkg3", From: 0, To: 300, ChangeType: changeTypeAdd}},
{Base{Name: "pkg1", From: 100, To: 150, ChangeType: changeTypeChange}},
{Base{Name: "pkg2", From: 200, To: 0, ChangeType: changeTypeRemove}},
},
Sections: []diffSection{
{DiffBase: DiffBase{Name: "sec3", From: 0, To: 150, ChangeType: changeTypeAdd}, OldFileSize: 0, OldKnownSize: 0, NewFileSize: 300, NewKnownSize: 150},
{DiffBase: DiffBase{Name: "sec1", From: 50, To: 75, ChangeType: changeTypeChange}, OldFileSize: 100, OldKnownSize: 50, NewFileSize: 150, NewKnownSize: 75},
{DiffBase: DiffBase{Name: "sec2", From: 100, To: 0, ChangeType: changeTypeRemove}, OldFileSize: 200, OldKnownSize: 100, NewFileSize: 0, NewKnownSize: 0},
{Base: Base{Name: "sec3", From: 0, To: 150, ChangeType: changeTypeAdd}, OldFileSize: 0, OldKnownSize: 0, NewFileSize: 300, NewKnownSize: 150},
{Base: Base{Name: "sec1", From: 50, To: 75, ChangeType: changeTypeChange}, OldFileSize: 100, OldKnownSize: 50, NewFileSize: 150, NewKnownSize: 75},
{Base: Base{Name: "sec2", From: 100, To: 0, ChangeType: changeTypeRemove}, OldFileSize: 200, OldKnownSize: 100, NewFileSize: 0, NewKnownSize: 0},
},
}

Expand Down
1 change: 0 additions & 1 deletion internal/diff/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ func TestDiffJSONAndBinary(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

require.NoError(t, Diff(io.Discard, DOptions{
OldTarget: tt.old,
NewTarget: tt.new,
Expand Down
8 changes: 4 additions & 4 deletions internal/diff/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/Zxilly/go-size-analyzer/internal/utils"
)

func diffString(b DiffBase) string {
func diffString(b Base) string {
switch b.ChangeType {
case changeTypeChange:
sign := "+"
Expand Down Expand Up @@ -61,7 +61,7 @@ func text(r *diffResult, writer io.Writer) error {
}

t.AppendRow(table.Row{
diffString(pkg.DiffBase),
diffString(pkg.Base),
name,
bytesWithIgnore(pkg.From),
bytesWithIgnore(pkg.To),
Expand All @@ -73,7 +73,7 @@ func text(r *diffResult, writer io.Writer) error {

for _, section := range r.Sections {
t.AppendRow(table.Row{
diffString(section.DiffBase),
diffString(section.Base),
section.Name,
bytesWithIgnore(section.From),
bytesWithIgnore(section.To),
Expand All @@ -82,7 +82,7 @@ func text(r *diffResult, writer io.Writer) error {
}

t.AppendFooter(table.Row{
diffString(DiffBase{
diffString(Base{
From: r.OldSize,
To: r.NewSize,
ChangeType: changeTypeChange,
Expand Down
17 changes: 9 additions & 8 deletions internal/diff/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestDiffStringChangeTypeChangeReturnsPercentage(t *testing.T) {
b := DiffBase{From: 100, To: 150, ChangeType: changeTypeChange}
b := Base{From: 100, To: 150, ChangeType: changeTypeChange}
assert.Equal(t, "+50.00%", diffString(b))
b = DiffBase{From: 150, To: 100, ChangeType: changeTypeChange}
b = Base{From: 150, To: 100, ChangeType: changeTypeChange}
assert.Equal(t, "-33.33%", diffString(b))
}

func TestDiffStringChangeTypeAddReturnsAdd(t *testing.T) {
b := DiffBase{ChangeType: changeTypeAdd}
b := Base{ChangeType: changeTypeAdd}
assert.Equal(t, "add", diffString(b))
}

func TestDiffStringChangeTypeRemoveReturnsRemove(t *testing.T) {
b := DiffBase{ChangeType: changeTypeRemove}
b := Base{ChangeType: changeTypeRemove}
assert.Equal(t, "remove", diffString(b))
}

Expand All @@ -40,14 +41,14 @@ func TestTextRendersTableCorrectly(t *testing.T) {
OldSize: 100,
NewSize: 150,
Sections: []diffSection{
{DiffBase: DiffBase{Name: "sec1", From: 100, To: 150, ChangeType: changeTypeChange}},
{Base: Base{Name: "sec1", From: 100, To: 150, ChangeType: changeTypeChange}},
},
Packages: []diffPackage{
{DiffBase: DiffBase{Name: "pkg1", From: 100, To: 150, ChangeType: changeTypeChange}},
{Base: Base{Name: "pkg1", From: 100, To: 150, ChangeType: changeTypeChange}},
},
}
err := text(r, &buf)
assert.NoError(t, err)
require.NoError(t, err)
assert.Contains(t, buf.String(), "Diff between old and new")
assert.Contains(t, buf.String(), "sec1")
assert.Contains(t, buf.String(), "pkg1")
Expand All @@ -57,6 +58,6 @@ func TestTextHandlesEmptyResultWithoutError(t *testing.T) {
var buf bytes.Buffer
r := &diffResult{}
err := text(r, &buf)
assert.NoError(t, err)
require.NoError(t, err)
assert.Contains(t, buf.String(), "Diff between and ")
}
6 changes: 3 additions & 3 deletions internal/diff/result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ func TestCommonResultFromFullResult(t *testing.T) {
})
require.NoError(t, err)

crFromJson := new(commonResult)
err = json.UnmarshalRead(jsonData, crFromJson)
crFromJSON := new(commonResult)
err = json.UnmarshalRead(jsonData, crFromJSON)
require.NoError(t, err)

assert.Equal(t, cr, crFromJson)
assert.Equal(t, cr, crFromJSON)
}

func TestCommonResultFromFullAndCompactJSON(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion internal/utils/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ func (r *ReaderAtAdapter) Read(p []byte) (n int, err error) {
if errors.Is(err, io.EOF) && n > 0 {
return n, nil
}
return
return n, err
}
2 changes: 1 addition & 1 deletion internal/utils/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ func TestReaderAtAdapter_Read(t *testing.T) {
// Test reading beyond the data
readData = make([]byte, 10)
n, err = reader.Read(readData)
assert.ErrorIs(t, err, io.EOF)
require.ErrorIs(t, err, io.EOF)
assert.Zero(t, n)
}
16 changes: 8 additions & 8 deletions internal/webui/net_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,26 @@ type CacheSuite struct {
}

func (s *CacheSuite) TestGetTemplate() {
s.T().Run("cache exist", func(t *testing.T) {
s.Run("cache exist", func() {
cacheFile, err := getCacheFilePath()
require.NoError(t, err)
s.Require().NoError(err)

_, err = updateCache(cacheFile)
require.NoError(t, err)
s.Require().NoError(err)

got := GetTemplate()
assert.Contains(t, got, constant.ReplacedStr)
s.Contains(got, constant.ReplacedStr)
})

s.T().Run("cache not exist", func(t *testing.T) {
s.Run("cache not exist", func() {
cacheFile, err := getCacheFilePath()
require.NoError(t, err)
s.Require().NoError(err)

err = os.Remove(cacheFile)
require.NoError(t, err)
s.Require().NoError(err)

got := GetTemplate()
assert.Contains(t, got, constant.ReplacedStr)
s.Contains(got, constant.ReplacedStr)
})
}

Expand Down

0 comments on commit c4fa3c1

Please sign in to comment.