Use gofumpt as code formatter

This commit is contained in:
eikendev 2022-04-20 22:08:21 +02:00
parent 04e29f04d9
commit 0b8d22d293
No known key found for this signature in database
GPG key ID: A1BDB1B28C8EF694
12 changed files with 23 additions and 30 deletions

View file

@ -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

View file

@ -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) {

View file

@ -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 {

View file

@ -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)
}
}

View file

@ -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 {

View file

@ -62,5 +62,4 @@ func TestApi_IsCurrentUser(t *testing.T) {
}
}
}
}

View file

@ -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

View file

@ -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 {

View file

@ -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)
}
}

View file

@ -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
}

View file

@ -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
}

View file

@ -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