github.com/khulnasoft/cli@v0.0.0-20240402070845-01bcad7beefa/cli/context/store/storeconfig_test.go (about)

     1  // FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16:
     2  //go:build go1.19
     3  
     4  package store
     5  
     6  import (
     7  	"testing"
     8  
     9  	"gotest.tools/v3/assert"
    10  )
    11  
    12  type (
    13  	testCtx struct{}
    14  	testEP1 struct{}
    15  	testEP2 struct{}
    16  	testEP3 struct{}
    17  )
    18  
    19  func TestConfigModification(t *testing.T) {
    20  	cfg := NewConfig(func() any { return &testCtx{} }, EndpointTypeGetter("ep1", func() any { return &testEP1{} }))
    21  	assert.Equal(t, &testCtx{}, cfg.contextType())
    22  	assert.Equal(t, &testEP1{}, cfg.endpointTypes["ep1"]())
    23  	cfgCopy := cfg
    24  
    25  	// modify existing endpoint
    26  	cfg.SetEndpoint("ep1", func() any { return &testEP2{} })
    27  	// add endpoint
    28  	cfg.SetEndpoint("ep2", func() any { return &testEP3{} })
    29  	assert.Equal(t, &testCtx{}, cfg.contextType())
    30  	assert.Equal(t, &testEP2{}, cfg.endpointTypes["ep1"]())
    31  	assert.Equal(t, &testEP3{}, cfg.endpointTypes["ep2"]())
    32  	// check it applied on already initialized store
    33  	assert.Equal(t, &testCtx{}, cfgCopy.contextType())
    34  	assert.Equal(t, &testEP2{}, cfgCopy.endpointTypes["ep1"]())
    35  	assert.Equal(t, &testEP3{}, cfgCopy.endpointTypes["ep2"]())
    36  }
    37  
    38  func TestValidFilePaths(t *testing.T) {
    39  	paths := map[string]bool{
    40  		"tls/_/../../something":        false,
    41  		"tls/../../something":          false,
    42  		"../../something":              false,
    43  		"/tls/absolute/unix/path":      false,
    44  		`C:\tls\absolute\windows\path`: false,
    45  		"C:/tls/absolute/windows/path": false,
    46  	}
    47  	for p, expectedValid := range paths {
    48  		err := isValidFilePath(p)
    49  		assert.Equal(t, err == nil, expectedValid, "%q should report valid as: %v", p, expectedValid)
    50  	}
    51  }
    52  
    53  func TestValidateContextName(t *testing.T) {
    54  	names := map[string]bool{
    55  		"../../invalid/escape": false,
    56  		"/invalid/absolute":    false,
    57  		`\invalid\windows`:     false,
    58  		"validname":            true,
    59  	}
    60  	for n, expectedValid := range names {
    61  		err := ValidateContextName(n)
    62  		assert.Equal(t, err == nil, expectedValid, "%q should report valid as: %v", n, expectedValid)
    63  	}
    64  }