mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-06-27 00:46:18 +00:00
This adds a test logger for libevm, that treats errors as they _should_
be treated.
- Logs at `log.LevelWarn` and above trigger `t.Errorf()`, causing tests
to fail
- Critical logs fail immediately: Logs at `log.LevelCrit` trigger
`t.Fatalf()`
- Info/debug logs pass through: Lower severity logs use `t.Logf()` for
informational output
You can use the logger like this:
```go
import (
"github.com/ava-labs/libevm/ethtest"
"github.com/ava-labs/libevm/log"
)
func TestSomething(t *testing.T) {
logger := log.NewLogger(ethtest.NewTBLogHandler(t, log.LevelDebug))
// Or to set globally
log.SetDefault(log.NewLogger(ethtest.NewTBLogHandler(t, log.LevelDebug))
}
```
I was thinking about adding tests to show this works, but it seemed
silly to test a testing utility.
---------
Signed-off-by: Jonathan Oppenheimer <147infiniti@gmail.com>
Co-authored-by: Arran Schlosberg <519948+ARR4N@users.noreply.github.com>
57 lines
2 KiB
Go
57 lines
2 KiB
Go
// Copyright 2024-2025 the libevm authors.
|
|
//
|
|
// The libevm additions to go-ethereum are free software: you can redistribute
|
|
// them and/or modify them under the terms of the GNU Lesser General Public License
|
|
// as published by the Free Software Foundation, either version 3 of the License,
|
|
// or (at your option) any later version.
|
|
//
|
|
// The libevm additions are distributed in the hope that they will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
|
|
// General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
// along with the go-ethereum library. If not, see
|
|
// <http://www.gnu.org/licenses/>.
|
|
|
|
package hookstest
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/ava-labs/libevm/common"
|
|
"github.com/ava-labs/libevm/core"
|
|
"github.com/ava-labs/libevm/core/rawdb"
|
|
"github.com/ava-labs/libevm/libevm/ethtest"
|
|
"github.com/ava-labs/libevm/log"
|
|
"github.com/ava-labs/libevm/params"
|
|
)
|
|
|
|
func TestSetupGenesisBlockWithStub(t *testing.T) {
|
|
// The original bug was due to [Stub] resulting in an error when being
|
|
// marshalled to JSON for [rawdb.WriteChainConfig], which resulted in
|
|
// [log.Crit] being called.
|
|
l := log.Root()
|
|
t.Cleanup(func() { log.SetDefault(l) })
|
|
log.SetDefault(log.NewLogger(ethtest.NewTBLogHandler(t, log.LevelDebug)))
|
|
|
|
stub := &Stub{}
|
|
extras := stub.Register(t)
|
|
|
|
config := ¶ms.ChainConfig{}
|
|
extras.ChainConfig.Set(config, stub)
|
|
gen := &core.Genesis{
|
|
Config: config,
|
|
}
|
|
|
|
db, cache, _ := ethtest.NewEmptyStateDB(t)
|
|
|
|
// An eventual call to this function was the root cause.
|
|
rawdb.WriteChainConfig(db, common.Hash{1, 2, 3, 4, 5, 6}, config)
|
|
|
|
// Also check calls to this function, which was the desired behaviour.
|
|
_, _, err := core.SetupGenesisBlock(db, cache.TrieDB(), gen)
|
|
require.NoError(t, err, "core.SetupGenesisBlock([%T with %T as registered %T extra])", gen, stub, config)
|
|
}
|