github.com/ethereum/go-ethereum@v1.16.1/cmd/evm/reporter.go (about)

     1  // Copyright 2024 The go-ethereum Authors
     2  // This file is part of go-ethereum.
     3  //
     4  // go-ethereum is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // go-ethereum is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU General Public License
    15  // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package main
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  
    23  	"github.com/ethereum/go-ethereum/common"
    24  	"github.com/ethereum/go-ethereum/core/state"
    25  	"github.com/urfave/cli/v2"
    26  )
    27  
    28  const (
    29  	PASS = "\033[32mPASS\033[0m"
    30  	FAIL = "\033[31mFAIL\033[0m"
    31  )
    32  
    33  // testResult contains the execution status after running a state test, any
    34  // error that might have occurred and a dump of the final state if requested.
    35  type testResult struct {
    36  	Name  string       `json:"name"`
    37  	Pass  bool         `json:"pass"`
    38  	Root  *common.Hash `json:"stateRoot,omitempty"`
    39  	Fork  string       `json:"fork"`
    40  	Error string       `json:"error,omitempty"`
    41  	State *state.Dump  `json:"state,omitempty"`
    42  	Stats *execStats   `json:"benchStats,omitempty"`
    43  }
    44  
    45  func (r testResult) String() string {
    46  	var status string
    47  	if r.Pass {
    48  		status = fmt.Sprintf("[%s]", PASS)
    49  	} else {
    50  		status = fmt.Sprintf("[%s]", FAIL)
    51  	}
    52  	info := r.Name
    53  	m := parseTestMetadata(r.Name)
    54  	if m != nil {
    55  		info = fmt.Sprintf("%s %s, param=%s", m.module, m.function, m.parameters)
    56  	}
    57  	var extra string
    58  	if !r.Pass {
    59  		extra = fmt.Sprintf(", err=%v, fork=%s", r.Error, r.Fork)
    60  	}
    61  	out := fmt.Sprintf("%s %s%s", status, info, extra)
    62  	if r.State != nil {
    63  		state, _ := json.MarshalIndent(r.State, "", "  ")
    64  		out += "\n" + string(state)
    65  	}
    66  	return out
    67  }
    68  
    69  // report prints the after-test summary.
    70  func report(ctx *cli.Context, results []testResult) {
    71  	if ctx.Bool(HumanReadableFlag.Name) {
    72  		pass := 0
    73  		for _, r := range results {
    74  			if r.Pass {
    75  				pass++
    76  			}
    77  		}
    78  		for _, r := range results {
    79  			fmt.Println(r)
    80  		}
    81  		fmt.Println("--")
    82  		fmt.Printf("%d tests passed, %d tests failed.\n", pass, len(results)-pass)
    83  		return
    84  	}
    85  	out, _ := json.MarshalIndent(results, "", "  ")
    86  	fmt.Println(string(out))
    87  }