@@ -4249,3 +4249,185 @@ func TestEnforceLocalResolutionForTarFiles_Integration(t *testing.T) {
42494249 })
42504250 }
42514251}
4252+
4253+ func TestValidateGitCommitHistoryFlag (t * testing.T ) {
4254+ tests := []struct {
4255+ name string
4256+ flagValue string
4257+ expectedErrorMsg string
4258+ }{
4259+ {
4260+ name : "Valid true value" ,
4261+ flagValue : "true" ,
4262+ },
4263+ {
4264+ name : "Valid false value" ,
4265+ flagValue : "false" ,
4266+ },
4267+ {
4268+ name : "Valid TRUE value (case insensitive)" ,
4269+ flagValue : "TRUE" ,
4270+ },
4271+ {
4272+ name : "Valid FALSE value (case insensitive)" ,
4273+ flagValue : "FALSE" ,
4274+ },
4275+ {
4276+ name : "Invalid value 'maybe'" ,
4277+ flagValue : "maybe" ,
4278+ expectedErrorMsg : "Invalid value for --git-commit-history. Use 'true' or 'false'." ,
4279+ },
4280+ {
4281+ name : "Empty value" ,
4282+ flagValue : "" ,
4283+ },
4284+ }
4285+
4286+ for _ , tt := range tests {
4287+ t .Run (tt .name , func (t * testing.T ) {
4288+ cmdCommand := & cobra.Command {}
4289+ cmdCommand .PersistentFlags ().String (commonParams .GitCommitHistoryFlag , "false" , commonParams .GitCommitHistoryFlagUsage )
4290+ _ = cmdCommand .Flags ().Set (commonParams .GitCommitHistoryFlag , tt .flagValue )
4291+
4292+ err := validateGitCommitHistoryFlag (cmdCommand )
4293+ if err != nil {
4294+ assert .Assert (t , err .Error () == tt .expectedErrorMsg , "Expected error: %v, got: %v" , tt .expectedErrorMsg , err )
4295+ } else {
4296+ assert .NilError (t , err , "Expected no error, got: %v" , err )
4297+ }
4298+ })
4299+ }
4300+ }
4301+
4302+ func TestShouldEnableGitCommitHistory (t * testing.T ) {
4303+ // Create a temporary directory with .git for testing
4304+ tempDir := t .TempDir ()
4305+ gitDir := filepath .Join (tempDir , ".git" )
4306+ _ = os .Mkdir (gitDir , 0755 )
4307+
4308+ // Create a directory without .git
4309+ tempDirNoGit := t .TempDir ()
4310+
4311+ // Create a directory with .git in a subdirectory
4312+ tempDirWithSubGit := t .TempDir ()
4313+ subDir := filepath .Join (tempDirWithSubGit , "project1" )
4314+ _ = os .Mkdir (subDir , 0755 )
4315+ gitDirSub := filepath .Join (subDir , ".git" )
4316+ _ = os .Mkdir (gitDirSub , 0755 )
4317+
4318+ tests := []struct {
4319+ name string
4320+ flagValue string
4321+ scanTypes string
4322+ scsEngines string
4323+ source string
4324+ expectEnabled bool
4325+ expectWarnings []string
4326+ }{
4327+ {
4328+ name : "Flag set to false - disabled" ,
4329+ flagValue : "false" ,
4330+ scanTypes : "scs" ,
4331+ scsEngines : "secret-detection" ,
4332+ source : tempDir ,
4333+ expectEnabled : false ,
4334+ },
4335+ {
4336+ name : "Flag set to true with SCS not in scan types - warning and disabled" ,
4337+ flagValue : "true" ,
4338+ scanTypes : "sast" ,
4339+ scsEngines : "secret-detection" ,
4340+ source : tempDir ,
4341+ expectEnabled : false ,
4342+ expectWarnings : []string {"--git-commit-history' was provided, but SCS is not selected" },
4343+ },
4344+ {
4345+ name : "Flag set to true with only scorecard - warning and disabled" ,
4346+ flagValue : "true" ,
4347+ scanTypes : "scs" ,
4348+ scsEngines : "scorecard" ,
4349+ source : tempDir ,
4350+ expectEnabled : false ,
4351+ expectWarnings : []string {"Commit History applies only to Secret Detection" },
4352+ },
4353+ {
4354+ name : "Flag set to true without git context - warning and disabled" ,
4355+ flagValue : "true" ,
4356+ scanTypes : "scs" ,
4357+ scsEngines : "secret-detection" ,
4358+ source : tempDirNoGit ,
4359+ expectEnabled : false ,
4360+ expectWarnings : []string {"No Git history found" },
4361+ },
4362+ {
4363+ name : "Flag set to true with git context (directory) - enabled" ,
4364+ flagValue : "true" ,
4365+ scanTypes : "scs" ,
4366+ scsEngines : "secret-detection" ,
4367+ source : tempDir ,
4368+ expectEnabled : true ,
4369+ },
4370+ {
4371+ name : "Flag set to true with git in subdirectory - enabled" ,
4372+ flagValue : "true" ,
4373+ scanTypes : "scs" ,
4374+ scsEngines : "secret-detection" ,
4375+ source : tempDirWithSubGit ,
4376+ expectEnabled : true ,
4377+ },
4378+ {
4379+ name : "Flag set to true with both secret-detection and scorecard - enabled" ,
4380+ flagValue : "true" ,
4381+ scanTypes : "scs" ,
4382+ scsEngines : "secret-detection,scorecard" ,
4383+ source : tempDir ,
4384+ expectEnabled : true ,
4385+ },
4386+ }
4387+
4388+ for _ , tt := range tests {
4389+ t .Run (tt .name , func (t * testing.T ) {
4390+ // Create command with flags
4391+ cmdCommand := & cobra.Command {
4392+ Use : "scan" ,
4393+ Short : "Scan a project with git commit history" ,
4394+ }
4395+ cmdCommand .PersistentFlags ().String (commonParams .GitCommitHistoryFlag , "false" , commonParams .GitCommitHistoryFlagUsage )
4396+ cmdCommand .PersistentFlags ().String (commonParams .ScanTypes , "" , "Scan types" )
4397+ cmdCommand .PersistentFlags ().String (commonParams .SCSEnginesFlag , "" , "SCS engines" )
4398+ cmdCommand .PersistentFlags ().String (commonParams .SourcesFlag , "" , "Sources" )
4399+
4400+ _ = cmdCommand .Execute ()
4401+
4402+ _ = cmdCommand .Flags ().Set (commonParams .GitCommitHistoryFlag , tt .flagValue )
4403+ _ = cmdCommand .Flags ().Set (commonParams .ScanTypes , tt .scanTypes )
4404+ _ = cmdCommand .Flags ().Set (commonParams .SCSEnginesFlag , tt .scsEngines )
4405+ _ = cmdCommand .Flags ().Set (commonParams .SourcesFlag , tt .source )
4406+
4407+ // Capture output for warnings (fmt.Println goes to stdout)
4408+ oldStdout := os .Stdout
4409+ r , w , _ := os .Pipe ()
4410+ os .Stdout = w
4411+
4412+ result := shouldEnableGitCommitHistory (cmdCommand )
4413+
4414+ w .Close ()
4415+ os .Stdout = oldStdout
4416+
4417+ // Read captured output
4418+ var buf bytes.Buffer
4419+ _ , _ = io .Copy (& buf , r )
4420+ r .Close ()
4421+ output := buf .String ()
4422+
4423+ // Check result
4424+ assert .Equal (t , tt .expectEnabled , result , "Expected enabled=%v, got=%v" , tt .expectEnabled , result )
4425+
4426+ // Check warnings
4427+ for _ , expectedWarning := range tt .expectWarnings {
4428+ assert .Assert (t , strings .Contains (output , expectedWarning ),
4429+ "Expected warning containing '%s' not found in output: %s" , expectedWarning , output )
4430+ }
4431+ })
4432+ }
4433+ }
0 commit comments