github.com/rigado/snapd@v2.42.5-go-mod+incompatible/osutil/env_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2016 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package osutil_test
    21  
    22  import (
    23  	"fmt"
    24  	"math"
    25  	"os"
    26  	"strings"
    27  
    28  	"gopkg.in/check.v1"
    29  
    30  	"github.com/snapcore/snapd/osutil"
    31  )
    32  
    33  type envSuite struct{}
    34  
    35  var _ = check.Suite(&envSuite{})
    36  
    37  func (s *envSuite) TestGetenvBoolTrue(c *check.C) {
    38  	key := "__XYZZY__"
    39  	os.Unsetenv(key)
    40  
    41  	for _, s := range []string{
    42  		"1", "t", "TRUE",
    43  	} {
    44  		os.Setenv(key, s)
    45  		c.Assert(os.Getenv(key), check.Equals, s)
    46  		c.Check(osutil.GetenvBool(key), check.Equals, true, check.Commentf(s))
    47  		c.Check(osutil.GetenvBool(key, false), check.Equals, true, check.Commentf(s))
    48  		c.Check(osutil.GetenvBool(key, true), check.Equals, true, check.Commentf(s))
    49  	}
    50  }
    51  
    52  func (s *envSuite) TestGetenvBoolFalse(c *check.C) {
    53  	key := "__XYZZY__"
    54  	os.Unsetenv(key)
    55  	c.Assert(osutil.GetenvBool(key), check.Equals, false)
    56  
    57  	for _, s := range []string{
    58  		"", "0", "f", "FALSE", "potato",
    59  	} {
    60  		os.Setenv(key, s)
    61  		c.Assert(os.Getenv(key), check.Equals, s)
    62  		c.Check(osutil.GetenvBool(key), check.Equals, false, check.Commentf(s))
    63  		c.Check(osutil.GetenvBool(key, false), check.Equals, false, check.Commentf(s))
    64  	}
    65  }
    66  
    67  func (s *envSuite) TestGetenvBoolFalseDefaultTrue(c *check.C) {
    68  	key := "__XYZZY__"
    69  	os.Unsetenv(key)
    70  	c.Assert(osutil.GetenvBool(key), check.Equals, false)
    71  
    72  	for _, s := range []string{
    73  		"0", "f", "FALSE",
    74  	} {
    75  		os.Setenv(key, s)
    76  		c.Assert(os.Getenv(key), check.Equals, s)
    77  		c.Check(osutil.GetenvBool(key, true), check.Equals, false, check.Commentf(s))
    78  	}
    79  
    80  	for _, s := range []string{
    81  		"", "potato", // etc
    82  	} {
    83  		os.Setenv(key, s)
    84  		c.Assert(os.Getenv(key), check.Equals, s)
    85  		c.Check(osutil.GetenvBool(key, true), check.Equals, true, check.Commentf(s))
    86  	}
    87  }
    88  
    89  func (s *envSuite) TestGetenvInt64(c *check.C) {
    90  	key := "__XYZZY__"
    91  	os.Unsetenv(key)
    92  
    93  	c.Check(osutil.GetenvInt64(key), check.Equals, int64(0))
    94  	c.Check(osutil.GetenvInt64(key, -1), check.Equals, int64(-1))
    95  	c.Check(osutil.GetenvInt64(key, math.MaxInt64), check.Equals, int64(math.MaxInt64))
    96  	c.Check(osutil.GetenvInt64(key, math.MinInt64), check.Equals, int64(math.MinInt64))
    97  
    98  	for _, n := range []int64{
    99  		0, -1, math.MinInt64, math.MaxInt64,
   100  	} {
   101  		for _, tpl := range []string{"%d", "  %d  ", "%#x", "%#X", "%#o"} {
   102  			v := fmt.Sprintf(tpl, n)
   103  			os.Setenv(key, v)
   104  			c.Assert(os.Getenv(key), check.Equals, v)
   105  			c.Check(osutil.GetenvInt64(key), check.Equals, n, check.Commentf(v))
   106  		}
   107  	}
   108  }
   109  
   110  func (s *envSuite) TestSubstitueEnv(c *check.C) {
   111  	for _, t := range []struct {
   112  		env string
   113  
   114  		expected string
   115  	}{
   116  		// trivial
   117  		{"K1=V1,K2=V2", "K1=V1,K2=V2"},
   118  		// simple (order is preserved)
   119  		{"K=V,K2=$K", "K=V,K2=V"},
   120  		// simple from environment
   121  		{"K=$PATH", fmt.Sprintf("K=%s", os.Getenv("PATH"))},
   122  		// append to substitution from environment
   123  		{"K=${PATH}:/foo", fmt.Sprintf("K=%s", os.Getenv("PATH")+":/foo")},
   124  		// multi-level
   125  		{"A=1,B=$A/2,C=$B/3,D=$C/4", "A=1,B=1/2,C=1/2/3,D=1/2/3/4"},
   126  		// parsing is top down
   127  		{"A=$A", "A="},
   128  		{"A=$B,B=$A", "A=,B="},
   129  		{"A=$B,B=$C,C=$A", "A=,B=,C="},
   130  	} {
   131  		env := osutil.SubstituteEnv(strings.Split(t.env, ","))
   132  		c.Check(strings.Join(env, ","), check.DeepEquals, t.expected, check.Commentf("invalid result for %q, got %q expected %q", t.env, env, t.expected))
   133  	}
   134  }
   135  
   136  func (s *envSuite) TestEnvMap(c *check.C) {
   137  	for _, t := range []struct {
   138  		env      []string
   139  		expected map[string]string
   140  	}{
   141  		{
   142  			[]string{"K=V"},
   143  			map[string]string{"K": "V"},
   144  		},
   145  		{
   146  			[]string{"K=V=V=V"},
   147  			map[string]string{"K": "V=V=V"},
   148  		},
   149  		{
   150  			[]string{"K1=V1", "K2=V2"},
   151  			map[string]string{"K1": "V1", "K2": "V2"},
   152  		},
   153  		{
   154  			// invalid input is handled gracefully
   155  			[]string{"KEY_ONLY"},
   156  			map[string]string{},
   157  		},
   158  	} {
   159  		m := osutil.EnvMap(t.env)
   160  		c.Check(m, check.DeepEquals, t.expected, check.Commentf("invalid result for %q, got %q expected %q", t.env, m, t.expected))
   161  	}
   162  }