github.com/aaronlehmann/figtree@v1.0.1/exec_test.go (about)

     1  package figtree
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestOptionsExecConfigD3(t *testing.T) {
    11  	opts := TestOptions{}
    12  	os.Chdir("d1/d2/d3")
    13  	defer os.Chdir("../../..")
    14  
    15  	arr1 := []StringOption{}
    16  	arr1 = append(arr1, StringOption{"exec.yml", true, "d3arr1val1"})
    17  	arr1 = append(arr1, StringOption{"exec.yml", true, "d3arr1val2"})
    18  	arr1 = append(arr1, StringOption{"../exec.yml", true, "d2arr1val1"})
    19  	arr1 = append(arr1, StringOption{"../exec.yml", true, "d2arr1val2"})
    20  	arr1 = append(arr1, StringOption{"../../exec.yml", true, "d1arr1val1"})
    21  	arr1 = append(arr1, StringOption{"../../exec.yml", true, "d1arr1val2"})
    22  
    23  	expected := TestOptions{
    24  		String1:    StringOption{"exec.yml", true, "d3str1val1"},
    25  		LeaveEmpty: StringOption{},
    26  		Array1:     arr1,
    27  		Map1: map[string]StringOption{
    28  			"key0": StringOption{"../../exec.yml", true, "d1map1val0"},
    29  			"key1": StringOption{"../exec.yml", true, "d2map1val1"},
    30  			"key2": StringOption{"exec.yml", true, "d3map1val2"},
    31  			"key3": StringOption{"exec.yml", true, "d3map1val3"},
    32  		},
    33  		Int1:   IntOption{"exec.yml", true, 333},
    34  		Float1: Float32Option{"exec.yml", true, 3.33},
    35  		Bool1:  BoolOption{"exec.yml", true, true},
    36  	}
    37  
    38  	fig := newFigTreeFromEnv()
    39  	err := fig.LoadAllConfigs("exec.yml", &opts)
    40  	assert.Nil(t, err)
    41  	assert.Exactly(t, expected, opts)
    42  }
    43  
    44  func TestOptionsExecConfigD2(t *testing.T) {
    45  	opts := TestOptions{}
    46  	os.Chdir("d1/d2")
    47  	defer os.Chdir("../..")
    48  
    49  	arr1 := []StringOption{}
    50  	arr1 = append(arr1, StringOption{"exec.yml", true, "d2arr1val1"})
    51  	arr1 = append(arr1, StringOption{"exec.yml", true, "d2arr1val2"})
    52  	arr1 = append(arr1, StringOption{"../exec.yml", true, "d1arr1val1"})
    53  	arr1 = append(arr1, StringOption{"../exec.yml", true, "d1arr1val2"})
    54  
    55  	expected := TestOptions{
    56  		String1:    StringOption{"exec.yml", true, "d2str1val1"},
    57  		LeaveEmpty: StringOption{},
    58  		Array1:     arr1,
    59  		Map1: map[string]StringOption{
    60  			"key0": StringOption{"../exec.yml", true, "d1map1val0"},
    61  			"key1": StringOption{"exec.yml", true, "d2map1val1"},
    62  			"key2": StringOption{"exec.yml", true, "d2map1val2"},
    63  		},
    64  		Int1:   IntOption{"exec.yml", true, 222},
    65  		Float1: Float32Option{"exec.yml", true, 2.22},
    66  		Bool1:  BoolOption{"exec.yml", true, false},
    67  	}
    68  
    69  	fig := newFigTreeFromEnv()
    70  	err := fig.LoadAllConfigs("exec.yml", &opts)
    71  	assert.Nil(t, err)
    72  	assert.Exactly(t, expected, opts)
    73  }
    74  
    75  func TestOptionsExecConfigD1(t *testing.T) {
    76  	opts := TestOptions{}
    77  	os.Chdir("d1")
    78  	defer os.Chdir("..")
    79  
    80  	arr1 := []StringOption{}
    81  	arr1 = append(arr1, StringOption{"exec.yml", true, "d1arr1val1"})
    82  	arr1 = append(arr1, StringOption{"exec.yml", true, "d1arr1val2"})
    83  
    84  	expected := TestOptions{
    85  		String1:    StringOption{"exec.yml", true, "d1str1val1"},
    86  		LeaveEmpty: StringOption{},
    87  		Array1:     arr1,
    88  		Map1: map[string]StringOption{
    89  			"key0": StringOption{"exec.yml", true, "d1map1val0"},
    90  			"key1": StringOption{"exec.yml", true, "d1map1val1"},
    91  		},
    92  		Int1:   IntOption{"exec.yml", true, 111},
    93  		Float1: Float32Option{"exec.yml", true, 1.11},
    94  		Bool1:  BoolOption{"exec.yml", true, true},
    95  	}
    96  
    97  	fig := newFigTreeFromEnv()
    98  	err := fig.LoadAllConfigs("exec.yml", &opts)
    99  	assert.Nil(t, err)
   100  	assert.Exactly(t, expected, opts)
   101  }
   102  
   103  func TestBuiltinExecConfigD3(t *testing.T) {
   104  	opts := TestBuiltin{}
   105  	os.Chdir("d1/d2/d3")
   106  	defer os.Chdir("../../..")
   107  
   108  	arr1 := []string{}
   109  	arr1 = append(arr1, "d3arr1val1")
   110  	arr1 = append(arr1, "d3arr1val2")
   111  	arr1 = append(arr1, "d2arr1val1")
   112  	arr1 = append(arr1, "d2arr1val2")
   113  	arr1 = append(arr1, "d1arr1val1")
   114  	arr1 = append(arr1, "d1arr1val2")
   115  
   116  	expected := TestBuiltin{
   117  		String1:    "d3str1val1",
   118  		LeaveEmpty: "",
   119  		Array1:     arr1,
   120  		Map1: map[string]string{
   121  			"key0": "d1map1val0",
   122  			"key1": "d2map1val1",
   123  			"key2": "d3map1val2",
   124  			"key3": "d3map1val3",
   125  		},
   126  		Int1:   333,
   127  		Float1: 3.33,
   128  		Bool1:  true,
   129  	}
   130  
   131  	fig := newFigTreeFromEnv()
   132  	err := fig.LoadAllConfigs("exec.yml", &opts)
   133  	assert.Nil(t, err)
   134  	assert.Exactly(t, expected, opts)
   135  }
   136  
   137  func TestBuiltinExecConfigD2(t *testing.T) {
   138  	opts := TestBuiltin{}
   139  	os.Chdir("d1/d2")
   140  	defer os.Chdir("../..")
   141  
   142  	arr1 := []string{}
   143  	arr1 = append(arr1, "d2arr1val1")
   144  	arr1 = append(arr1, "d2arr1val2")
   145  	arr1 = append(arr1, "d1arr1val1")
   146  	arr1 = append(arr1, "d1arr1val2")
   147  
   148  	expected := TestBuiltin{
   149  		String1:    "d2str1val1",
   150  		LeaveEmpty: "",
   151  		Array1:     arr1,
   152  		Map1: map[string]string{
   153  			"key0": "d1map1val0",
   154  			"key1": "d2map1val1",
   155  			"key2": "d2map1val2",
   156  		},
   157  		Int1:   222,
   158  		Float1: 2.22,
   159  		// note this will be true from d1/exec.yml since the
   160  		// d1/d2/exec.yml set it to false which is a zero value
   161  		Bool1: true,
   162  	}
   163  
   164  	fig := newFigTreeFromEnv()
   165  	err := fig.LoadAllConfigs("exec.yml", &opts)
   166  	assert.Nil(t, err)
   167  	assert.Exactly(t, expected, opts)
   168  }
   169  
   170  func TestBuiltinExecConfigD1(t *testing.T) {
   171  	opts := TestBuiltin{}
   172  	os.Chdir("d1")
   173  	defer os.Chdir("..")
   174  
   175  	arr1 := []string{}
   176  	arr1 = append(arr1, "d1arr1val1")
   177  	arr1 = append(arr1, "d1arr1val2")
   178  
   179  	expected := TestBuiltin{
   180  		String1:    "d1str1val1",
   181  		LeaveEmpty: "",
   182  		Array1:     arr1,
   183  		Map1: map[string]string{
   184  			"key0": "d1map1val0",
   185  			"key1": "d1map1val1",
   186  		},
   187  		Int1:   111,
   188  		Float1: 1.11,
   189  		Bool1:  true,
   190  	}
   191  
   192  	fig := newFigTreeFromEnv()
   193  	err := fig.LoadAllConfigs("exec.yml", &opts)
   194  	assert.Nil(t, err)
   195  	assert.Exactly(t, expected, opts)
   196  }