From d50ed41e799682382c78c396820f4befa6ee7509 Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Wed, 26 Jun 2024 14:06:49 +0200 Subject: [PATCH] chore: enable float-compare rule from testifylint (#6967) Signed-off-by: knqyf263 Co-authored-by: knqyf263 --- .golangci.yaml | 3 -- .../arm/parser/armjson/parse_number_test.go | 8 +-- .../arm/parser/armjson/parse_object_test.go | 4 +- .../cloudformation/parser/parser_test.go | 2 +- pkg/iac/scanners/json/parser/parser_test.go | 2 +- pkg/parallel/pipeline_test.go | 51 +++++++++---------- 6 files changed, 33 insertions(+), 37 deletions(-) diff --git a/.golangci.yaml b/.golangci.yaml index 17efa8050e11..95c66cc69f73 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -84,9 +84,6 @@ linters-settings: ignore-generated-header: true testifylint: enable-all: true - disable: - - float-compare - linters: disable-all: true enable: diff --git a/pkg/iac/scanners/azure/arm/parser/armjson/parse_number_test.go b/pkg/iac/scanners/azure/arm/parser/armjson/parse_number_test.go index f0f7532c15a4..d2acf3b0a8d9 100644 --- a/pkg/iac/scanners/azure/arm/parser/armjson/parse_number_test.go +++ b/pkg/iac/scanners/azure/arm/parser/armjson/parse_number_test.go @@ -24,7 +24,7 @@ func Test_Number_IntToFloat(t *testing.T) { metadata := types.NewTestMetadata() err := Unmarshal(example, &output, &metadata) require.NoError(t, err) - assert.Equal(t, 123.0, output) + assert.InEpsilon(t, 123.0, output, 0.0001) } func Test_Number_FloatToFloat(t *testing.T) { @@ -33,7 +33,7 @@ func Test_Number_FloatToFloat(t *testing.T) { metadata := types.NewTestMetadata() err := Unmarshal(example, &output, &metadata) require.NoError(t, err) - assert.Equal(t, 123.456, output) + assert.InEpsilon(t, 123.456, output, 0.0001) } func Test_Number_FloatToInt(t *testing.T) { @@ -42,7 +42,7 @@ func Test_Number_FloatToInt(t *testing.T) { metadata := types.NewTestMetadata() err := Unmarshal(example, &output, &metadata) require.NoError(t, err) - assert.Equal(t, 123, output) + assert.InEpsilon(t, 123, output, 0.0001) } func Test_Number_FloatWithExponent(t *testing.T) { @@ -70,7 +70,7 @@ func Test_Number_FloatWithExponent(t *testing.T) { metadata := types.NewTestMetadata() err := Unmarshal(example, &output, &metadata) require.NoError(t, err) - assert.Equal(t, test.out, output) + assert.InEpsilon(t, test.out, output, 0.0001) }) } diff --git a/pkg/iac/scanners/azure/arm/parser/armjson/parse_object_test.go b/pkg/iac/scanners/azure/arm/parser/armjson/parse_object_test.go index 2971ae73de21..31ea685cef23 100644 --- a/pkg/iac/scanners/azure/arm/parser/armjson/parse_object_test.go +++ b/pkg/iac/scanners/azure/arm/parser/armjson/parse_object_test.go @@ -21,7 +21,7 @@ func Test_Object(t *testing.T) { metadata := types.NewTestMetadata() require.NoError(t, Unmarshal(example, &target, &metadata)) assert.Equal(t, "testing", target.Name) - assert.Equal(t, 3.14, target.Balance) + assert.InEpsilon(t, 3.14, target.Balance, 0.0001) } func Test_ObjectWithPointers(t *testing.T) { @@ -36,7 +36,7 @@ func Test_ObjectWithPointers(t *testing.T) { metadata := types.NewTestMetadata() require.NoError(t, Unmarshal(example, &target, &metadata)) assert.Equal(t, "testing", *target.Name) - assert.Equal(t, 3.14, *target.Balance) + assert.InEpsilon(t, 3.14, *target.Balance, 0.0001) } type nestedParent struct { diff --git a/pkg/iac/scanners/cloudformation/parser/parser_test.go b/pkg/iac/scanners/cloudformation/parser/parser_test.go index 396e12f1bf57..0d37440e2e1a 100644 --- a/pkg/iac/scanners/cloudformation/parser/parser_test.go +++ b/pkg/iac/scanners/cloudformation/parser/parser_test.go @@ -410,7 +410,7 @@ func TestJsonWithNumbers(t *testing.T) { file := files[0] assert.Equal(t, 1, file.Parameters["SomeIntParam"].Default()) - assert.Equal(t, 1.1, file.Parameters["SomeFloatParam"].Default()) + assert.InEpsilon(t, 1.1, file.Parameters["SomeFloatParam"].Default(), 0.0001) res := file.GetResourcesByType("Test::Resource") assert.NotNil(t, res) diff --git a/pkg/iac/scanners/json/parser/parser_test.go b/pkg/iac/scanners/json/parser/parser_test.go index ed7b87492d96..a47868fad0ed 100644 --- a/pkg/iac/scanners/json/parser/parser_test.go +++ b/pkg/iac/scanners/json/parser/parser_test.go @@ -34,7 +34,7 @@ func Test_Parser(t *testing.T) { y, ok := yRaw.(float64) require.True(t, ok) - assert.Equal(t, 123.0, y) + assert.InEpsilon(t, 123.0, y, 0.0001) zRaw, ok := xMsi["z"] require.True(t, ok) diff --git a/pkg/parallel/pipeline_test.go b/pkg/parallel/pipeline_test.go index 60b8cec100ab..4fb008c9e00e 100644 --- a/pkg/parallel/pipeline_test.go +++ b/pkg/parallel/pipeline_test.go @@ -3,7 +3,6 @@ package parallel_test import ( "context" "fmt" - "math" "testing" "github.com/stretchr/testify/assert" @@ -15,13 +14,13 @@ import ( func TestPipeline_Do(t *testing.T) { type field struct { numWorkers int - items []float64 - onItem func(context.Context, float64) (float64, error) + items []int + onItem func(context.Context, int) (int, error) } type testCase struct { name string field field - want float64 + want int wantErr require.ErrorAssertionFunc } tests := []testCase{ @@ -29,7 +28,7 @@ func TestPipeline_Do(t *testing.T) { name: "pow", field: field{ numWorkers: 5, - items: []float64{ + items: []int{ 1, 2, 3, @@ -41,44 +40,44 @@ func TestPipeline_Do(t *testing.T) { 9, 10, }, - onItem: func(_ context.Context, f float64) (float64, error) { - return math.Pow(f, 2), nil + onItem: func(_ context.Context, i int) (int, error) { + return i * i, nil }, }, want: 385, wantErr: require.NoError, }, { - name: "ceil", + name: "double", field: field{ numWorkers: 3, - items: []float64{ - 1.1, - 2.2, - 3.3, - 4.4, - 5.5, - -1.1, - -2.2, - -3.3, + items: []int{ + 1, + 2, + 3, + 4, + 5, + -1, + -2, + -3, }, - onItem: func(_ context.Context, f float64) (float64, error) { - return math.Round(f), nil + onItem: func(_ context.Context, i int) (int, error) { + return i * 2, nil }, }, - want: 10, + want: 18, wantErr: require.NoError, }, { name: "error in series", field: field{ numWorkers: 1, - items: []float64{ + items: []int{ 1, 2, 3, }, - onItem: func(_ context.Context, f float64) (float64, error) { + onItem: func(_ context.Context, _ int) (int, error) { return 0, fmt.Errorf("error") }, }, @@ -88,11 +87,11 @@ func TestPipeline_Do(t *testing.T) { name: "error in parallel", field: field{ numWorkers: 3, - items: []float64{ + items: []int{ 1, 2, }, - onItem: func(_ context.Context, f float64) (float64, error) { + onItem: func(_ context.Context, _ int) (int, error) { return 0, fmt.Errorf("error") }, }, @@ -101,8 +100,8 @@ func TestPipeline_Do(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - var got float64 - p := parallel.NewPipeline(tt.field.numWorkers, false, tt.field.items, tt.field.onItem, func(f float64) error { + var got int + p := parallel.NewPipeline(tt.field.numWorkers, false, tt.field.items, tt.field.onItem, func(f int) error { got += f return nil })