github.com/argoproj/argo-cd@v1.8.7/util/config/env_test.go (about)

     1  package config
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func loadOpts(t *testing.T, opts string) {
    11  	assert.Nil(t, os.Setenv("ARGOCD_OPTS", opts))
    12  	assert.Nil(t, loadFlags())
    13  }
    14  func loadInvalidOpts(t *testing.T, opts string) {
    15  	assert.Nil(t, os.Setenv("ARGOCD_OPTS", opts))
    16  	assert.Error(t, loadFlags())
    17  }
    18  
    19  func TestNilOpts(t *testing.T) {
    20  	assert.Equal(t, "foo", GetFlag("foo", "foo"))
    21  }
    22  
    23  func TestEmptyOpts(t *testing.T) {
    24  	loadOpts(t, "")
    25  
    26  	assert.Equal(t, "foo", GetFlag("foo", "foo"))
    27  }
    28  
    29  func TestRubbishOpts(t *testing.T) {
    30  	loadInvalidOpts(t, "rubbish")
    31  }
    32  
    33  func TestBoolFlag(t *testing.T) {
    34  	loadOpts(t, "--foo")
    35  
    36  	assert.True(t, GetBoolFlag("foo"))
    37  }
    38  
    39  func TestBoolFlagAtStart(t *testing.T) {
    40  	loadOpts(t, "--foo --bar baz")
    41  
    42  	assert.True(t, GetBoolFlag("foo"))
    43  }
    44  func TestBoolFlagInMiddle(t *testing.T) {
    45  	loadOpts(t, "--bar baz --foo --qux")
    46  
    47  	assert.True(t, GetBoolFlag("foo"))
    48  }
    49  
    50  func TestBooleanFlagAtEnd(t *testing.T) {
    51  	loadOpts(t, "--bar baz --foo")
    52  
    53  	assert.True(t, GetBoolFlag("foo"))
    54  }
    55  
    56  func TestFlagAtStart(t *testing.T) {
    57  	loadOpts(t, "--foo bar")
    58  
    59  	assert.Equal(t, "bar", GetFlag("foo", ""))
    60  }
    61  
    62  func TestFlagInTheMiddle(t *testing.T) {
    63  	loadOpts(t, "--baz --foo bar --qux")
    64  
    65  	assert.Equal(t, "bar", GetFlag("foo", ""))
    66  }
    67  
    68  func TestFlagAtTheEnd(t *testing.T) {
    69  	loadOpts(t, "--baz --foo bar")
    70  
    71  	assert.Equal(t, "bar", GetFlag("foo", ""))
    72  }
    73  
    74  func TestFlagWithSingleQuotes(t *testing.T) {
    75  	loadOpts(t, "--foo 'bar baz'")
    76  
    77  	assert.Equal(t, "bar baz", GetFlag("foo", ""))
    78  }
    79  func TestFlagWithDoubleQuotes(t *testing.T) {
    80  	loadOpts(t, "--foo \"bar baz\"")
    81  
    82  	assert.Equal(t, "bar baz", GetFlag("foo", ""))
    83  }