allow test to use different file for tracer output

This commit is contained in:
Chris Ziogas 2024-03-27 14:00:20 +02:00
parent 5db20834f0
commit bc983e2afd
No known key found for this signature in database
GPG key ID: 2329CF479E36E2DA
2 changed files with 16 additions and 8 deletions

View file

@ -22,7 +22,6 @@ import (
"fmt" "fmt"
"math/big" "math/big"
"os" "os"
"path"
"path/filepath" "path/filepath"
"reflect" "reflect"
"testing" "testing"
@ -518,11 +517,15 @@ func testSupplyTracer(genesis *core.Genesis, gen func(*core.BlockGen)) ([]live.S
) )
traceOutputPath := filepath.ToSlash(os.TempDir()) traceOutputPath := filepath.ToSlash(os.TempDir())
traceOutputFilename := path.Join(traceOutputPath, "supply.jsonl") traceOutputFile, err := os.CreateTemp(traceOutputPath, "supply-*.jsonl")
defer os.Remove(traceOutputFilename) traceOutputFilename := filepath.Base(traceOutputFile.Name())
if err != nil {
return nil, nil, fmt.Errorf("failed to create temp file for supply logs: %v", err)
}
defer os.Remove(traceOutputFile.Name())
// Load supply tracer // Load supply tracer
tracer, err := tracers.LiveDirectory.New("supply", json.RawMessage(fmt.Sprintf(`{"path":"%s"}`, traceOutputPath))) tracer, err := tracers.LiveDirectory.New("supply", json.RawMessage(fmt.Sprintf(`{"path":"%s","filename":"%s"}`, traceOutputPath, traceOutputFilename)))
if err != nil { if err != nil {
return nil, nil, fmt.Errorf("failed to create call tracer: %v", err) return nil, nil, fmt.Errorf("failed to create call tracer: %v", err)
} }
@ -544,7 +547,7 @@ func testSupplyTracer(genesis *core.Genesis, gen func(*core.BlockGen)) ([]live.S
// Check and compare the results // Check and compare the results
// TODO: replace file to pass results // TODO: replace file to pass results
file, err := os.OpenFile(traceOutputFilename, os.O_RDONLY, 0666) file, err := os.OpenFile(traceOutputFile.Name(), os.O_RDONLY, 0666)
if err != nil { if err != nil {
return nil, chain, fmt.Errorf("failed to open output file: %v", err) return nil, chain, fmt.Errorf("failed to open output file: %v", err)
} }

View file

@ -51,7 +51,8 @@ type Supply struct {
type supplyTracerConfig struct { type supplyTracerConfig struct {
Path string `json:"path"` // Path to the directory where the tracer logs will be stored Path string `json:"path"` // Path to the directory where the tracer logs will be stored
MaxSize int `json:"maxSize"` // MaxSize is the maximum size in megabytes of the tracer log file before it gets rotated. It defaults to 100 megabytes. Filename string `json:"filename"` // Filename of the tracer log file. Defaults to "supply.jsonl".
MaxSize int `json:"maxsize"` // MaxSize is the maximum size in megabytes of the tracer log file before it gets rotated. It defaults to 100 megabytes.
} }
func newSupply(cfg json.RawMessage) (*tracing.Hooks, error) { func newSupply(cfg json.RawMessage) (*tracing.Hooks, error) {
@ -66,9 +67,13 @@ func newSupply(cfg json.RawMessage) (*tracing.Hooks, error) {
return nil, errors.New("supply tracer output path is required") return nil, errors.New("supply tracer output path is required")
} }
if config.Filename == "" {
config.Filename = "supply.jsonl"
}
// Store traces in a rotating file // Store traces in a rotating file
loggerOutput := &lumberjack.Logger{ loggerOutput := &lumberjack.Logger{
Filename: filepath.Join(config.Path, "supply.jsonl"), Filename: filepath.Join(config.Path, config.Filename),
} }
if config.MaxSize > 0 { if config.MaxSize > 0 {