github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/utils/envvars/envvars_test.go (about)

     1  package envvars
     2  
     3  import (
     4  	"encoding/json"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/lmorg/murex/test/count"
     9  )
    10  
    11  type envAllTestT struct {
    12  	Slice    []string
    13  	Expected map[string]string
    14  	Error    bool
    15  }
    16  
    17  func TestEnvVarsAll(t *testing.T) {
    18  	tests := []envAllTestT{
    19  		{
    20  			Slice:    []string{},
    21  			Expected: map[string]string{},
    22  			Error:    false,
    23  		},
    24  		{
    25  			Slice: []string{
    26  				`a=a`,
    27  				`b=b`,
    28  				`c=c`,
    29  			},
    30  			Expected: map[string]string{
    31  				"a": "a",
    32  				"b": "b",
    33  				"c": "c",
    34  			},
    35  			Error: false,
    36  		},
    37  		{
    38  			Slice: []string{
    39  				`a=`,
    40  				`b=b`,
    41  				`c=c`,
    42  			},
    43  			Expected: map[string]string{
    44  				"a": "",
    45  				"b": "b",
    46  				"c": "c",
    47  			},
    48  			Error: false,
    49  		},
    50  		{
    51  			Slice: []string{
    52  				`a`,
    53  				`b=b`,
    54  				`c=c`,
    55  			},
    56  			Expected: map[string]string{
    57  				"a": "",
    58  				"b": "b",
    59  				"c": "c",
    60  			},
    61  			Error: false,
    62  		},
    63  		{
    64  			Slice: []string{
    65  				`a=a=a`,
    66  				`b=b`,
    67  				`c=c`,
    68  			},
    69  			Expected: map[string]string{
    70  				"a": "a=a",
    71  				"b": "b",
    72  				"c": "c",
    73  			},
    74  			Error: false,
    75  		},
    76  		{
    77  			Slice: []string{
    78  				`a=a`,
    79  				`foo=bar`,
    80  				`c=c`,
    81  			},
    82  			Expected: map[string]string{
    83  				"a":   "a",
    84  				"foo": "bar",
    85  				"c":   "c",
    86  			},
    87  			Error: false,
    88  		},
    89  		{
    90  			Slice: []string{
    91  				`a=a`,
    92  				`=foobar`,
    93  				`c=c`,
    94  			},
    95  			Expected: map[string]string{
    96  				"a": "a",
    97  				"":  "foobar",
    98  				"c": "c",
    99  			},
   100  			Error: false,
   101  		},
   102  		{
   103  			Slice: []string{
   104  				`a=a`,
   105  				`foobar=`,
   106  				`c=c`,
   107  			},
   108  			Expected: map[string]string{
   109  				"a":      "a",
   110  				"foobar": "",
   111  				"c":      "c",
   112  			},
   113  			Error: false,
   114  		},
   115  		{
   116  			Slice: []string{
   117  				`a=a`,
   118  				`=`,
   119  				`c=c`,
   120  			},
   121  			Expected: map[string]string{
   122  				"a": "a",
   123  				"":  "",
   124  				"c": "c",
   125  			},
   126  			Error: false,
   127  		},
   128  		{
   129  			Slice: []string{
   130  				`a=a`,
   131  				`foobar`,
   132  				`c=c`,
   133  			},
   134  			Expected: map[string]string{
   135  				"a":      "a",
   136  				"foobar": "",
   137  				"c":      "c",
   138  			},
   139  			Error: false,
   140  		},
   141  		{
   142  			Slice: []string{
   143  				`a=a`,
   144  				``,
   145  				`c=c`,
   146  			},
   147  			Expected: map[string]string{
   148  				"a": "a",
   149  				"":  "",
   150  				"c": "c",
   151  			},
   152  			Error: false,
   153  		},
   154  		{
   155  			Slice: []string{
   156  				`a=a`,
   157  				`hello=世界`,
   158  				`c=c`,
   159  			},
   160  			Expected: map[string]string{
   161  				"a":     "a",
   162  				"hello": "世界",
   163  				"c":     "c",
   164  			},
   165  			Error: false,
   166  		},
   167  		{
   168  			Slice: []string{
   169  				`a=a`,
   170  				`世界=hello`,
   171  				`c=c`,
   172  			},
   173  			Expected: map[string]string{
   174  				"a":  "a",
   175  				"世界": "hello",
   176  				"c":  "c",
   177  			},
   178  			Error: false,
   179  		},
   180  		{
   181  			Slice: []string{
   182  				`a=a`,
   183  				`世界=`,
   184  				`c=c`,
   185  			},
   186  			Expected: map[string]string{
   187  				"a":  "a",
   188  				"世界": "",
   189  				"c":  "c",
   190  			},
   191  			Error: false,
   192  		},
   193  		{
   194  			Slice: []string{
   195  				`a=a`,
   196  				`世界`,
   197  				`c=c`,
   198  			},
   199  			Expected: map[string]string{
   200  				"a":  "a",
   201  				"世界": "",
   202  				"c":  "c",
   203  			},
   204  			Error: false,
   205  		},
   206  		{
   207  			Slice: []string{
   208  				`a=a`,
   209  				`=世界`,
   210  				`c=c`,
   211  			},
   212  			Expected: map[string]string{
   213  				"a": "a",
   214  				"":  "世界",
   215  				"c": "c",
   216  			},
   217  			Error: false,
   218  		},
   219  		{
   220  			Slice: []string{
   221  				"a=a",
   222  				"TIMEFMT=\n================\nCPU	%P\nuser	%*U\nsystem	%*S\ntotal	%*E",
   223  				`c=c`,
   224  			},
   225  			Expected: map[string]string{
   226  				"a": "a",
   227  				"TIMEFMT": "\n================\nCPU	%P\nuser	%*U\nsystem	%*S\ntotal	%*E",
   228  				"c": "c",
   229  			},
   230  			Error: false,
   231  		},
   232  	}
   233  
   234  	testEnvVarsAll(t, tests)
   235  }
   236  
   237  func testEnvVarsAll(t *testing.T, tests []envAllTestT) {
   238  	count.Tests(t, len(tests))
   239  	t.Helper()
   240  
   241  	for i, test := range tests {
   242  		if test.Expected == nil {
   243  			test.Expected = make(map[string]string)
   244  		}
   245  
   246  		actual := make(map[string]interface{})
   247  		all(test.Slice, actual)
   248  
   249  		/*if (err != nil) != test.Error {
   250  			t.Errorf("Error expectation mismatch in test %d", i)
   251  			t.Logf("  Expected: %v", test.Error)
   252  			t.Logf("  Actual:   %v", err)
   253  		}*/
   254  
   255  		if len(test.Expected) != len(actual) {
   256  			t.Errorf("Output count mistmatch in test %d", i)
   257  			t.Logf("  Exp Count: %d", len(test.Expected))
   258  			t.Logf("  Act Count: %d", len(actual))
   259  			t.Logf("  Expected:\n%s", testJsonEncode(test.Expected))
   260  			t.Logf("  Actual:\n%s", testJsonEncode(actual))
   261  		}
   262  
   263  		for k := range actual {
   264  			if actual[k] != test.Expected[k] {
   265  				t.Errorf("Key/value mistmatch in test %d", i)
   266  				t.Logf("  Key:       `%s`", k)
   267  				t.Logf("  Exp Value: `%s`", test.Expected[k])
   268  				t.Logf("  Act Value: `%s`", actual[k].(string))
   269  				t.Logf("  Expected:\n%s", testJsonEncode(test.Expected))
   270  				t.Logf("  Actual:\n%s", testJsonEncode(actual))
   271  			}
   272  		}
   273  	}
   274  }
   275  
   276  func testJsonEncode(v interface{}) string {
   277  	b, _ := json.MarshalIndent(v, "    ", "    ")
   278  	return string(b)
   279  }
   280  
   281  func TestEnvVarsE2E(t *testing.T) {
   282  	namespace := "MUREX_TEST_TestDumpExports_"
   283  
   284  	tests := map[string]string{
   285  		"nil":       "",
   286  		"text":      "testing123",
   287  		"s p a c e": "testing 123",
   288  		"equals":    "testing=123",
   289  	}
   290  
   291  	count.Tests(t, len(tests))
   292  
   293  	for k, v := range tests {
   294  		if err := os.Setenv(namespace+k, v); err != nil {
   295  			t.Error("Unable to set env var for test!")
   296  			t.Logf("  Name:  '%s'", namespace+k)
   297  			t.Logf("  Value: '%s'", v)
   298  			t.Logf("  Error:  %s", err)
   299  		}
   300  	}
   301  
   302  	m := make(map[string]interface{})
   303  	All(m)
   304  
   305  	for k, v := range tests {
   306  		if m[namespace+k] != v {
   307  			t.Error("Unexpected map value:")
   308  			t.Logf("  Name:     '%s'", namespace+k)
   309  			t.Logf("  Expected: '%s'", v)
   310  			t.Logf("  Actual:   '%s'", m[namespace+k].(string))
   311  		}
   312  
   313  		if err := os.Unsetenv(namespace + k); err != nil {
   314  			t.Errorf("Unable to unset '%s': %s", namespace+k, err)
   315  		}
   316  	}
   317  }