From d76143ccb88b74bfc13468843aeb318f49d89354 Mon Sep 17 00:00:00 2001 From: Thomas Kohler Date: Tue, 13 Feb 2024 14:19:03 +0100 Subject: [PATCH] build(linter): update linter to v1.56.1 and fix issues (#1068) --- .circleci/config.yml | 2 +- .github/workflows/golangci-lint.yml | 2 +- .golangci.yml | 8 ++++++++ drivers/aio/temperature_sensor_driver_test.go | 9 +++++---- drivers/i2c/ads1x15_driver_test.go | 8 ++++---- drivers/i2c/wiichuck_driver_test.go | 4 ++-- platforms/joystick/bin/scanner.go | 2 +- platforms/joystick/joystick_driver.go | 8 ++++---- platforms/parrot/bebop/client/client.go | 2 -- robot_test.go | 9 ++++++++- 10 files changed, 34 insertions(+), 20 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 749a8d12..168a145f 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -61,7 +61,7 @@ jobs: "fmt_check_examples": docker: - - image: golangci/golangci-lint:v1.55.2 + - image: golangci/golangci-lint:v1.56.1 steps: - checkout - run: diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml index 0e7a965c..e1489e64 100644 --- a/.github/workflows/golangci-lint.yml +++ b/.github/workflows/golangci-lint.yml @@ -24,7 +24,7 @@ jobs: uses: golangci/golangci-lint-action@v3 with: # Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version - version: v1.55.2 + version: v1.56.1 # Optional: working directory, useful for monorepos # working-directory: v2 diff --git a/.golangci.yml b/.golangci.yml index cced8fe1..f826bb4c 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -168,6 +168,14 @@ linters-settings: # Default: false require-specific: true + perfsprint: + # Optimizes `fmt.Errorf`. + # Default: true + errorf: false + # Optimizes `fmt.Sprintf` with only one argument + # Default: true + sprintf1: false + revive: rules: # https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#unexported-return diff --git a/drivers/aio/temperature_sensor_driver_test.go b/drivers/aio/temperature_sensor_driver_test.go index 43857b1f..a1662c1b 100644 --- a/drivers/aio/temperature_sensor_driver_test.go +++ b/drivers/aio/temperature_sensor_driver_test.go @@ -184,16 +184,17 @@ func TestTemperatureSensorWithSensorCyclicRead_PublishesError(t *testing.T) { func TestTemperatureSensorHalt_WithSensorCyclicRead(t *testing.T) { // arrange d := NewTemperatureSensorDriver(newAioTestAdaptor(), "1", WithSensorCyclicRead(10*time.Millisecond)) - done := make(chan struct{}) require.NoError(t, d.Start()) + errChan := make(chan error, 1) // act & assert go func() { - require.NoError(t, d.Halt()) - close(done) + errChan <- d.Halt() }() + // test that the halt is not blocked by any deadlock with mutex and/or channel select { - case <-done: + case err := <-errChan: + require.NoError(t, err) case <-time.After(100 * time.Millisecond): require.Fail(t, "Temperature Sensor was not halted") } diff --git a/drivers/i2c/ads1x15_driver_test.go b/drivers/i2c/ads1x15_driver_test.go index ba524af5..6df3ea8f 100644 --- a/drivers/i2c/ads1x15_driver_test.go +++ b/drivers/i2c/ads1x15_driver_test.go @@ -51,7 +51,7 @@ func TestADS1x15CommandsReadDifferenceWithDefaults(t *testing.T) { result := d.Command("ReadDifferenceWithDefaults")(ads1x15TestChannel) // assert assert.Nil(t, result.(map[string]interface{})["err"]) - assert.Equal(t, -4.096, result.(map[string]interface{})["val"]) + assert.InDelta(t, -4.096, result.(map[string]interface{})["val"], 0.0) } func TestADS1x15CommandsReadDifference(t *testing.T) { @@ -61,7 +61,7 @@ func TestADS1x15CommandsReadDifference(t *testing.T) { result := d.Command("ReadDifference")(ads1x15TestChannelGainDataRate) // assert assert.Nil(t, result.(map[string]interface{})["err"]) - assert.Equal(t, -2.048, result.(map[string]interface{})["val"]) + assert.InDelta(t, -2.048, result.(map[string]interface{})["val"], 0.0) } func TestADS1x15CommandsReadWithDefaults(t *testing.T) { @@ -71,7 +71,7 @@ func TestADS1x15CommandsReadWithDefaults(t *testing.T) { result := d.Command("ReadWithDefaults")(ads1x15TestChannel) // assert assert.Nil(t, result.(map[string]interface{})["err"]) - assert.Equal(t, -4.096, result.(map[string]interface{})["val"]) + assert.InDelta(t, -4.096, result.(map[string]interface{})["val"], 0.0) } func TestADS1x15CommandsRead(t *testing.T) { @@ -81,7 +81,7 @@ func TestADS1x15CommandsRead(t *testing.T) { result := d.Command("Read")(ads1x15TestChannelGainDataRate) // assert assert.Nil(t, result.(map[string]interface{})["err"]) - assert.Equal(t, -2.048, result.(map[string]interface{})["val"]) + assert.InDelta(t, -2.048, result.(map[string]interface{})["val"], 0.0) } func TestADS1x15CommandsAnalogRead(t *testing.T) { diff --git a/drivers/i2c/wiichuck_driver_test.go b/drivers/i2c/wiichuck_driver_test.go index 82cf3db4..654c7ced 100644 --- a/drivers/i2c/wiichuck_driver_test.go +++ b/drivers/i2c/wiichuck_driver_test.go @@ -109,7 +109,7 @@ func TestWiichuckDriverCButton(t *testing.T) { done := make(chan bool) _ = d.On(d.Event(C), func(data interface{}) { - assert.Equal(t, true, data) //nolint:testifylint // data is an interface + assert.Equal(t, true, data) done <- true }) @@ -132,7 +132,7 @@ func TestWiichuckDriverZButton(t *testing.T) { done := make(chan bool) _ = d.On(d.Event(Z), func(data interface{}) { - assert.Equal(t, true, data) //nolint:testifylint // data is an interface + assert.Equal(t, true, data) done <- true }) diff --git a/platforms/joystick/bin/scanner.go b/platforms/joystick/bin/scanner.go index 2940ed60..6bda8e44 100644 --- a/platforms/joystick/bin/scanner.go +++ b/platforms/joystick/bin/scanner.go @@ -99,7 +99,7 @@ func main() { case <-ticker.C: printAt(1, 0, "-- Press 'q' to Exit --") - printAt(1, 1, fmt.Sprintf("Joystick Name: %s", js.Name())) + printAt(1, 1, fmt.Sprintf("Joystick Name: %s", js.Name())) //nolint:perfsprint // ok here printAt(1, 2, fmt.Sprintf(" Axis Count: %d", js.AxisCount())) printAt(1, 3, fmt.Sprintf(" Button Count: %d", js.ButtonCount())) readJoystick(js) diff --git a/platforms/joystick/joystick_driver.go b/platforms/joystick/joystick_driver.go index 3af42505..22b25414 100644 --- a/platforms/joystick/joystick_driver.go +++ b/platforms/joystick/joystick_driver.go @@ -189,8 +189,8 @@ func (j *Driver) initConfig() error { func (j *Driver) initEvents() { for _, value := range j.config.Buttons { - j.AddEvent(fmt.Sprintf("%s_press", value.Name)) - j.AddEvent(fmt.Sprintf("%s_release", value.Name)) + j.AddEvent(fmt.Sprintf("%s_press", value.Name)) //nolint:perfsprint // ok here + j.AddEvent(fmt.Sprintf("%s_release", value.Name)) //nolint:perfsprint // ok here } for _, value := range j.config.Axis { j.AddEvent(value.Name) @@ -214,9 +214,9 @@ func (j *Driver) handleButtons(state js.State) error { } if buttonPressed { - j.Publish(j.Event(fmt.Sprintf("%s_press", name)), nil) + j.Publish(j.Event(fmt.Sprintf("%s_press", name)), nil) //nolint:perfsprint // ok here } else { - j.Publish(j.Event(fmt.Sprintf("%s_release", name)), nil) + j.Publish(j.Event(fmt.Sprintf("%s_release", name)), nil) //nolint:perfsprint // ok here } } } diff --git a/platforms/parrot/bebop/client/client.go b/platforms/parrot/bebop/client/client.go index 6813fe77..fc4c391c 100644 --- a/platforms/parrot/bebop/client/client.go +++ b/platforms/parrot/bebop/client/client.go @@ -194,7 +194,6 @@ func (b *Bebop) Discover() error { data := make([]byte, 10240) _, err = b.discoveryClient.Read(data) - if err != nil { return err } @@ -214,7 +213,6 @@ func (b *Bebop) Connect() error { } b.c2dClient, err = net.DialUDP("udp", nil, c2daddr) - if err != nil { return err } diff --git a/robot_test.go b/robot_test.go index 2c757162..15d4765a 100644 --- a/robot_test.go +++ b/robot_test.go @@ -55,8 +55,9 @@ func TestRobotStartAutoRun(t *testing.T) { // work, ) + errChan := make(chan error, 1) go func() { - require.NoError(t, r.Start()) + errChan <- r.Start() // if no strange things happen, this runs until os.signal occurs }() time.Sleep(10 * time.Millisecond) @@ -65,4 +66,10 @@ func TestRobotStartAutoRun(t *testing.T) { // stop it require.NoError(t, r.Stop()) assert.False(t, r.Running()) + select { + case err := <-errChan: + require.NoError(t, err) + case <-time.After(10 * time.Millisecond): + // because the Start() will run forever, until os.Signal, this is ok here + } }