From 4ef77b41caddf5486036809709dd211c4becbff6 Mon Sep 17 00:00:00 2001 From: Goran Rojovic Date: Tue, 5 Nov 2024 14:51:44 +0100 Subject: [PATCH] fix: lint and UT --- agglayer/errors_test.go | 41 ++++++++++++++-------------- agglayer/proof_generation_error.go | 4 +-- agglayer/proof_verification_error.go | 2 +- agglayer/type_conversion_error.go | 2 +- agglayer/types.go | 7 ++++- sonar-project.properties | 4 +-- 6 files changed, 33 insertions(+), 27 deletions(-) diff --git a/agglayer/errors_test.go b/agglayer/errors_test.go index a8abb728..3ca7b7ed 100644 --- a/agglayer/errors_test.go +++ b/agglayer/errors_test.go @@ -13,7 +13,6 @@ import ( func TestErrorVectors(t *testing.T) { t.Parallel() - // we define an alias to avoid infinite recursion type testCase struct { TestName string `json:"test_name"` ExpectedError string `json:"expected_error"` @@ -106,6 +105,7 @@ func TestConvertMapValue_String(t *testing.T) { } } +//nolint:dupl func TestConvertMapValue_Uint32(t *testing.T) { t.Parallel() @@ -160,41 +160,42 @@ func TestConvertMapValue_Uint32(t *testing.T) { } } -func TestConvertMapValue_Bool(t *testing.T) { +//nolint:dupl +func TestConvertMapValue_Uint64(t *testing.T) { t.Parallel() tests := []struct { name string data map[string]interface{} key string - want bool + want uint64 errString string }{ { name: "Key exists and type matches", data: map[string]interface{}{ - "key1": true, + "key1": uint64(3411), }, key: "key1", - want: true, + want: uint64(3411), }, { name: "Key exists but type does not match", data: map[string]interface{}{ - "key1": "value1", + "key1": "not a number", }, key: "key1", - want: false, + want: 0, errString: "is not of type", }, { name: "Key does not exist", data: map[string]interface{}{ - "key1": true, + "key1": uint64(123555), }, - key: "key2", - want: false, - errString: "key key2 not found in map", + key: "key22", + want: 0, + errString: "key key22 not found in map", }, } @@ -204,7 +205,7 @@ func TestConvertMapValue_Bool(t *testing.T) { t.Run(tt.name, func(t *testing.T) { t.Parallel() - got, err := convertMapValue[bool](tt.data, tt.key) + got, err := convertMapValue[uint64](tt.data, tt.key) if tt.errString != "" { require.ErrorContains(t, err, tt.errString) } else { @@ -214,23 +215,23 @@ func TestConvertMapValue_Bool(t *testing.T) { } } -func TestConvertMapValue_Uint64(t *testing.T) { +func TestConvertMapValue_Bool(t *testing.T) { t.Parallel() tests := []struct { name string data map[string]interface{} key string - want uint64 + want bool errString string }{ { name: "Key exists and type matches", data: map[string]interface{}{ - "key1": uint64(123456789), + "key1": true, }, key: "key1", - want: uint64(123456789), + want: true, }, { name: "Key exists but type does not match", @@ -238,16 +239,16 @@ func TestConvertMapValue_Uint64(t *testing.T) { "key1": "value1", }, key: "key1", - want: 0, + want: false, errString: "is not of type", }, { name: "Key does not exist", data: map[string]interface{}{ - "key1": uint64(123456789), + "key1": true, }, key: "key2", - want: 0, + want: false, errString: "key key2 not found in map", }, } @@ -258,7 +259,7 @@ func TestConvertMapValue_Uint64(t *testing.T) { t.Run(tt.name, func(t *testing.T) { t.Parallel() - got, err := convertMapValue[uint64](tt.data, tt.key) + got, err := convertMapValue[bool](tt.data, tt.key) if tt.errString != "" { require.ErrorContains(t, err, tt.errString) } else { diff --git a/agglayer/proof_generation_error.go b/agglayer/proof_generation_error.go index f397fd25..fa7012f7 100644 --- a/agglayer/proof_generation_error.go +++ b/agglayer/proof_generation_error.go @@ -616,9 +616,9 @@ func convertMapValue[T any](data map[string]interface{}, key string) (T, error) if floatValue, ok := value.(float64); ok && targetType.Kind() >= reflect.Int && targetType.Kind() <= reflect.Uint64 { convertedValue, err := convertNumeric(floatValue, targetType) if err != nil { - return target, fmt.Errorf("conversion error for key %s: %v", key, err) + return target, fmt.Errorf("conversion error for key %s: %w", key, err) } - return convertedValue.(T), nil + return convertedValue.(T), nil //nolint:forcetypeassert } return target, fmt.Errorf("value of key %s is not of type %T", key, target) diff --git a/agglayer/proof_verification_error.go b/agglayer/proof_verification_error.go index 00a777f9..dd5c5f74 100644 --- a/agglayer/proof_verification_error.go +++ b/agglayer/proof_verification_error.go @@ -78,7 +78,7 @@ func (p *ProofVerificationError) Unmarshal(data interface{}) error { dataMap, ok := data.(map[string]interface{}) if !ok { // it can be a single error - return getAndAddInnerErrorFn(data.(string), nil) + return getAndAddInnerErrorFn(data.(string), nil) //nolint:forcetypeassert } for key, value := range dataMap { diff --git a/agglayer/type_conversion_error.go b/agglayer/type_conversion_error.go index 37ac6e22..89129253 100644 --- a/agglayer/type_conversion_error.go +++ b/agglayer/type_conversion_error.go @@ -80,7 +80,7 @@ func (p *TypeConversionError) Unmarshal(data interface{}) error { errorSourceMap, ok := data.(map[string]interface{}) if !ok { // it can be a single error - return getAndAddInnerErrorFn(data.(string), nil) + return getAndAddInnerErrorFn(data.(string), nil) //nolint:forcetypeassert } for key, value := range errorSourceMap { diff --git a/agglayer/types.go b/agglayer/types.go index 51216f55..8b343bef 100644 --- a/agglayer/types.go +++ b/agglayer/types.go @@ -433,8 +433,13 @@ type CertificateHeader struct { } func (c CertificateHeader) String() string { + errors := "" + if c.Error != nil { + errors = c.Error.String() + } + return fmt.Sprintf("Height: %d, CertificateID: %s, NewLocalExitRoot: %s. Status: %s. Errors: %s", - c.Height, c.CertificateID.String(), c.NewLocalExitRoot.String(), c.Status.String(), c.Error.String()) + c.Height, c.CertificateID.String(), c.NewLocalExitRoot.String(), c.Status.String(), errors) } func (c *CertificateHeader) UnmarshalJSON(data []byte) error { diff --git a/sonar-project.properties b/sonar-project.properties index f46e9863..f5d0bc73 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -7,11 +7,11 @@ sonar.projectName=cdk sonar.organization=0xpolygon sonar.sources=. -sonar.exclusions=**/test/**,**/vendor/**,**/mocks/**,**/build/**,**/target/**,**/proto/include/**,**/*.pb.go,**/docs/**,**/*.sql,**/mocks_*/*,scripts/**,**/mock_*.go,**/agglayer/**,**/cmd/** +sonar.exclusions=**/test/**,**/vendor/**,**/mocks/**,**/build/**,**/target/**,**/proto/include/**,**/*.pb.go,**/docs/**,**/*.sql,**/mocks_*/*,scripts/**,**/mock_*.go,**/cmd/** sonar.tests=. sonar.test.inclusions=**/*_test.go -sonar.test.exclusions=**/vendor/**,**/docs/**,**/mocks/**,**/*.pb.go,**/*.yml,**/*.yaml,**/*.json,**/*.xml,**/*.toml,**/mocks_*/*,**/mock_*.go,**/agglayer/**,**/cmd/** +sonar.test.exclusions=**/vendor/**,**/docs/**,**/mocks/**,**/*.pb.go,**/*.yml,**/*.yaml,**/*.json,**/*.xml,**/*.toml,**/mocks_*/*,**/mock_*.go,**/cmd/** sonar.issue.enforceSemantic=true # =====================================================