From a999db8cc1ee1bda894c833fc3cee90fab46d9c0 Mon Sep 17 00:00:00 2001 From: ligun0805 Date: Tue, 6 May 2025 20:08:22 +0300 Subject: [PATCH] update readme & test --- README.md | 2 ++ cmd/geth/dumpbalancescmd_test.go | 55 ++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 cmd/geth/dumpbalancescmd_test.go diff --git a/README.md b/README.md index 9ccfe933aa..53fc5afc33 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,8 @@ directory. | `evm` | Developer utility version of the EVM (Ethereum Virtual Machine) that is capable of running bytecode snippets within a configurable environment and execution mode. Its purpose is to allow isolated, fine-grained debugging of EVM opcodes (e.g. `evm --code 60ff60ff --debug run`). | | `rlpdump` | Developer utility tool to convert binary RLP ([Recursive Length Prefix](https://ethereum.org/en/developers/docs/data-structures-and-encoding/rlp)) dumps (data encoding used by the Ethereum protocol both network as well as consensus wise) to user-friendlier hierarchical representation (e.g. `rlpdump --hex CE0183FFFFFFC4C304050583616263`). | +| **`dump-balances`** | Export all non-zero accounts from the current state trie to a file `addresses_balances.txt`. The output is a two-column list (address and ETH balance with 6 decimal places), sorted in descending order by balance. | + ## Running `geth` Going through all the possible command line flags is out of scope here (please consult our diff --git a/cmd/geth/dumpbalancescmd_test.go b/cmd/geth/dumpbalancescmd_test.go new file mode 100644 index 0000000000..ddcdb39f10 --- /dev/null +++ b/cmd/geth/dumpbalancescmd_test.go @@ -0,0 +1,55 @@ +package main + +import ( + "bytes" + "strings" + "testing" + + "github.com/urfave/cli/v2" +) + +// TestDumpBalancesCommandRegistered checks that our dump-balances +// command is present in app.Commands with the correct Usage text. +func TestDumpBalancesCommandRegistered(t *testing.T) { + found := false + for _, cmd := range app.Commands { + if cmd.Name == dumpBalancesCommand.Name { + found = true + // Ensure that the Usage string mentions "non-zero accounts" + if !strings.Contains(cmd.Usage, "non-zero accounts") { + t.Errorf("Usage for %q does not include expected text, got %q", cmd.Name, cmd.Usage) + } + break + } + } + if !found { + t.Fatalf("command %q is not registered in app.Commands", dumpBalancesCommand.Name) + } +} + +// TestDumpBalancesHelpInProcess simulates running `geth dump-balances --help` +// in memory and verifies that help output includes the command name and its Usage. +func TestDumpBalancesHelpInProcess(t *testing.T) { + // Create a fresh CLI app with our commands + app := cli.NewApp() + app.Commands = append([]*cli.Command{}, app.Commands...) // copy existing commands + // No need to re-register dumpBalancesCommand because init() already ran + + // Capture the help output in a buffer + buf := &bytes.Buffer{} + app.Writer = buf + + // Run the help command; cli returns flag.ErrHelp on --help + err := app.Run([]string{"geth", "dump-balances", "--help"}) + if err == nil { + t.Fatalf("expected error when running help, got nil") + } + + output := buf.String() + if !strings.Contains(output, "dump-balances") { + t.Errorf("help output missing command name; got:\n%s", output) + } + if !strings.Contains(output, "non-zero accounts") { + t.Errorf("help output missing Usage description; got:\n%s", output) + } +}