github.com/khulnasoft/cli@v0.0.0-20240402070845-01bcad7beefa/opts/config_test.go (about)

     1  package opts
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"gotest.tools/v3/assert"
     8  	is "gotest.tools/v3/assert/cmp"
     9  )
    10  
    11  func TestConfigOptions(t *testing.T) {
    12  	testCases := []struct {
    13  		name       string
    14  		input      string
    15  		configName string
    16  		fileName   string
    17  		uid        string
    18  		gid        string
    19  		fileMode   uint
    20  	}{
    21  		{
    22  			name:       "Simple",
    23  			input:      "app-config",
    24  			configName: "app-config",
    25  			fileName:   "app-config",
    26  			uid:        "0",
    27  			gid:        "0",
    28  		},
    29  		{
    30  			name:       "Source",
    31  			input:      "source=foo",
    32  			configName: "foo",
    33  			fileName:   "foo",
    34  		},
    35  		{
    36  			name:       "SourceTarget",
    37  			input:      "source=foo,target=testing",
    38  			configName: "foo",
    39  			fileName:   "testing",
    40  		},
    41  		{
    42  			name:       "Shorthand",
    43  			input:      "src=foo,target=testing",
    44  			configName: "foo",
    45  			fileName:   "testing",
    46  		},
    47  		{
    48  			name:       "CustomUidGid",
    49  			input:      "source=foo,target=testing,uid=1000,gid=1001",
    50  			configName: "foo",
    51  			fileName:   "testing",
    52  			uid:        "1000",
    53  			gid:        "1001",
    54  		},
    55  		{
    56  			name:       "CustomMode",
    57  			input:      "source=foo,target=testing,uid=1000,gid=1001,mode=0444",
    58  			configName: "foo",
    59  			fileName:   "testing",
    60  			uid:        "1000",
    61  			gid:        "1001",
    62  			fileMode:   0o444,
    63  		},
    64  	}
    65  
    66  	for _, tc := range testCases {
    67  		tc := tc
    68  		t.Run(tc.name, func(t *testing.T) {
    69  			var opt ConfigOpt
    70  			assert.NilError(t, opt.Set(tc.input))
    71  			reqs := opt.Value()
    72  			assert.Assert(t, is.Len(reqs, 1))
    73  			req := reqs[0]
    74  			assert.Check(t, is.Equal(tc.configName, req.ConfigName))
    75  			assert.Check(t, is.Equal(tc.fileName, req.File.Name))
    76  			if tc.uid != "" {
    77  				assert.Check(t, is.Equal(tc.uid, req.File.UID))
    78  			}
    79  			if tc.gid != "" {
    80  				assert.Check(t, is.Equal(tc.gid, req.File.GID))
    81  			}
    82  			if tc.fileMode != 0 {
    83  				assert.Check(t, is.Equal(os.FileMode(tc.fileMode), req.File.Mode))
    84  			}
    85  		})
    86  	}
    87  }