...

Source file src/github.com/ory/x/configx/koanf_file_test.go

Documentation: github.com/ory/x/configx

     1  package configx
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"io/ioutil"
     7  	"path/filepath"
     8  	"testing"
     9  
    10  	"github.com/ghodss/yaml"
    11  	"github.com/pelletier/go-toml"
    12  	"github.com/stretchr/testify/assert"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func TestKoanfFile(t *testing.T) {
    17  	setupFile := func(t *testing.T, fn, fc, subKey string) (*KoanfFile, context.CancelFunc) {
    18  		dir := t.TempDir()
    19  		fn = filepath.Join(dir, fn)
    20  		require.NoError(t, ioutil.WriteFile(fn, []byte(fc), 0600))
    21  
    22  		ctx, cancel := context.WithCancel(context.Background())
    23  		kf, err := NewKoanfFileSubKey(ctx, fn, subKey)
    24  		require.NoError(t, err)
    25  		return kf, cancel
    26  	}
    27  
    28  	t.Run("case=reads json root file", func(t *testing.T) {
    29  		v := map[string]interface{}{
    30  			"foo": "bar",
    31  		}
    32  		encV, err := json.Marshal(v)
    33  		require.NoError(t, err)
    34  
    35  		kf, cancel := setupFile(t, "config.json", string(encV), "")
    36  		defer cancel()
    37  
    38  		actual, err := kf.Read()
    39  		require.NoError(t, err)
    40  		assert.Equal(t, v, actual)
    41  	})
    42  
    43  	t.Run("case=reads yaml root file", func(t *testing.T) {
    44  		v := map[string]interface{}{
    45  			"foo": "yaml string",
    46  		}
    47  		encV, err := yaml.Marshal(v)
    48  		require.NoError(t, err)
    49  
    50  		kf, cancel := setupFile(t, "config.yml", string(encV), "")
    51  		defer cancel()
    52  
    53  		actual, err := kf.Read()
    54  		require.NoError(t, err)
    55  		assert.Equal(t, v, actual)
    56  	})
    57  
    58  	t.Run("case=reads toml root file", func(t *testing.T) {
    59  		v := map[string]interface{}{
    60  			"foo": "toml string",
    61  		}
    62  		encV, err := toml.Marshal(v)
    63  		require.NoError(t, err)
    64  
    65  		kf, cancel := setupFile(t, "config.toml", string(encV), "")
    66  		defer cancel()
    67  
    68  		actual, err := kf.Read()
    69  		require.NoError(t, err)
    70  		assert.Equal(t, v, actual)
    71  	})
    72  
    73  	t.Run("case=reads json file as subkey", func(t *testing.T) {
    74  		v := map[string]interface{}{
    75  			"bar": "asdf",
    76  		}
    77  		encV, err := json.Marshal(v)
    78  		require.NoError(t, err)
    79  
    80  		kf, cancel := setupFile(t, "config.json", string(encV), "parent.of.config")
    81  		defer cancel()
    82  
    83  		actual, err := kf.Read()
    84  		require.NoError(t, err)
    85  		assert.Equal(t, map[string]interface{}{
    86  			"parent": map[string]interface{}{
    87  				"of": map[string]interface{}{
    88  					"config": v,
    89  				},
    90  			},
    91  		}, actual)
    92  	})
    93  }
    94  

View as plain text