github.com/artisanhe/tools@v1.0.1-0.20210607022958-19a8fef2eb04/conf/env_var_test.go (about)

     1  package conf
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/go-courier/ptr"
     8  	"github.com/stretchr/testify/assert"
     9  
    10  	"github.com/artisanhe/tools/conf/presets"
    11  )
    12  
    13  type SubConfig struct {
    14  	Key  string `conf:"env"`
    15  	Bool bool
    16  	Func func() error
    17  }
    18  
    19  func (s SubConfig) DockerDefaults() DockerDefaults {
    20  	return DockerDefaults{
    21  		"Key":  "test",
    22  		"Bool": false,
    23  	}
    24  }
    25  
    26  func TestEnvVar(t *testing.T) {
    27  	tt := assert.New(t)
    28  
    29  	c := struct {
    30  		Array     []string
    31  		Password  presets.Password `conf:"env"`
    32  		PtrString *string          `conf:"env"`
    33  		Host      string           `validate:"@hostname"`
    34  		SubConfig
    35  	}{}
    36  
    37  	c.Password = "123456"
    38  	c.Key = "123456"
    39  	c.PtrString = ptr.String("123456")
    40  	c.Array = []string{"1", "2"}
    41  	c.Password = "host/"
    42  
    43  	rv := reflect.Indirect(reflect.ValueOf(c))
    44  
    45  	envVars, err := CollectEnvVars(rv, "T")
    46  	tt.Nil(err)
    47  
    48  	envVars.Print()
    49  }