1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/pkg/log/errors_test.go
Jk1484 ffc931aad1
feat(golangBuild): use 'unit' build tag to include tests during test execution (#4345)
* Added unit tag as argument. Added description to runTests command. Changed code generator to have unit build tag in generated unit test files.

* Added unit build tag to all unit test files.

* added to new unit test unit build tag

* Update verify-go.yml

* small fix

---------

Co-authored-by: Muhammadali Nazarov <Muhammadali.Nazarov@acronis.com>
Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
2023-05-03 21:02:11 +05:00

94 lines
2.0 KiB
Go

//go:build unit
// +build unit
package log
import (
"encoding/json"
"fmt"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"testing"
)
func TestSetErrorCategory(t *testing.T) {
SetErrorCategory(ErrorCustom)
assert.Equal(t, errorCategory, ErrorCustom)
assert.Equal(t, "custom", fmt.Sprint(errorCategory))
}
func TestGetErrorCategory(t *testing.T) {
errorCategory = ErrorCompliance
assert.Equal(t, GetErrorCategory(), errorCategory)
}
func TestSetFatalErrorDetail(t *testing.T) {
sampleError := logrus.Fields{"Message": "Error happened"}
errDetails, _ := json.Marshal(&sampleError)
tests := []struct {
name string
error []byte
want []byte
errPresent bool
}{
{
name: "set fatal error",
error: errDetails,
want: errDetails,
errPresent: false,
},
{
name: "set fatal error - override",
error: errDetails,
want: errDetails,
errPresent: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.errPresent {
SetFatalErrorDetail(tt.error)
}
SetFatalErrorDetail(tt.error)
assert.Equalf(t, tt.want, fatalError, "GetFatalErrorDetail()")
fatalError = nil // reset error
})
}
}
func TestGetFatalErrorDetail(t *testing.T) {
sampleError := logrus.Fields{"Message": "Error happened"}
errDetails, _ := json.Marshal(&sampleError)
tests := []struct {
name string
errDetails []byte
want string
}{
{
name: "returns fatal error",
errDetails: errDetails,
want: "{\"Message\":\"Error happened\"}",
},
{
name: "no fatal error set - returns empty",
errDetails: nil,
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.errDetails != nil {
SetFatalErrorDetail(tt.errDetails)
}
str := ""
data := []byte(str)
fmt.Println(data)
fatalErrorDetail := string(GetFatalErrorDetail())
assert.Equalf(t, tt.want, fatalErrorDetail, "GetFatalErrorDetail()")
fatalError = nil // resets fatal error
})
}
}