github.com/wfusion/gofusion@v1.1.14/test/config/test.go (about)

     1  package config
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"reflect"
     7  	"sync"
     8  
     9  	"github.com/stretchr/testify/suite"
    10  	"go.uber.org/atomic"
    11  
    12  	"github.com/wfusion/gofusion/common/env"
    13  	"github.com/wfusion/gofusion/common/utils"
    14  	"github.com/wfusion/gofusion/log"
    15  	"github.com/wfusion/gofusion/test"
    16  )
    17  
    18  var (
    19  	component = "config"
    20  )
    21  
    22  type Test struct {
    23  	test.Suite
    24  
    25  	once  sync.Once
    26  	exits []func()
    27  
    28  	testName   string
    29  	testsLefts atomic.Int64
    30  }
    31  
    32  func (t *Test) SetupTest() {
    33  	t.Catch(func() {
    34  		log.Info(context.Background(), fmt.Sprintf("------------ %s test case begin ------------", component))
    35  
    36  		t.once.Do(func() {
    37  			t.exits = append(t.exits, t.Suite.Copy(t.AllConfigFiles(), t.testName, 1))
    38  		})
    39  	})
    40  }
    41  
    42  func (t *Test) TearDownTest() {
    43  	t.Catch(func() {
    44  		log.Info(context.Background(), fmt.Sprintf("------------ %s test case end ------------", component))
    45  		if t.testsLefts.Add(-1) == 0 {
    46  			for i := len(t.exits) - 1; i >= 0; i-- {
    47  				t.exits[i]()
    48  			}
    49  		}
    50  	})
    51  }
    52  
    53  func (t *Test) ConfigFiles() []string {
    54  	files := []string{
    55  		"app.local.yml",
    56  		"app.yml",
    57  		"app.json",
    58  		"app.toml",
    59  	}
    60  	for i := 0; i < len(files); i++ {
    61  		files[i] = env.WorkDir + "/configs/" + t.AppName() + "." + files[i]
    62  	}
    63  	return files
    64  }
    65  
    66  func (t *Test) AppName() string {
    67  	return fmt.Sprintf("%s.%s", component, t.testName)
    68  }
    69  
    70  func (t *Test) Init(testingSuite suite.TestingSuite) {
    71  	methodFinder := reflect.TypeOf(testingSuite)
    72  	numMethod := methodFinder.NumMethod()
    73  
    74  	numTestLeft := int64(0)
    75  	for i := 0; i < numMethod; i++ {
    76  		method := methodFinder.Method(i)
    77  		ok, _ := test.MethodFilter(method.Name)
    78  		if !ok {
    79  			continue
    80  		}
    81  		numTestLeft++
    82  	}
    83  	t.testName = utils.IndirectType(methodFinder).Name()
    84  	t.testsLefts.Add(numTestLeft)
    85  }