github.com/vishnusomank/figtree@v0.1.0/exec_test.go (about)

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