From 0b8d22d293437aef6cedc2d4f643c21cc29365c5 Mon Sep 17 00:00:00 2001 From: eikendev Date: Wed, 20 Apr 2022 22:08:21 +0200 Subject: [PATCH] Use gofumpt as code formatter --- Makefile | 9 +++++++-- internal/api/application_test.go | 16 ++++++++-------- internal/api/notification.go | 3 +-- internal/api/notification_test.go | 2 -- internal/api/user_test.go | 1 - internal/api/util_test.go | 1 - internal/authentication/credentials/password.go | 2 -- internal/configuration/configuration_test.go | 8 ++++---- internal/database/database.go | 2 +- internal/dispatcher/notification.go | 3 --- tests/mockups/config.go | 3 +-- tests/mockups/dispatcher.go | 3 +-- 12 files changed, 23 insertions(+), 30 deletions(-) diff --git a/Makefile b/Makefile index fc97081..626a8cf 100644 --- a/Makefile +++ b/Makefile @@ -26,7 +26,7 @@ clean: .PHONY: test test: touch $(SEMGREP_MODFILE) # See [1]. - go fmt ./... + stdout=$$(gofumpt -l . 2>&1); if [ "$$stdout" ]; then exit 1; fi go vet ./... gocyclo -over 10 $(shell find . -type f \( -iname '*.go' ! -path "./tests/semgrep-rules/*" \)) staticcheck ./... @@ -42,9 +42,14 @@ setup: go install github.com/fzipp/gocyclo/cmd/gocyclo@latest go install github.com/securego/gosec/v2/cmd/gosec@latest go install github.com/swaggo/swag/cmd/swag@latest - go install honnef.co/go/tools/cmd/staticcheck@v0.2.2 + go install honnef.co/go/tools/cmd/staticcheck@latest + go install mvdan.cc/gofumpt@latest poetry install +.PHONY: fmt +fmt: + gofumpt -l -w . + .PHONY: swag swag: swag init --parseDependency=true --exclude $(TESTS_DIR) -g cmd/pushbits/main.go diff --git a/internal/api/application_test.go b/internal/api/application_test.go index 267bfd0..8ee30ff 100644 --- a/internal/api/application_test.go +++ b/internal/api/application_test.go @@ -19,12 +19,14 @@ import ( "github.com/stretchr/testify/require" ) -var TestApplicationHandler *ApplicationHandler -var TestUsers []*model.User -var TestDatabase *database.Database -var TestNotificationHandler *NotificationHandler -var TestUserHandler *UserHandler -var TestConfig *configuration.Configuration +var ( + TestApplicationHandler *ApplicationHandler + TestUsers []*model.User + TestDatabase *database.Database + TestNotificationHandler *NotificationHandler + TestUserHandler *UserHandler + TestConfig *configuration.Configuration +) // Collect all created applications to check & delete them later var SuccessAplications map[uint][]model.Application @@ -95,7 +97,6 @@ func TestApi_RegisterApplicationWithoutUser(t *testing.T) { } assert.Panicsf(func() { TestApplicationHandler.CreateApplication(c) }, "CreateApplication did not panic altough user is not in context") - } func TestApi_RegisterApplication(t *testing.T) { @@ -186,7 +187,6 @@ func TestApi_GetApplicationsWithoutUser(t *testing.T) { } assert.Panicsf(func() { TestApplicationHandler.GetApplications(c) }, "GetApplications did not panic altough user is not in context") - } func TestApi_GetApplicationErrors(t *testing.T) { diff --git a/internal/api/notification.go b/internal/api/notification.go index 2d2aaa9..3095f56 100644 --- a/internal/api/notification.go +++ b/internal/api/notification.go @@ -13,8 +13,7 @@ import ( ) // The NotificationDatabase interface for encapsulating database access. -type NotificationDatabase interface { -} +type NotificationDatabase interface{} // The NotificationDispatcher interface for relaying notifications. type NotificationDispatcher interface { diff --git a/internal/api/notification_test.go b/internal/api/notification_test.go index f9f9527..4d483e3 100644 --- a/internal/api/notification_test.go +++ b/internal/api/notification_test.go @@ -61,7 +61,6 @@ func TestApi_CreateNotification(t *testing.T) { assert.Equalf(w.Code, req.ShouldStatus, "(Test case: \"%s\") Expected status code %v but have %v.", req.Name, req.ShouldStatus, w.Code) } - } func TestApi_DeleteNotification(t *testing.T) { @@ -93,5 +92,4 @@ func TestApi_DeleteNotification(t *testing.T) { assert.Equalf(w.Code, req.ShouldStatus, "(Test case: \"%s\") Expected status code %v but have %v.", req.Name, req.ShouldStatus, w.Code) } - } diff --git a/internal/api/user_test.go b/internal/api/user_test.go index 727600c..9f7bc50 100644 --- a/internal/api/user_test.go +++ b/internal/api/user_test.go @@ -216,7 +216,6 @@ func TestApi_DeleteUser(t *testing.T) { assert.Equalf(testCase.ShouldStatus, w.Code, "(Test case %s) Expected status code %d but have %d", testCase.Name, testCase.ShouldStatus, w.Code) } - } func getAdmin() *model.User { diff --git a/internal/api/util_test.go b/internal/api/util_test.go index af0b6e0..3dc2bb8 100644 --- a/internal/api/util_test.go +++ b/internal/api/util_test.go @@ -62,5 +62,4 @@ func TestApi_IsCurrentUser(t *testing.T) { } } } - } diff --git a/internal/authentication/credentials/password.go b/internal/authentication/credentials/password.go index 5474e2c..15484ce 100644 --- a/internal/authentication/credentials/password.go +++ b/internal/authentication/credentials/password.go @@ -20,7 +20,6 @@ func (m *Manager) CreatePasswordHash(password string) ([]byte, error) { } hash, err := argon2id.CreateHash(password, m.argon2Params) - if err != nil { log.L.Fatal(err) panic(err) @@ -32,7 +31,6 @@ func (m *Manager) CreatePasswordHash(password string) ([]byte, error) { // ComparePassword compares a hashed password with its possible plaintext equivalent. func ComparePassword(hash, password []byte) bool { match, err := argon2id.ComparePasswordAndHash(string(password), string(hash)) - if err != nil { log.L.Fatal(err) return false diff --git a/internal/configuration/configuration_test.go b/internal/configuration/configuration_test.go index 988661a..b4294a5 100644 --- a/internal/configuration/configuration_test.go +++ b/internal/configuration/configuration_test.go @@ -146,7 +146,7 @@ func writeMinimalConfig() error { return err } - return ioutil.WriteFile("config_unittest.yml", configString, 0644) + return ioutil.WriteFile("config_unittest.yml", configString, 0o644) } // Writes a config with default values to config.yml @@ -174,13 +174,13 @@ func writeValidConfig() error { return err } - return ioutil.WriteFile("config_unittest.yml", configString, 0644) + return ioutil.WriteFile("config_unittest.yml", configString, 0o644) } // Writes a config that is empty func writeEmptyConfig() error { cleanUp() - return ioutil.WriteFile("config_unittest.yml", []byte(""), 0644) + return ioutil.WriteFile("config_unittest.yml", []byte(""), 0o644) } // Writes a config with invalid entries @@ -197,7 +197,7 @@ func writeInvalidConfig() error { return err } - return ioutil.WriteFile("config_unittest.yml", configString, 0644) + return ioutil.WriteFile("config_unittest.yml", configString, 0o644) } func cleanUp() error { diff --git a/internal/database/database.go b/internal/database/database.go index 45d4aaf..f1482fb 100644 --- a/internal/database/database.go +++ b/internal/database/database.go @@ -28,7 +28,7 @@ func createFileDir(file string) { if _, err := os.Stat(dir); os.IsNotExist(err) { // nosemgrep: tests.semgrep-rules.go.lang.correctness.permissions.incorrect-default-permission - if err := os.MkdirAll(dir, 0750); err != nil { + if err := os.MkdirAll(dir, 0o750); err != nil { panic(err) } } diff --git a/internal/dispatcher/notification.go b/internal/dispatcher/notification.go index 56a0ca5..66e9578 100644 --- a/internal/dispatcher/notification.go +++ b/internal/dispatcher/notification.go @@ -84,7 +84,6 @@ func (d *Dispatcher) DeleteNotification(a *model.Application, n *model.DeleteNot // get the message we want to delete deleteMessage, err := d.getMessage(a, n.ID) - if err != nil { log.L.Println(err) return pberrors.ErrorMessageNotFound @@ -219,7 +218,6 @@ func (d *Dispatcher) replaceMessage(a *model.Application, newBody, newFormattedB } sendEvent, err := d.mautrixClient.SendMessageEvent(mId.RoomID(a.MatrixID), event.EventMessage, &replaceEvent) - if err != nil { log.L.Println(err) return nil, err @@ -231,7 +229,6 @@ func (d *Dispatcher) replaceMessage(a *model.Application, newBody, newFormattedB // Sends a notification in response to another matrix message event func (d *Dispatcher) respondToMessage(a *model.Application, body, formattedBody string, respondMessage *event.Event) (*mautrix.RespSendEvent, error) { oldBody, oldFormattedBody, err := bodiesFromMessage(respondMessage) - if err != nil { return nil, err } diff --git a/tests/mockups/config.go b/tests/mockups/config.go index 8aad161..2b010e6 100644 --- a/tests/mockups/config.go +++ b/tests/mockups/config.go @@ -23,12 +23,11 @@ func ReadConfig(filename string, removeFile bool) (config *configuration.Configu } file, err := ioutil.ReadFile(filename) - if err != nil { return nil, err } - err = ioutil.WriteFile("config.yml", file, 0644) + err = ioutil.WriteFile("config.yml", file, 0o644) if err != nil { return nil, err } diff --git a/tests/mockups/dispatcher.go b/tests/mockups/dispatcher.go index af66dce..4441414 100644 --- a/tests/mockups/dispatcher.go +++ b/tests/mockups/dispatcher.go @@ -7,8 +7,7 @@ import ( ) // MockDispatcher is a dispatcher used for testing - it does not need any storage interface -type MockDispatcher struct { -} +type MockDispatcher struct{} func (d *MockDispatcher) RegisterApplication(id uint, name, token, user string) (string, error) { return fmt.Sprintf("%d-%s", id, name), nil