github.com/etecs-ru/ristretto@v0.9.1/z/flags_test.go (about)

     1  package z
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"os/user"
     7  	"path/filepath"
     8  	"runtime"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func TestFlag(t *testing.T) {
    16  	const opt = `bool_key=true; int-key=5; float-key=0.05; string_key=value; ;`
    17  	const def = `bool_key=false; int-key=0; float-key=1.0; string-key=; other-key=5;
    18  		duration-minutes=15m; duration-hours=12h; duration-days=30d;`
    19  
    20  	_, err := NewSuperFlag("boolo-key=true").MergeWithDefault(def)
    21  	require.Error(t, err)
    22  	_, err = newSuperFlagImpl("key-without-value")
    23  	require.Error(t, err)
    24  
    25  	// bool-key and int-key should not be overwritten. Only other-key should be set.
    26  	sf := NewSuperFlag(opt)
    27  	sf.MergeAndCheckDefault(def)
    28  
    29  	require.Equal(t, true, sf.GetBool("bool-key"))
    30  	require.Equal(t, uint64(5), sf.GetUint64("int-key"))
    31  	require.Equal(t, "value", sf.GetString("string-key"))
    32  	require.Equal(t, uint64(5), sf.GetUint64("other-key"))
    33  
    34  	require.Equal(t, time.Minute*15, sf.GetDuration("duration-minutes"))
    35  	require.Equal(t, time.Hour*12, sf.GetDuration("duration-hours"))
    36  	require.Equal(t, time.Hour*24*30, sf.GetDuration("duration-days"))
    37  }
    38  
    39  func TestFlagDefault(t *testing.T) {
    40  	def := `one=false; two=; three=;`
    41  	f := NewSuperFlag(`one=true; two=4;`).MergeAndCheckDefault(def)
    42  	require.Equal(t, true, f.GetBool("one"))
    43  	require.Equal(t, int64(4), f.GetInt64("two"))
    44  }
    45  
    46  func TestGetPath(t *testing.T) {
    47  	if runtime.GOOS == "windows" {
    48  		t.Skip("not ready for Windows")
    49  	}
    50  	usr, err := user.Current()
    51  	require.NoError(t, err)
    52  	homeDir := usr.HomeDir
    53  	cwd, err := os.Getwd()
    54  	require.NoError(t, err)
    55  
    56  	tests := []struct {
    57  		path     string
    58  		expected string
    59  	}{
    60  		{
    61  			"/home/user/file.txt",
    62  			"/home/user/file.txt",
    63  		},
    64  		{
    65  			"~/file.txt",
    66  			filepath.Join(homeDir, "file.txt"),
    67  		},
    68  		{
    69  			"~/abc/../file.txt",
    70  			filepath.Join(homeDir, "file.txt"),
    71  		},
    72  		{
    73  			"~/",
    74  			homeDir,
    75  		},
    76  		{
    77  			"~filename",
    78  			filepath.Join(cwd, "~filename"),
    79  		},
    80  		{
    81  			"./filename",
    82  			filepath.Join(cwd, "filename"),
    83  		},
    84  		{
    85  			"",
    86  			"",
    87  		},
    88  		{
    89  			"./",
    90  			cwd,
    91  		},
    92  	}
    93  
    94  	get := func(p string) string {
    95  		opt := fmt.Sprintf("file=%s", p)
    96  		sf := NewSuperFlag(opt)
    97  		return sf.GetPath("file")
    98  	}
    99  
   100  	for _, tc := range tests {
   101  		actual := get(tc.path)
   102  		require.Equalf(t, tc.expected, actual, "Failed on testcase: %s", tc.path)
   103  	}
   104  }