11package util
22
33import (
4+ "os"
5+ "strings"
46 "testing"
57
8+ "github.com/checkmarx/ast-cli/internal/wrappers/configuration"
9+ asserts "github.com/stretchr/testify/assert"
610 "gotest.tools/assert"
711)
812
13+ const cxAscaPort = "cx_asca_port"
14+
915func TestNewConfigCommand (t * testing.T ) {
1016 cmd := NewConfigCommand ()
1117 assert .Assert (t , cmd != nil , "Config command must exist" )
@@ -23,3 +29,68 @@ func TestNewConfigCommand(t *testing.T) {
2329 assert .Assert (t , err != nil )
2430 assert .Assert (t , err .Error () == "Failed to set property: unknown property or bad value" )
2531}
32+
33+ func TestGetConfigFilePath_CheckmarxConfigFileExists_Success (t * testing.T ) {
34+ want := ".checkmarx/checkmarxcli.yaml"
35+ got , err := configuration .GetConfigFilePath ()
36+
37+ if err != nil {
38+ t .Errorf ("GetConfigFilePath() error = %v, wantErr = false" , err )
39+ return
40+ }
41+
42+ asserts .True (t , strings .HasSuffix (got , want ), "Expected config file path to end with %q, but got %q" , want , got )
43+ }
44+
45+ func TestWriteSingleConfigKeyToExistingFile_ChangeAscaPortToZero_Success (t * testing.T ) {
46+ configuration .LoadConfiguration ()
47+ configFilePath , _ := configuration .GetConfigFilePath ()
48+ err := configuration .SafeWriteSingleConfigKey (configFilePath , cxAscaPort , 0 )
49+ assert .NilError (t , err )
50+
51+ config , err := configuration .LoadConfig (configFilePath )
52+ assert .NilError (t , err )
53+ asserts .Equal (t , 0 , config [cxAscaPort ])
54+ }
55+
56+ func TestWriteSingleConfigKeyNonExistingFile_CreatingTheFileAndWritesTheKey_Success (t * testing.T ) {
57+ configFilePath := "non-existing-file"
58+
59+ file , err := os .Open (configFilePath )
60+ asserts .NotNil (t , err )
61+ asserts .Nil (t , file )
62+
63+ err = configuration .SafeWriteSingleConfigKey (configFilePath , cxAscaPort , 0 )
64+ assert .NilError (t , err )
65+
66+ file , err = os .Open (configFilePath )
67+ assert .NilError (t , err )
68+ defer func (file * os.File ) {
69+ _ = file .Close ()
70+ _ = os .Remove (configFilePath )
71+ _ = os .Remove (configFilePath + ".lock" )
72+ }(file )
73+ asserts .NotNil (t , file )
74+ }
75+
76+ func TestChangedOnlyAscaPortInConfigFile_ConfigFileExistsWithDefaultValues_OnlyAscaPortChangedSuccess (t * testing.T ) {
77+ configuration .LoadConfiguration ()
78+ configFilePath , _ := configuration .GetConfigFilePath ()
79+
80+ oldConfig , err := configuration .LoadConfig (configFilePath )
81+ assert .NilError (t , err )
82+
83+ err = configuration .SafeWriteSingleConfigKey (configFilePath , cxAscaPort , - 1 )
84+ assert .NilError (t , err )
85+
86+ config , err := configuration .LoadConfig (configFilePath )
87+ assert .NilError (t , err )
88+ asserts .Equal (t , - 1 , config [cxAscaPort ])
89+
90+ // Assert all the other properties are the same
91+ for key , value := range oldConfig {
92+ if key != cxAscaPort {
93+ asserts .Equal (t , value , config [key ])
94+ }
95+ }
96+ }
0 commit comments