Skip to content

Commit 35d3156

Browse files
🌱 Unit tests for pinned_dependencies
- Additional tests for pinned_dependencies #986
1 parent c10a6ae commit 35d3156

File tree

1 file changed

+190
-0
lines changed

1 file changed

+190
-0
lines changed

checks/pinned_dependencies_test.go

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package checks
1616

1717
import (
18+
"errors"
1819
"fmt"
1920
"os"
2021
"strings"
@@ -1414,3 +1415,192 @@ func TestGitHubWorkInsecureDownloadsLineNumber(t *testing.T) {
14141415
})
14151416
}
14161417
}
1418+
1419+
func Test_createReturnValuesForGitHubActionsWorkflowPinned(t *testing.T) {
1420+
t.Parallel()
1421+
//nolint
1422+
type args struct {
1423+
r worklowPinningResult
1424+
infoMsg string
1425+
dl checker.DetailLogger
1426+
err error
1427+
}
1428+
//nolint
1429+
tests := []struct {
1430+
name string
1431+
args args
1432+
want int
1433+
wantErr bool
1434+
}{
1435+
{
1436+
name: "both actions workflow pinned",
1437+
args: args{
1438+
r: worklowPinningResult{
1439+
thirdParties: 1,
1440+
gitHubOwned: 1,
1441+
},
1442+
infoMsg: "",
1443+
dl: &scut.TestDetailLogger{},
1444+
err: nil,
1445+
},
1446+
want: 10,
1447+
wantErr: false,
1448+
},
1449+
{
1450+
name: "github actions workflow pinned",
1451+
args: args{
1452+
r: worklowPinningResult{
1453+
thirdParties: 2,
1454+
gitHubOwned: 2,
1455+
},
1456+
infoMsg: "",
1457+
dl: &scut.TestDetailLogger{},
1458+
err: nil,
1459+
},
1460+
want: 0,
1461+
wantErr: false,
1462+
},
1463+
{
1464+
name: "error in github actions workflow pinned",
1465+
args: args{
1466+
r: worklowPinningResult{
1467+
thirdParties: 2,
1468+
gitHubOwned: 2,
1469+
},
1470+
infoMsg: "",
1471+
dl: &scut.TestDetailLogger{},
1472+
err: errors.New("error"),
1473+
},
1474+
want: -1,
1475+
wantErr: true,
1476+
},
1477+
}
1478+
for _, tt := range tests {
1479+
tt := tt // Re-initializing variable so it is not changed while executing the closure below
1480+
t.Run(tt.name, func(t *testing.T) {
1481+
t.Parallel()
1482+
got, err := createReturnValuesForGitHubActionsWorkflowPinned(tt.args.r, tt.args.infoMsg, tt.args.dl, tt.args.err)
1483+
if (err != nil) != tt.wantErr {
1484+
t.Errorf("createReturnValuesForGitHubActionsWorkflowPinned() error = %v, wantErr %v", err, tt.wantErr)
1485+
return
1486+
}
1487+
if got != tt.want {
1488+
t.Errorf("createReturnValuesForGitHubActionsWorkflowPinned() = %v, want %v", got, tt.want)
1489+
}
1490+
})
1491+
}
1492+
}
1493+
1494+
func Test_createReturnValues(t *testing.T) {
1495+
t.Parallel()
1496+
//nolint
1497+
type args struct {
1498+
r pinnedResult
1499+
infoMsg string
1500+
dl checker.DetailLogger
1501+
err error
1502+
}
1503+
//nolint
1504+
tests := []struct {
1505+
name string
1506+
args args
1507+
want int
1508+
wantErr bool
1509+
}{
1510+
{
1511+
name: "returns 10 if no error",
1512+
args: args{
1513+
r: 1,
1514+
infoMsg: "",
1515+
dl: &scut.TestDetailLogger{},
1516+
err: nil,
1517+
},
1518+
want: 10,
1519+
wantErr: false,
1520+
},
1521+
{
1522+
name: "returns 0 if unpinned ",
1523+
args: args{
1524+
r: 2,
1525+
infoMsg: "",
1526+
dl: &scut.TestDetailLogger{},
1527+
err: nil,
1528+
},
1529+
want: 0,
1530+
wantErr: false,
1531+
},
1532+
{
1533+
name: "if err is not nil, returns 0",
1534+
args: args{
1535+
r: 2,
1536+
infoMsg: "",
1537+
dl: &scut.TestDetailLogger{},
1538+
//nolint
1539+
err: errors.New("error"),
1540+
},
1541+
want: -1,
1542+
wantErr: true,
1543+
},
1544+
}
1545+
for _, tt := range tests {
1546+
tt := tt
1547+
t.Run(tt.name, func(t *testing.T) {
1548+
t.Parallel()
1549+
got, err := createReturnValues(tt.args.r, tt.args.infoMsg, tt.args.dl, tt.args.err)
1550+
if (err != nil) != tt.wantErr {
1551+
t.Errorf("createReturnValues() error = %v, wantErr %v", err, tt.wantErr)
1552+
return
1553+
}
1554+
if got != tt.want {
1555+
t.Errorf("createReturnValues() = %v, want %v", got, tt.want)
1556+
}
1557+
})
1558+
}
1559+
}
1560+
1561+
func Test_maxScore(t *testing.T) {
1562+
t.Parallel()
1563+
type args struct {
1564+
s1 int
1565+
s2 int
1566+
}
1567+
tests := []struct {
1568+
name string
1569+
args args
1570+
want int
1571+
}{
1572+
{
1573+
name: "returns s1 if s1 is greater than s2",
1574+
args: args{
1575+
s1: 10,
1576+
s2: 5,
1577+
},
1578+
want: 10,
1579+
},
1580+
{
1581+
name: "returns s2 if s2 is greater than s1",
1582+
args: args{
1583+
s1: 5,
1584+
s2: 10,
1585+
},
1586+
want: 10,
1587+
},
1588+
{
1589+
name: "returns s1 if s1 is equal to s2",
1590+
args: args{
1591+
s1: 10,
1592+
s2: 10,
1593+
},
1594+
want: 10,
1595+
},
1596+
}
1597+
for _, tt := range tests {
1598+
tt := tt
1599+
t.Run(tt.name, func(t *testing.T) {
1600+
t.Parallel()
1601+
if got := maxScore(tt.args.s1, tt.args.s2); got != tt.want {
1602+
t.Errorf("maxScore() = %v, want %v", got, tt.want)
1603+
}
1604+
})
1605+
}
1606+
}

0 commit comments

Comments
 (0)