github.com/googollee/clic@v0.1.5/source/env_test.go (about)

     1  package source
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"reflect"
     7  	"testing"
     8  
     9  	"github.com/googollee/clic/structtags"
    10  )
    11  
    12  func TestEnv(t *testing.T) {
    13  	tests := []struct {
    14  		name                   string
    15  		options                []EnvOption
    16  		envs                   map[string]string
    17  		wantA1, wantA2, wantA3 string
    18  	}{
    19  		{
    20  			name:    "FromValue",
    21  			options: []EnvOption{},
    22  			envs: map[string]string{
    23  				"A1":       "123",
    24  				"L1_A2":    "abc",
    25  				"L2_L3_A3": "xyz",
    26  			},
    27  			wantA1: "123",
    28  			wantA2: "abc",
    29  			wantA3: "xyz",
    30  		},
    31  		{
    32  			name:    "FromDefault",
    33  			options: []EnvOption{},
    34  			envs:    map[string]string{},
    35  			wantA1:  "a1",
    36  			wantA2:  "a2",
    37  			wantA3:  "a3",
    38  		},
    39  		{
    40  			name:    "WithSplitter",
    41  			options: []EnvOption{EnvSplitter("__")},
    42  			envs: map[string]string{
    43  				"A1":         "123",
    44  				"L1__A2":     "abc",
    45  				"L2__L3__A3": "xyz",
    46  			},
    47  			wantA1: "123",
    48  			wantA2: "abc",
    49  			wantA3: "xyz",
    50  		},
    51  	}
    52  
    53  	for _, tc := range tests {
    54  		t.Run(tc.name, func(t *testing.T) {
    55  			src := Env(tc.options...)
    56  			a1, a2, a3 = "a1", "a2", "a3"
    57  
    58  			if err := src.Register(nil, fields); err != nil {
    59  				t.Fatalf("src.Prepare(fields) returns error: %v", err)
    60  			}
    61  
    62  			for key, value := range tc.envs {
    63  				t.Setenv(key, value)
    64  			}
    65  
    66  			if err := src.Parse(context.Background(), nil); err != nil {
    67  				t.Fatalf("src.Parse() should return no error, which is not: %v", err)
    68  			}
    69  
    70  			if got, want := a1, tc.wantA1; got != want {
    71  				t.Errorf("after src.Parse(), a1 = %q, want: %q", got, want)
    72  			}
    73  			if got, want := a2, tc.wantA2; got != want {
    74  				t.Errorf("after src.Parse(), a2 = %q, want: %q", got, want)
    75  			}
    76  			if got, want := a3, tc.wantA3; got != want {
    77  				t.Errorf("after src.Parse(), a3 = %q, want: %q", got, want)
    78  			}
    79  		})
    80  	}
    81  }
    82  
    83  func TestEnvOptionError(t *testing.T) {
    84  	tests := []struct {
    85  		name    string
    86  		options []EnvOption
    87  	}{
    88  		{"EmptySplitter", []EnvOption{EnvSplitter("")}},
    89  	}
    90  
    91  	for _, tc := range tests {
    92  		t.Run(tc.name, func(t *testing.T) {
    93  			src := Env(tc.options...)
    94  			err := src.Error()
    95  			if err == nil {
    96  				t.Errorf("src().Error() want an error, which is not")
    97  			}
    98  
    99  			errPrepare := src.Register(nil, fields)
   100  			if err != errPrepare {
   101  				t.Errorf("src().Prepare() = %v, src().Error() = %v, they should be same", errPrepare, err)
   102  			}
   103  
   104  			errParse := src.Parse(context.Background(), nil)
   105  			if err != errPrepare {
   106  				t.Errorf("src().Parse() = %v, src().Error() = %v, they should be same", errParse, err)
   107  			}
   108  		})
   109  	}
   110  }
   111  
   112  func parserWithError(v reflect.Value, str string) error {
   113  	return fmt.Errorf("error!")
   114  }
   115  
   116  func TestEnvValueError(t *testing.T) {
   117  	var i int
   118  	tests := []struct {
   119  		fields []structtags.Field
   120  		envs   map[string]string
   121  	}{
   122  		{
   123  			fields: []structtags.Field{
   124  				{Name: []string{"int"}, Description: "int", Parser: parserWithError, Value: reflect.ValueOf(&i).Elem()},
   125  			},
   126  			envs: map[string]string{"INT": "abc"}},
   127  	}
   128  
   129  	for _, tc := range tests {
   130  		t.Run(fmt.Sprintf("%v", tc.envs), func(t *testing.T) {
   131  			i = 0
   132  			src := Env()
   133  
   134  			if err := src.Register(nil, tc.fields); err != nil {
   135  				t.Errorf("src().Prepare() = %v, should be no error", err)
   136  			}
   137  
   138  			for key, value := range tc.envs {
   139  				t.Setenv(key, value)
   140  			}
   141  
   142  			if err := src.Parse(context.Background(), nil); err == nil {
   143  				t.Errorf("src().Parse() = %v, should be an error", err)
   144  			}
   145  		})
   146  	}
   147  }