Skip to content

Commit

Permalink
fix: change getCommand default format auto
Browse files Browse the repository at this point in the history
Signed-off-by: Mustafa Elbehery <[email protected]>
  • Loading branch information
Elbehery committed Nov 6, 2023
1 parent 89a5a72 commit 651830d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 24 deletions.
4 changes: 2 additions & 2 deletions cmd/bbolt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -994,7 +994,7 @@ func (cmd *getCommand) Run(args ...string) error {
var parseFormat string
var format string
fs.StringVar(&parseFormat, "parse-format", "ascii-encoded", "Input format. One of: ascii-encoded|hex (default: ascii-encoded)")
fs.StringVar(&format, "format", "bytes", "Output format. One of: "+FORMAT_MODES+" (default: bytes)")
fs.StringVar(&format, "format", "auto", "Output format. One of: "+FORMAT_MODES+" (default: auto)")
help := fs.Bool("h", false, "")
if err := fs.Parse(args); err != nil {
return err
Expand Down Expand Up @@ -1064,7 +1064,7 @@ Additional options include:
--format
Output format. One of: `+FORMAT_MODES+` (default=bytes)
--parse-format
Input format (of key). One of: ascii-encoded|hex (default=ascii-encoded)"
Input format (of key). One of: ascii-encoded|hex (default=auto)"
`, "\n")
}

Expand Down
80 changes: 58 additions & 22 deletions cmd/bbolt/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,39 +313,75 @@ func TestKeysCommand_Run(t *testing.T) {

// Ensure the "get" command can print the value of a key in a bucket.
func TestGetCommand_Run(t *testing.T) {
db := btesting.MustCreateDB(t)
t.Run("test values in string format", func(t *testing.T) {
db := btesting.MustCreateDB(t)

if err := db.Update(func(tx *bolt.Tx) error {
for _, name := range []string{"foo", "bar"} {
b, err := tx.CreateBucket([]byte(name))
if err := db.Update(func(tx *bolt.Tx) error {
for _, name := range []string{"foo", "bar"} {
b, err := tx.CreateBucket([]byte(name))
if err != nil {
return err
}
for i := 0; i < 3; i++ {
key := fmt.Sprintf("%s-%d", name, i)
val := fmt.Sprintf("val-%s-%d", name, i)
if err := b.Put([]byte(key), []byte(val)); err != nil {
return err
}
}
}
return nil
}); err != nil {
t.Fatal(err)
}
db.Close()

defer requireDBNoChange(t, dbData(t, db.Path()), db.Path())

expected := "val-foo-1\n"

// Run the command.
m := NewMain()
if err := m.Run("get", db.Path(), "foo", "foo-1"); err != nil {
t.Fatal(err)
} else if actual := m.Stdout.String(); actual != expected {
t.Fatalf("unexpected stdout:\n\n%s", actual)
}
})

t.Run("test values in int64 format", func(t *testing.T) {
db := btesting.MustCreateDB(t)

if err := db.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucket([]byte("test-bucket"))
if err != nil {
return err
}
for i := 0; i < 3; i++ {
key := fmt.Sprintf("%s-%d", name, i)
val := fmt.Sprintf("val-%s-%d", name, i)
if err := b.Put([]byte(key), []byte(val)); err != nil {
for k, v := range map[string]int64{"10001": 10001,
"10002": 10002,
} {
if err := b.Put([]byte(k), convertInt64IntoBytes(t, v)); err != nil {

Check failure on line 363 in cmd/bbolt/main_test.go

View workflow job for this annotation

GitHub Actions / test-windows (windows-amd64-unit-test-4-cpu)

undefined: convertInt64IntoBytes

Check failure on line 363 in cmd/bbolt/main_test.go

View workflow job for this annotation

GitHub Actions / test-linux (linux-amd64-unit-test-1-cpu)

undefined: convertInt64IntoBytes

Check failure on line 363 in cmd/bbolt/main_test.go

View workflow job for this annotation

GitHub Actions / test-linux (linux-amd64-unit-test-2-cpu)

undefined: convertInt64IntoBytes

Check failure on line 363 in cmd/bbolt/main_test.go

View workflow job for this annotation

GitHub Actions / test-linux (linux-amd64-unit-test-4-cpu)

undefined: convertInt64IntoBytes

Check failure on line 363 in cmd/bbolt/main_test.go

View workflow job for this annotation

GitHub Actions / test-linux (linux-amd64-unit-test-4-cpu-race)

undefined: convertInt64IntoBytes
return err
}
}
return nil
}); err != nil {
t.Fatal(err)
}
return nil
}); err != nil {
t.Fatal(err)
}
db.Close()
db.Close()

defer requireDBNoChange(t, dbData(t, db.Path()), db.Path())
defer requireDBNoChange(t, dbData(t, db.Path()), db.Path())

expected := "val-foo-1\n"
expected := "10001\n"

// Run the command.
m := NewMain()
if err := m.Run("get", db.Path(), "foo", "foo-1"); err != nil {
t.Fatal(err)
} else if actual := m.Stdout.String(); actual != expected {
t.Fatalf("unexpected stdout:\n\n%s", actual)
}
// Run the command.
m := NewMain()
if err := m.Run("get", db.Path(), "test-bucket", "10001"); err != nil {
t.Fatal(err)
} else if actual := m.Stdout.String(); actual != expected {
t.Fatalf("unexpected stdout:\n\n%s", actual)
}
})
}

// Ensure the "pages" command neither panic, nor change the db file.
Expand Down

0 comments on commit 651830d

Please sign in to comment.