From 0d32d568e3564de1dd464668a02efc8d55e1a513 Mon Sep 17 00:00:00 2001 From: Alexey Palazhchenko Date: Wed, 13 Jul 2022 10:29:40 +0300 Subject: [PATCH] Extract two more helpers (#875) --- .golangci-new.yml | 6 ++- .golangci.yml | 9 ++--- Taskfile.yml | 2 +- integration/setup.go | 4 +- internal/handlers/common/msg_hostinfo.go | 13 ++++++- internal/util/testutil/db.go | 48 ++++++++++++++++++++++++ internal/util/testutil/pg.go | 22 ++++------- 7 files changed, 78 insertions(+), 26 deletions(-) create mode 100644 internal/util/testutil/db.go diff --git a/.golangci-new.yml b/.golangci-new.yml index a1b81d9f782b..c5a6e40e15bc 100644 --- a/.golangci-new.yml +++ b/.golangci-new.yml @@ -1,10 +1,12 @@ --- -# New or experimental linters that should pass (or be reconfigured) for new code. +# New or experimental linters that should pass (or be reconfigured) for new code (compared to `origin/main`). run: timeout: 2m linters-settings: + staticcheck: + checks: ["all"] linters: enable-all: true @@ -27,7 +29,6 @@ linters: - misspell - nolintlint - revive - - staticcheck - unused - whitespace @@ -110,3 +111,4 @@ linters: issues: exclude-use-default: false + new-from-rev: origin/main diff --git a/.golangci.yml b/.golangci.yml index b72ec1a0c27d..f329bf9381e5 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -58,7 +58,6 @@ linters-settings: - go.mongodb.org/mongo-driver: reason: "FerretDB should not depend on MongoDB Go driver." gosimple: - go: "1.18" checks: ["all"] # govet # ineffassign @@ -79,10 +78,10 @@ linters-settings: severity: warning enableAllRules: true staticcheck: - go: "1.18" - checks: ["all"] - unused: - go: "1.18" + checks: + - all + - -SA1019 # ignore deprecation errors in existing code; new code is checked by the other configuration + # unused whitespace: multi-if: false multi-func: false diff --git a/Taskfile.yml b/Taskfile.yml index 0eb0ee9e9520..f17206869836 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -205,7 +205,7 @@ tasks: - lint-go-consistent - lint-go-consistent-integration cmds: - - bin/golangci-lint run --config=.golangci-new.yml --new-from-rev=main + - bin/golangci-lint run --config=.golangci-new.yml lint-golangci-lint: cmds: diff --git a/integration/setup.go b/integration/setup.go index 6cb65b493c65..76c1544d5376 100644 --- a/integration/setup.go +++ b/integration/setup.go @@ -67,7 +67,7 @@ func SetupWithOpts(t *testing.T, opts *SetupOpts) (context.Context, *mongo.Colle var ownDatabase bool if opts.DatabaseName == "" { - opts.DatabaseName = testutil.SchemaName(t) + opts.DatabaseName = testutil.DatabaseName(t) ownDatabase = true } @@ -85,7 +85,7 @@ func SetupWithOpts(t *testing.T, opts *SetupOpts) (context.Context, *mongo.Colle client := setupClient(t, ctx, port) db := client.Database(opts.DatabaseName) - collectionName := testutil.TableName(t) + collectionName := testutil.CollectionName(t) collection := db.Collection(collectionName) // drop remnants of the previous failed run diff --git a/internal/handlers/common/msg_hostinfo.go b/internal/handlers/common/msg_hostinfo.go index fc60badc04bb..4af787975a5c 100644 --- a/internal/handlers/common/msg_hostinfo.go +++ b/internal/handlers/common/msg_hostinfo.go @@ -19,7 +19,6 @@ import ( "os" "runtime" "strconv" - "strings" "time" "github.com/FerretDB/FerretDB/internal/types" @@ -51,6 +50,16 @@ func MsgHostInfo(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { } } + os := "unknown" + switch runtime.GOOS { + case "linux": + os = "Linux" + case "darwin": + os = "macOS" + case "windows": + os = "Windows" + } + var reply wire.OpMsg err = reply.SetSections(wire.OpMsgSection{ Documents: []*types.Document{must.NotFail(types.NewDocument( @@ -62,7 +71,7 @@ func MsgHostInfo(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) { "cpuArch", runtime.GOARCH, )), "os", must.NotFail(types.NewDocument( - "type", strings.Title(runtime.GOOS), //nolint:staticcheck // good enough for GOOS + "type", os, "name", osName, "version", osVersion, )), diff --git a/internal/util/testutil/db.go b/internal/util/testutil/db.go new file mode 100644 index 000000000000..a67ee6579f0e --- /dev/null +++ b/internal/util/testutil/db.go @@ -0,0 +1,48 @@ +// Copyright 2021 FerretDB Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package testutil + +import ( + "strings" + "testing" + + "github.com/stretchr/testify/require" +) + +// DatabaseName returns a stable database name for that test. +func DatabaseName(tb testing.TB) string { + tb.Helper() + + name := strings.ToLower(tb.Name()) + name = strings.ReplaceAll(name, "/", "-") + name = strings.ReplaceAll(name, " ", "-") + name = strings.ReplaceAll(name, "$", "-") + + require.Less(tb, len(name), 64) + return name +} + +// CollectionName returns a stable collection name for that test. +func CollectionName(tb testing.TB) string { + tb.Helper() + + name := strings.ToLower(tb.Name()) + name = strings.ReplaceAll(name, "/", "_") + name = strings.ReplaceAll(name, " ", "_") + name = strings.ReplaceAll(name, "$", "_") + + require.Less(tb, len(name), 255) + return name +} diff --git a/internal/util/testutil/pg.go b/internal/util/testutil/pg.go index 8d76258663b5..e2f27ef6f201 100644 --- a/internal/util/testutil/pg.go +++ b/internal/util/testutil/pg.go @@ -17,7 +17,6 @@ package testutil import ( "context" "errors" - "strings" "testing" "github.com/stretchr/testify/require" @@ -63,16 +62,13 @@ func Pool(ctx context.Context, tb testing.TB, opts *PoolOpts, l *zap.Logger) *pg return pool } -// SchemaName returns a stable schema name for that test. +// SchemaName should not be used. +// +// Deprecated: use DatabaseName instead. func SchemaName(tb testing.TB) string { tb.Helper() - name := strings.ToLower(tb.Name()) - name = strings.ReplaceAll(name, "/", "-") - name = strings.ReplaceAll(name, " ", "-") - - require.Less(tb, len(name), 64) - return name + return DatabaseName(tb) } // Schema creates a new FerretDB database / PostgreSQL schema for testing. @@ -109,15 +105,13 @@ func Schema(ctx context.Context, tb testing.TB, pool *pgdb.Pool) string { return schema } -// TableName returns a stable table name for that test. +// TableName should not be used. +// +// Deprecated: use CollectionName instead. func TableName(tb testing.TB) string { tb.Helper() - name := strings.ToLower(tb.Name()) - name = strings.ReplaceAll(name, "/", "_") - name = strings.ReplaceAll(name, " ", "_") - - return name + return CollectionName(tb) } // Table creates FerretDB collection / PostgreSQL table for testing.