Skip to content

Commit eb70f8f

Browse files
committed
chore(cmd): migrate page command to cobra style
Signed-off-by: Lavish Pal <[email protected]>
1 parent b2906c8 commit eb70f8f

File tree

4 files changed

+164
-184
lines changed

4 files changed

+164
-184
lines changed

cmd/bbolt/command_page_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package main_test
2+
3+
import (
4+
"bytes"
5+
"errors"
6+
"fmt"
7+
"io"
8+
"testing"
9+
10+
"github.com/stretchr/testify/require"
11+
12+
"go.etcd.io/bbolt"
13+
main "go.etcd.io/bbolt/cmd/bbolt"
14+
"go.etcd.io/bbolt/internal/btesting"
15+
)
16+
17+
// Ensure the "page" command neither panics nor changes the db file.
18+
func TestPageCommand_Run(t *testing.T) {
19+
t.Log("Creating sample DB")
20+
// Create a test database
21+
db := btesting.MustCreateDB(t)
22+
err := db.Update(func(tx *bbolt.Tx) error {
23+
// Create buckets and insert sample data
24+
for _, name := range []string{"foo", "bar"} {
25+
b, err := tx.CreateBucket([]byte(name))
26+
if err != nil {
27+
return err
28+
}
29+
for i := 0; i < 3; i++ {
30+
key := fmt.Sprintf("%s-%d", name, i)
31+
val := fmt.Sprintf("val-%s-%d", name, i)
32+
if err := b.Put([]byte(key), []byte(val)); err != nil {
33+
return err
34+
}
35+
}
36+
}
37+
return nil
38+
})
39+
require.NoError(t, err)
40+
db.Close()
41+
defer requireDBNoChange(t, dbData(t, db.Path()), db.Path())
42+
43+
// Running the page command
44+
t.Log("Running page cmd with a valid page ID")
45+
rootCmd := main.NewRootCommand()
46+
outputBuf := bytes.NewBufferString("")
47+
rootCmd.SetOut(outputBuf)
48+
49+
rootCmd.SetArgs([]string{"page", db.Path(), "0"})
50+
err = rootCmd.Execute()
51+
require.NoError(t, err)
52+
53+
// Check that the command does not panic and generates output
54+
t.Log("Checking output for page command")
55+
_, err = io.ReadAll(outputBuf)
56+
require.NoError(t, err)
57+
58+
// Verify output includes expected headers
59+
require.Contains(t, outputBuf.String(), "Page ID TYPE ITEMS OVRFLW")
60+
61+
// Test Case 2: Handle missing page ID argument
62+
t.Log("Running page cmd with missing page ID argument")
63+
rootCmd.SetArgs([]string{"page", db.Path()})
64+
err = rootCmd.Execute()
65+
require.ErrorContains(t, err, "page ID(s) required")
66+
67+
// Test Case 3: Handle invalid page ID argument (non-numeric)
68+
t.Log("Running page cmd with invalid page ID argument")
69+
rootCmd.SetArgs([]string{"page", db.Path(), "invalid"})
70+
err = rootCmd.Execute()
71+
require.ErrorContains(t, err, "invalid page ID")
72+
}
73+
74+
// Ensure the "page" command handles missing database path gracefully.
75+
func TestPageCommand_NoArgs(t *testing.T) {
76+
t.Log("Running page cmd with missing database path")
77+
rootCmd := main.NewRootCommand()
78+
expErr := errors.New("accepts 2 arg(s), received 1")
79+
rootCmd.SetArgs([]string{"page"})
80+
err := rootCmd.Execute()
81+
require.ErrorContains(t, err, expErr.Error())
82+
}

cmd/bbolt/main.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,6 @@ func (m *Main) Run(args ...string) error {
132132
return newInfoCommand(m).Run(args[1:]...)
133133
case "keys":
134134
return newKeysCommand(m).Run(args[1:]...)
135-
case "page":
136-
return newPageCommand(m).Run(args[1:]...)
137135
case "pages":
138136
return newPagesCommand(m).Run(args[1:]...)
139137
case "stats":

cmd/bbolt/main_test.go

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -93,34 +93,6 @@ func TestDumpCommand_Run(t *testing.T) {
9393
t.Fatalf("unexpected stdout:\n%s\n", m.Stdout.String())
9494
}
9595
}
96-
97-
func TestPageCommand_Run(t *testing.T) {
98-
db := btesting.MustCreateDBWithOption(t, &bolt.Options{PageSize: 4096})
99-
db.Close()
100-
101-
defer requireDBNoChange(t, dbData(t, db.Path()), db.Path())
102-
103-
exp := "Page ID: 0\n" +
104-
"Page Type: meta\n" +
105-
"Total Size: 4096 bytes\n" +
106-
"Overflow pages: 0\n" +
107-
"Version: 2\n" +
108-
"Page Size: 4096 bytes\n" +
109-
"Flags: 00000000\n" +
110-
"Root: <pgid=3>\n" +
111-
"Freelist: <pgid=2>\n" +
112-
"HWM: <pgid=4>\n" +
113-
"Txn ID: 0\n" +
114-
"Checksum: 07516e114689fdee\n\n"
115-
116-
m := NewMain()
117-
err := m.Run("page", db.Path(), "0")
118-
require.NoError(t, err)
119-
if m.Stdout.String() != exp {
120-
t.Fatalf("unexpected stdout:\n%s\n%s", m.Stdout.String(), exp)
121-
}
122-
}
123-
12496
func TestPageItemCommand_Run(t *testing.T) {
12597
testCases := []struct {
12698
name string

0 commit comments

Comments
 (0)