|
| 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 | +} |
0 commit comments