@@ -58,6 +58,40 @@ func TestHooksPreCommitLicenseValidation(t *testing.T) {
5858 assert .Error (t , err , "Error validating scan types: Token decoding error: token is malformed: token contains an invalid number of segments" )
5959}
6060
61+ func TestHooksPreCommitSecretDetection (t * testing.T ) {
62+ // Create a temporary directory and initialize git repository
63+ tmpDir , cleanup := setupTempDir (t )
64+ defer cleanup ()
65+
66+ // Initialize Git repository
67+ execCmd (t , tmpDir , "git" , "init" )
68+
69+ // Install pre-commit hook
70+ _ = executeCmdNilAssertion (t , "Installing pre-commit hook" , "hooks" , "pre-commit" , "secrets-install-git-hook" )
71+
72+ // Copy the mock secret file to the temporary directory
73+ mockSecretPath := filepath .Join ("data" , "mock-secret" )
74+ targetPath := filepath .Join (tmpDir , "mock-secret" )
75+
76+ // Read the mock secret file
77+ secretContent , err := os .ReadFile (mockSecretPath )
78+ assert .NoError (t , err , "Failed to read mock secret file" )
79+
80+ // Write the content to the target file
81+ err = os .WriteFile (targetPath , secretContent , 0644 )
82+ assert .NoError (t , err , "Failed to write secret file" )
83+
84+ // Add the file to git
85+ execCmd (t , tmpDir , "git" , "add" , "mock-secret" )
86+
87+ // Try to commit the file - should fail due to secret detection
88+ cmd := exec .Command ("git" , "commit" , "-m" , "Add secret file" )
89+ cmd .Dir = tmpDir
90+ output , err := cmd .CombinedOutput ()
91+ assert .Error (t , err , "Commit should fail due to secret detection" )
92+ assert .Contains (t , string (output ), "Secret detection failed" , "Error message should indicate secret detection failure" )
93+ }
94+
6195// Helper functions
6296func execCmd (t * testing.T , dir string , name string , args ... string ) {
6397 cmd := exec .Command (name , args ... )
0 commit comments