github.com/wangyougui/gf/v2@v2.6.5/os/genv/genv_z_unit_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/wangyougui/gf.
     6  
     7  package genv_test
     8  
     9  import (
    10  	"os"
    11  	"testing"
    12  
    13  	"github.com/wangyougui/gf/v2/frame/g"
    14  	"github.com/wangyougui/gf/v2/os/gcmd"
    15  	"github.com/wangyougui/gf/v2/os/genv"
    16  	"github.com/wangyougui/gf/v2/os/gtime"
    17  	"github.com/wangyougui/gf/v2/test/gtest"
    18  	"github.com/wangyougui/gf/v2/util/gconv"
    19  )
    20  
    21  func Test_GEnv_All(t *testing.T) {
    22  	gtest.C(t, func(t *gtest.T) {
    23  		t.Assert(os.Environ(), genv.All())
    24  	})
    25  }
    26  
    27  func Test_GEnv_Map(t *testing.T) {
    28  	gtest.C(t, func(t *gtest.T) {
    29  		value := gconv.String(gtime.TimestampNano())
    30  		key := "TEST_ENV_" + value
    31  		err := os.Setenv(key, "TEST")
    32  		t.AssertNil(err)
    33  		t.Assert(genv.Map()[key], "TEST")
    34  	})
    35  }
    36  
    37  func Test_GEnv_Get(t *testing.T) {
    38  	gtest.C(t, func(t *gtest.T) {
    39  		value := gconv.String(gtime.TimestampNano())
    40  		key := "TEST_ENV_" + value
    41  		err := os.Setenv(key, "TEST")
    42  		t.AssertNil(err)
    43  		t.AssertEQ(genv.Get(key).String(), "TEST")
    44  	})
    45  }
    46  
    47  func Test_GEnv_GetVar(t *testing.T) {
    48  	gtest.C(t, func(t *gtest.T) {
    49  		value := gconv.String(gtime.TimestampNano())
    50  		key := "TEST_ENV_" + value
    51  		err := os.Setenv(key, "TEST")
    52  		t.AssertNil(err)
    53  		t.AssertEQ(genv.Get(key).String(), "TEST")
    54  	})
    55  }
    56  
    57  func Test_GEnv_Contains(t *testing.T) {
    58  	gtest.C(t, func(t *gtest.T) {
    59  		value := gconv.String(gtime.TimestampNano())
    60  		key := "TEST_ENV_" + value
    61  		err := os.Setenv(key, "TEST")
    62  		t.AssertNil(err)
    63  		t.AssertEQ(genv.Contains(key), true)
    64  		t.AssertEQ(genv.Contains("none"), false)
    65  	})
    66  }
    67  
    68  func Test_GEnv_Set(t *testing.T) {
    69  	gtest.C(t, func(t *gtest.T) {
    70  		value := gconv.String(gtime.TimestampNano())
    71  		key := "TEST_ENV_" + value
    72  		err := genv.Set(key, "TEST")
    73  		t.AssertNil(err)
    74  		t.AssertEQ(os.Getenv(key), "TEST")
    75  	})
    76  }
    77  
    78  func Test_GEnv_SetMap(t *testing.T) {
    79  	gtest.C(t, func(t *gtest.T) {
    80  		err := genv.SetMap(g.MapStrStr{
    81  			"K1": "TEST1",
    82  			"K2": "TEST2",
    83  		})
    84  		t.AssertNil(err)
    85  		t.AssertEQ(os.Getenv("K1"), "TEST1")
    86  		t.AssertEQ(os.Getenv("K2"), "TEST2")
    87  	})
    88  }
    89  
    90  func Test_GEnv_Build(t *testing.T) {
    91  	gtest.C(t, func(t *gtest.T) {
    92  		s := genv.Build(map[string]string{
    93  			"k1": "v1",
    94  			"k2": "v2",
    95  		})
    96  		t.AssertIN("k1=v1", s)
    97  		t.AssertIN("k2=v2", s)
    98  	})
    99  }
   100  
   101  func Test_GEnv_Remove(t *testing.T) {
   102  	gtest.C(t, func(t *gtest.T) {
   103  		value := gconv.String(gtime.TimestampNano())
   104  		key := "TEST_ENV_" + value
   105  		err := os.Setenv(key, "TEST")
   106  		t.AssertNil(err)
   107  		err = genv.Remove(key)
   108  		t.AssertNil(err)
   109  		t.AssertEQ(os.Getenv(key), "")
   110  	})
   111  }
   112  
   113  func Test_GetWithCmd(t *testing.T) {
   114  	gtest.C(t, func(t *gtest.T) {
   115  		gcmd.Init("-test", "2")
   116  		t.Assert(genv.GetWithCmd("TEST"), 2)
   117  	})
   118  	gtest.C(t, func(t *gtest.T) {
   119  		genv.Set("TEST", "1")
   120  		defer genv.Remove("TEST")
   121  		gcmd.Init("-test", "2")
   122  		t.Assert(genv.GetWithCmd("test"), 1)
   123  	})
   124  }
   125  
   126  func Test_MapFromEnv(t *testing.T) {
   127  	gtest.C(t, func(t *gtest.T) {
   128  		m := genv.MapFromEnv([]string{"a=1", "b=2"})
   129  		t.Assert(m, g.Map{"a": 1, "b": 2})
   130  	})
   131  }
   132  
   133  func Test_MapToEnv(t *testing.T) {
   134  	gtest.C(t, func(t *gtest.T) {
   135  		s := genv.MapToEnv(g.MapStrStr{"a": "1"})
   136  		t.Assert(s, []string{"a=1"})
   137  	})
   138  }
   139  
   140  func Test_Filter(t *testing.T) {
   141  	gtest.C(t, func(t *gtest.T) {
   142  		s := genv.Filter([]string{"a=1", "a=3"})
   143  		t.Assert(s, []string{"a=3"})
   144  	})
   145  }