* remove unused method * whitespace, redundant comments * use test helpers * move DumpConsoleConfig() from pkg/csconfig to cscli * package doc header * var -> const * rename ./tests -> ./testdata * shorter tests with more error checks * lint/formatting * use helpers; fix tests that didn't actually test * lint; rename expectedResult -> expected
70 lines
1.5 KiB
Go
70 lines
1.5 KiB
Go
package csconfig
|
|
|
|
import (
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/crowdsecurity/go-cs-lib/cstest"
|
|
)
|
|
|
|
func TestLoadCSCLI(t *testing.T) {
|
|
hubFullPath, err := filepath.Abs("./hub")
|
|
require.NoError(t, err)
|
|
|
|
dataFullPath, err := filepath.Abs("./data")
|
|
require.NoError(t, err)
|
|
|
|
configDirFullPath, err := filepath.Abs("./testdata")
|
|
require.NoError(t, err)
|
|
|
|
hubIndexFileFullPath, err := filepath.Abs("./hub/.index.json")
|
|
require.NoError(t, err)
|
|
|
|
tests := []struct {
|
|
name string
|
|
input *Config
|
|
expected *CscliCfg
|
|
expectedErr string
|
|
}{
|
|
{
|
|
name: "basic valid configuration",
|
|
input: &Config{
|
|
ConfigPaths: &ConfigurationPaths{
|
|
ConfigDir: "./testdata",
|
|
DataDir: "./data",
|
|
HubDir: "./hub",
|
|
HubIndexFile: "./hub/.index.json",
|
|
},
|
|
},
|
|
expected: &CscliCfg{
|
|
ConfigDir: configDirFullPath,
|
|
DataDir: dataFullPath,
|
|
HubDir: hubFullPath,
|
|
HubIndexFile: hubIndexFileFullPath,
|
|
},
|
|
},
|
|
{
|
|
name: "no configuration path",
|
|
input: &Config{},
|
|
expected: &CscliCfg{},
|
|
expectedErr: "no configuration paths provided",
|
|
},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
tc := tc
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
err := tc.input.LoadCSCLI()
|
|
cstest.RequireErrorContains(t, err, tc.expectedErr)
|
|
if tc.expectedErr != "" {
|
|
return
|
|
}
|
|
|
|
assert.Equal(t, tc.expected, tc.input.Cscli)
|
|
})
|
|
}
|
|
}
|