github.com/cockroachdb/pebble@v1.1.1-0.20240513155919-3622ade60459/internal/metamorphic/metarunner/main.go (about)

     1  // Copyright 2023 The LevelDB-Go and Pebble Authors. All rights reserved. Use
     2  // of this source code is governed by a BSD-style license that can be found in
     3  // the LICENSE file.
     4  
     5  // metarunner is a utility which runs metamorphic.RunOnce or Compare. It is
     6  // equivalent to executing `internal/metamorphic.TestMeta` with `--run-dir` or
     7  // `--compare`. It is used for code coverage instrumentation.
     8  package main
     9  
    10  import (
    11  	"flag"
    12  	"fmt"
    13  	"os"
    14  	"path/filepath"
    15  	"strings"
    16  
    17  	"github.com/cockroachdb/pebble/internal/metamorphic/metaflags"
    18  	"github.com/cockroachdb/pebble/metamorphic"
    19  )
    20  
    21  var runOnceFlags = metaflags.InitRunOnceFlags()
    22  var _ = flag.String("test.run", "", `ignored; used for compatibility with TestMeta`)
    23  
    24  func main() {
    25  	flag.Parse()
    26  	onceOpts := runOnceFlags.MakeRunOnceOptions()
    27  	t := &mockT{}
    28  	switch {
    29  	case runOnceFlags.Compare != "":
    30  		runDirs := strings.Split(runOnceFlags.Compare, ",")
    31  		metamorphic.Compare(t, runOnceFlags.Dir, runOnceFlags.Seed, runDirs, onceOpts...)
    32  
    33  	case runOnceFlags.RunDir != "":
    34  		// The --run-dir flag is specified either in the child process (see
    35  		// runOptions() below) or the user specified it manually in order to re-run
    36  		// a test.
    37  		metamorphic.RunOnce(t, runOnceFlags.RunDir, runOnceFlags.Seed, filepath.Join(runOnceFlags.RunDir, "history"), onceOpts...)
    38  
    39  	default:
    40  		t.Errorf("--compare or --run-dir must be used")
    41  	}
    42  
    43  	if t.Failed() {
    44  		// Make sure we return an error code.
    45  		t.FailNow()
    46  	}
    47  }
    48  
    49  type mockT struct {
    50  	failed bool
    51  }
    52  
    53  var _ metamorphic.TestingT = (*mockT)(nil)
    54  
    55  func (t *mockT) Errorf(format string, args ...interface{}) {
    56  	t.failed = true
    57  	fmt.Fprintf(os.Stderr, format+"\n", args...)
    58  }
    59  
    60  func (t *mockT) FailNow() {
    61  	os.Exit(2)
    62  }
    63  
    64  func (t *mockT) Failed() bool {
    65  	return t.failed
    66  }