Fix every finding surfaced by the canonical .golangci.yml with golangci-lint v2.12.2 (refs #61), behavior-preserving throughout: - err113: dynamic errors replaced with package-level sentinels and %w wrapping; direct comparisons converted to errors.Is - goprintffuncname: printf-style helpers renamed with an f suffix (ui.Writer message methods, cli.ReportErrorf, database.Fatalf, vaultik stdoutf) and all call sites updated - revive: stuttering type names renamed (blob.Handler, blob.WithReader, blob.ChunkPosition, storage.URL, storage.Info), doc comments added, unused parameters blanked, package comments added - contextcheck/noctx: ctx threaded through blob.Packer (AddChunk/Flush/FinalizeBlob/PackChunks) and scanner call sites; context-aware exec and sql variants used - funlen/cyclop/gocognit/nestif/dupl: oversized or duplicated functions split into focused helpers across production and test code - paralleltest/tparallel/thelper/usetesting/testpackage: tests parallelized where safe (global log.Initialize kept in the serial phase), helpers marked, t.TempDir adopted, external test packages where only exported API is used - gosec: integer conversions clamped or justified, header timeouts added, remaining findings suppressed with per-site justifications - mnd/goconst/lll/wsl_v5/nlreturn/noinlineerr/errcheck and other mechanical findings fixed directly Remove the deprecated log.LogOptions alias (callers migrated to log.Options). make check is green.
82 lines
1.9 KiB
Go
82 lines
1.9 KiB
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"sneak.berlin/go/vaultik/internal/types"
|
|
)
|
|
|
|
// Common fixture values shared by the internal repository tests.
|
|
const (
|
|
internalTestHost = "test-host"
|
|
internalTestSnapshotID = "test-snapshot"
|
|
internalTestFilePath = "/test.txt"
|
|
internalTestFile1 = "/file1.txt"
|
|
internalTestFile2 = "/file2.txt"
|
|
|
|
// countFilesQuery counts the rows of the files table.
|
|
countFilesQuery = "SELECT COUNT(*) FROM files"
|
|
)
|
|
|
|
// mustCreateFileRow inserts the file row, failing the test on error.
|
|
func mustCreateFileRow(t *testing.T, repos *Repositories, file *File) {
|
|
t.Helper()
|
|
|
|
err := repos.Files.Create(context.Background(), nil, file)
|
|
if err != nil {
|
|
t.Fatalf("failed to create file %s: %v", file.Path, err)
|
|
}
|
|
}
|
|
|
|
// mustAddFileToSnapshot associates a file with a snapshot, failing the
|
|
// test on error.
|
|
func mustAddFileToSnapshot(
|
|
t *testing.T, repos *Repositories, snapshotID string, fileID types.FileID,
|
|
) {
|
|
t.Helper()
|
|
|
|
err := repos.Snapshots.AddFileByID(context.Background(), nil, snapshotID, fileID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// setupTestDB creates an on-disk test database in a per-test temp
|
|
// directory and returns it along with a cleanup func that closes it.
|
|
func setupTestDB(t *testing.T) (*DB, func()) {
|
|
t.Helper()
|
|
|
|
ctx := context.Background()
|
|
dbPath := filepath.Join(t.TempDir(), "test.db")
|
|
|
|
db, err := New(ctx, dbPath)
|
|
if err != nil {
|
|
t.Fatalf("failed to create database: %v", err)
|
|
}
|
|
|
|
cleanup := func() {
|
|
err := db.Close()
|
|
if err != nil {
|
|
t.Errorf("failed to close database: %v", err)
|
|
}
|
|
}
|
|
|
|
return db, cleanup
|
|
}
|
|
|
|
// countRow runs a single-integer COUNT-style query and returns the value.
|
|
func countRow(t *testing.T, db *DB, query string, args ...any) int {
|
|
t.Helper()
|
|
|
|
var count int
|
|
|
|
err := db.conn.QueryRowContext(context.Background(), query, args...).Scan(&count)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
return count
|
|
}
|