Skip to content

Commit

Permalink
Extract two more helpers (FerretDB#875)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekSi authored Jul 13, 2022
1 parent 3b7e6f0 commit 0d32d56
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 26 deletions.
6 changes: 4 additions & 2 deletions .golangci-new.yml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -27,7 +29,6 @@ linters:
- misspell
- nolintlint
- revive
- staticcheck
- unused
- whitespace

Expand Down Expand Up @@ -110,3 +111,4 @@ linters:

issues:
exclude-use-default: false
new-from-rev: origin/main
9 changes: 4 additions & 5 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions integration/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
Expand Down
13 changes: 11 additions & 2 deletions internal/handlers/common/msg_hostinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"os"
"runtime"
"strconv"
"strings"
"time"

"github.com/FerretDB/FerretDB/internal/types"
Expand Down Expand Up @@ -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(
Expand All @@ -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,
)),
Expand Down
48 changes: 48 additions & 0 deletions internal/util/testutil/db.go
Original file line number Diff line number Diff line change
@@ -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
}
22 changes: 8 additions & 14 deletions internal/util/testutil/pg.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package testutil
import (
"context"
"errors"
"strings"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 0d32d56

Please sign in to comment.