...

Source file src/go.etcd.io/etcd/client/pkg/v3/testutil/testingtb.go

Documentation: go.etcd.io/etcd/client/pkg/v3/testutil

     1  // Copyright 2021 The etcd Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package testutil
    16  
    17  import (
    18  	"io/ioutil"
    19  	"log"
    20  	"os"
    21  )
    22  
    23  // TB is a subset of methods of testing.TB interface.
    24  // We cannot implement testing.TB due to protection, so we expose this simplified interface.
    25  type TB interface {
    26  	Cleanup(func())
    27  	Error(args ...interface{})
    28  	Errorf(format string, args ...interface{})
    29  	Fail()
    30  	FailNow()
    31  	Failed() bool
    32  	Fatal(args ...interface{})
    33  	Fatalf(format string, args ...interface{})
    34  	Logf(format string, args ...interface{})
    35  	Name() string
    36  	TempDir() string
    37  	Helper()
    38  	Skip(args ...interface{})
    39  }
    40  
    41  // NewTestingTBProthesis creates a fake variant of testing.TB implementation.
    42  // It's supposed to be used in contexts were real testing.T is not provided,
    43  // e.g. in 'examples'.
    44  //
    45  // The `closef` goroutine should get executed when tb will not be needed any longer.
    46  //
    47  // The provided implementation is NOT thread safe (Cleanup() method).
    48  func NewTestingTBProthesis(name string) (tb TB, closef func()) {
    49  	testtb := &testingTBProthesis{name: name}
    50  	return testtb, testtb.close
    51  }
    52  
    53  type testingTBProthesis struct {
    54  	name     string
    55  	failed   bool
    56  	cleanups []func()
    57  }
    58  
    59  func (t *testingTBProthesis) Helper() {
    60  	// Ignored
    61  }
    62  
    63  func (t *testingTBProthesis) Skip(args ...interface{}) {
    64  	t.Log(append([]interface{}{"Skipping due to: "}, args...))
    65  }
    66  
    67  func (t *testingTBProthesis) Cleanup(f func()) {
    68  	t.cleanups = append(t.cleanups, f)
    69  }
    70  
    71  func (t *testingTBProthesis) Error(args ...interface{}) {
    72  	log.Println(args...)
    73  	t.Fail()
    74  }
    75  
    76  func (t *testingTBProthesis) Errorf(format string, args ...interface{}) {
    77  	log.Printf(format, args...)
    78  	t.Fail()
    79  }
    80  
    81  func (t *testingTBProthesis) Fail() {
    82  	t.failed = true
    83  }
    84  
    85  func (t *testingTBProthesis) FailNow() {
    86  	t.failed = true
    87  	panic("FailNow() called")
    88  }
    89  
    90  func (t *testingTBProthesis) Failed() bool {
    91  	return t.failed
    92  }
    93  
    94  func (t *testingTBProthesis) Fatal(args ...interface{}) {
    95  	log.Fatalln(args...)
    96  }
    97  
    98  func (t *testingTBProthesis) Fatalf(format string, args ...interface{}) {
    99  	log.Fatalf(format, args...)
   100  }
   101  
   102  func (t *testingTBProthesis) Logf(format string, args ...interface{}) {
   103  	log.Printf(format, args...)
   104  }
   105  
   106  func (t *testingTBProthesis) Log(args ...interface{}) {
   107  	log.Println(args...)
   108  }
   109  
   110  func (t *testingTBProthesis) Name() string {
   111  	return t.name
   112  }
   113  
   114  func (t *testingTBProthesis) TempDir() string {
   115  	dir, err := ioutil.TempDir("", t.name)
   116  	if err != nil {
   117  		t.Fatal(err)
   118  	}
   119  	t.cleanups = append([]func(){func() {
   120  		t.Logf("Cleaning UP: %v", dir)
   121  		os.RemoveAll(dir)
   122  	}}, t.cleanups...)
   123  	return dir
   124  }
   125  
   126  func (t *testingTBProthesis) close() {
   127  	for i := len(t.cleanups) - 1; i >= 0; i-- {
   128  		t.cleanups[i]()
   129  	}
   130  }
   131  

View as plain text