blob: 62047bfc141749003f3bf04612e8d89cb9f7bc2c [file] [log] [blame]
// Copyright 2021 The Fuchsia Authors.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package diff
import (
"testing"
"github.com/google/go-cmp/cmp"
"go.fuchsia.dev/infra/cmd/size_check/sizes"
)
func TestDiffBinarySizes(t *testing.T) {
t.Parallel()
tests := []struct {
name string
baselineBinarySizes sizes.BinarySizes
binarySizes sizes.BinarySizes
expected *Diff
}{
{
name: "under budgets",
baselineBinarySizes: sizes.BinarySizes{
"componentA": {
Size: 25,
// These should be ignored. The budgets are determined by
// binarySizes.
Budget: 1,
CreepBudget: 1,
},
// componentB should be ignored completely. We only care about
// components in binarySizes.
"componentB": {
Size: 25,
Budget: 1,
CreepBudget: 1,
},
},
binarySizes: sizes.BinarySizes{
"componentA": {
Size: 30,
Budget: 50,
CreepBudget: 10,
},
},
expected: &Diff{
ComponentDiffs: []*ComponentDiff{
{
Name: "componentA",
BaselineSize: 25,
Size: 30,
SizeDiff: 5,
Budget: 50,
CreepBudget: 10,
BudgetExceeded: false,
CreepBudgetExceeded: false,
},
},
BudgetExceeded: false,
CreepBudgetExceeded: false,
},
},
{
name: "over creep budget",
baselineBinarySizes: sizes.BinarySizes{
"componentA": {
Size: 25,
Budget: 1,
CreepBudget: 1,
},
"componentB": {
Size: 25,
Budget: 1,
CreepBudget: 1,
},
},
binarySizes: sizes.BinarySizes{
"componentA": {
Size: 30,
Budget: 50,
CreepBudget: 1,
},
},
expected: &Diff{
ComponentDiffs: []*ComponentDiff{
{
Name: "componentA",
BaselineSize: 25,
Size: 30,
SizeDiff: 5,
Budget: 50,
CreepBudget: 1,
BudgetExceeded: false,
CreepBudgetExceeded: true,
},
},
BudgetExceeded: false,
CreepBudgetExceeded: true,
},
},
{
name: "skipped pairings",
baselineBinarySizes: sizes.BinarySizes{
"componentA": {
Size: 25,
Budget: 1,
CreepBudget: 1,
},
"componentB": {
Size: 25,
Budget: 1,
CreepBudget: 1,
},
},
binarySizes: sizes.BinarySizes{
"componentA": {
Size: 30,
Budget: 50,
CreepBudget: 10,
},
"componentC": {
Size: 30,
Budget: 50,
CreepBudget: 10,
},
},
expected: &Diff{
ComponentDiffs: []*ComponentDiff{
{
Name: "componentA",
BaselineSize: 25,
Size: 30,
SizeDiff: 5,
Budget: 50,
CreepBudget: 10,
BudgetExceeded: false,
CreepBudgetExceeded: false,
},
},
BudgetExceeded: false,
CreepBudgetExceeded: false,
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
d := DiffBinarySizes(test.binarySizes, test.baselineBinarySizes)
if diff := cmp.Diff(test.expected, d); diff != "" {
t.Fatalf("different (-want +got):\n%s", diff)
}
})
}
}
func TestNewDiff(t *testing.T) {
t.Parallel()
componentDiffs := []*ComponentDiff{
{
Name: "componentA",
BaselineSize: 25,
Size: 35,
SizeDiff: 10,
Budget: 30,
CreepBudget: 15,
BudgetExceeded: true,
CreepBudgetExceeded: false,
},
{
Name: "componentB",
BaselineSize: 25,
Size: 45,
SizeDiff: 20,
Budget: 50,
CreepBudget: 10,
BudgetExceeded: false,
CreepBudgetExceeded: true,
},
{
Name: "componentC",
BaselineSize: 25,
Size: 45,
SizeDiff: 20,
Budget: 40,
CreepBudget: 10,
BudgetExceeded: true,
CreepBudgetExceeded: true,
},
}
tests := []struct {
name string
componentDiffs []*ComponentDiff
expected *Diff
}{
{
name: "over absolute budget",
componentDiffs: componentDiffs[0:1],
expected: &Diff{
ComponentDiffs: componentDiffs[0:1],
BudgetExceeded: true,
CreepBudgetExceeded: false,
},
},
{
name: "over creep budget",
componentDiffs: componentDiffs[1:2],
expected: &Diff{
ComponentDiffs: componentDiffs[1:2],
BudgetExceeded: false,
CreepBudgetExceeded: true,
},
},
{
name: "over absolute and creep budgets",
componentDiffs: componentDiffs,
expected: &Diff{
ComponentDiffs: componentDiffs,
BudgetExceeded: true,
CreepBudgetExceeded: true,
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
d := NewDiff(test.componentDiffs)
if diff := cmp.Diff(test.expected, d); diff != "" {
t.Fatalf("different (-want +got):\n%s", diff)
}
})
}
}
func TestNewComponentDiff(t *testing.T) {
t.Parallel()
tests := []struct {
name string
componentName string
baselineSize int64
size int64
budget int64
creepBudget int64
expected *ComponentDiff
}{
{
name: "under budgets",
componentName: "componentA",
baselineSize: 25,
size: 30,
budget: 50,
creepBudget: 10,
expected: &ComponentDiff{
Name: "componentA",
BaselineSize: 25,
Size: 30,
SizeDiff: 5,
Budget: 50,
CreepBudget: 10,
BudgetExceeded: false,
CreepBudgetExceeded: false,
},
},
{
name: "over absolute budget",
componentName: "componentA",
baselineSize: 25,
size: 35,
budget: 30,
creepBudget: 15,
expected: &ComponentDiff{
Name: "componentA",
BaselineSize: 25,
Size: 35,
SizeDiff: 10,
Budget: 30,
CreepBudget: 15,
BudgetExceeded: true,
CreepBudgetExceeded: false,
},
},
{
name: "over creep budget",
componentName: "componentA",
baselineSize: 25,
size: 45,
budget: 50,
creepBudget: 10,
expected: &ComponentDiff{
Name: "componentA",
BaselineSize: 25,
Size: 45,
SizeDiff: 20,
Budget: 50,
CreepBudget: 10,
BudgetExceeded: false,
CreepBudgetExceeded: true,
},
},
{
name: "over absolute and creep budgets",
componentName: "componentA",
baselineSize: 25,
size: 45,
budget: 40,
creepBudget: 10,
expected: &ComponentDiff{
Name: "componentA",
BaselineSize: 25,
Size: 45,
SizeDiff: 20,
Budget: 40,
CreepBudget: 10,
BudgetExceeded: true,
CreepBudgetExceeded: true,
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
cd := NewComponentDiff(test.componentName, test.baselineSize, test.size, test.budget, test.creepBudget)
if diff := cmp.Diff(test.expected, cd); diff != "" {
t.Fatalf("different (-want +got):\n%s", diff)
}
})
}
}