Skip to content

Commit

Permalink
Merge pull request #735 from ChengenH/main
Browse files Browse the repository at this point in the history
chore: use errors.New to replace fmt.Errorf with no parameters will much better
  • Loading branch information
ahrtr authored and Elbehery committed Apr 22, 2024
2 parents 5ae7bc6 + b005c0c commit 98f38f4
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ You can retrieve an existing bucket using the `Tx.Bucket()` function:
db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("MyBucket"))
if b == nil {
return fmt.Errorf("bucket does not exist")
return errors.New("bucket does not exist")
}
return nil
})
Expand Down
11 changes: 9 additions & 2 deletions cmd/bbolt/command_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@ import (
"go.etcd.io/bbolt/internal/guts_cli"
)

var pageId uint64

func newCheckCommand() *cobra.Command {
checkCmd := &cobra.Command{
Use: "check <bbolt-file>",
Short: "verify integrity of bbolt database data",
Args: cobra.ExactArgs(1),
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return checkFunc(cmd, args[0])
},
}

checkCmd.Flags().Uint64VarP(&pageId, "pageId", "", pageId, "page Id")
return checkCmd
}

Expand All @@ -37,10 +40,14 @@ func checkFunc(cmd *cobra.Command, dbPath string) error {
}
defer db.Close()

opts := []bolt.CheckOption{bolt.WithKVStringer(CmdKvStringer())}
if pageId != 0 {
opts = append(opts, bolt.WithPageId(pageId))
}
// Perform consistency check.
return db.View(func(tx *bolt.Tx) error {
var count int
for err := range tx.Check(bolt.WithKVStringer(CmdKvStringer())) {
for err := range tx.Check(opts...) {
fmt.Fprintln(cmd.OutOrStdout(), err)
count++
}
Expand Down
70 changes: 52 additions & 18 deletions cmd/bbolt/command_check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,66 @@ package main_test

import (
"bytes"
"fmt"
"io"
"testing"

"github.com/stretchr/testify/require"

main "go.etcd.io/bbolt/cmd/bbolt"
"go.etcd.io/bbolt/internal/btesting"
"go.etcd.io/bbolt/internal/guts_cli"
)

func TestCheckCommand_Run(t *testing.T) {
db := btesting.MustCreateDB(t)
db.Close()
defer requireDBNoChange(t, dbData(t, db.Path()), db.Path())

rootCmd := main.NewRootCommand()
// capture output for assertion
outputBuf := bytes.NewBufferString("")
rootCmd.SetOut(outputBuf)

rootCmd.SetArgs([]string{
"check", db.Path(),
})
err := rootCmd.Execute()
require.NoError(t, err)

output, err := io.ReadAll(outputBuf)
require.NoError(t, err)
require.Equalf(t, "OK\n", string(output), "unexpected stdout:\n\n%s", string(output))
testCases := []struct {
name string
args []string
expErr error
expOutput string
}{
{
name: "check whole db",
args: []string{"check", "path"},
expErr: nil,
expOutput: "OK\n",
},
{
name: "check valid pageId",
args: []string{"check", "path", "--pageId", "3"},
expErr: nil,
expOutput: "OK\n",
},
{
name: "check invalid pageId",
args: []string{"check", "path", "--pageId", "1"},
expErr: guts_cli.ErrCorrupt,
expOutput: fmt.Sprintf("page ID (%v) out of range [2, 4)", 1),
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {

t.Log("Creating sample DB")
db := btesting.MustCreateDB(t)
db.Close()
defer requireDBNoChange(t, dbData(t, db.Path()), db.Path())

t.Log("Running check cmd")
rootCmd := main.NewRootCommand()
outputBuf := bytes.NewBufferString("") // capture output for assertion
rootCmd.SetOut(outputBuf)

tc.args[1] = db.Path() // path to be replaced with db.Path()
rootCmd.SetArgs(tc.args)
err := rootCmd.Execute()
require.Equal(t, tc.expErr, err)

t.Log("Checking output")
output, err := io.ReadAll(outputBuf)
require.NoError(t, err)
require.Containsf(t, string(output), tc.expOutput, "unexpected stdout:\n\n%s", string(output))
})
}
}
2 changes: 1 addition & 1 deletion cmd/bbolt/command_surgery.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (o *surgeryBaseOptions) AddFlags(fs *pflag.FlagSet) {

func (o *surgeryBaseOptions) Validate() error {
if o.outputDBFilePath == "" {
return fmt.Errorf("output database path wasn't given, specify output database file path with --output option")
return errors.New("output database path wasn't given, specify output database file path with --output option")
}
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/bbolt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ func (cmd *pageItemCommand) Run(args ...string) error {
}

if options.keyOnly && options.valueOnly {
return fmt.Errorf("The --key-only or --value-only flag may be set, but not both.")
return errors.New("The --key-only or --value-only flag may be set, but not both.")
}

// Require database path and page id.
Expand Down Expand Up @@ -1675,7 +1675,7 @@ func (cmd *compactCommand) Run(args ...string) (err error) {
} else if err != nil {
return err
} else if cmd.DstPath == "" {
return fmt.Errorf("output file required")
return errors.New("output file required")
}

// Require database paths.
Expand Down
2 changes: 1 addition & 1 deletion db.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ func (db *DB) mmapSize(size int) (int, error) {

// Verify the requested size is not above the maximum allowed.
if size > maxMapSize {
return 0, fmt.Errorf("mmap too large")
return 0, errors.New("mmap too large")
}

// If larger than 1GB then grow by 1GB at a time.
Expand Down

0 comments on commit 98f38f4

Please sign in to comment.