@@ -66,23 +66,21 @@ func TestHooksPreCommitSecretDetection(t *testing.T) {
6666 // Initialize Git repository
6767 execCmd (t , tmpDir , "git" , "init" )
6868
69+ // Configure Git user
70+ execCmd (
t ,
tmpDir ,
"git" ,
"config" ,
"user.email" ,
"[email protected] " )
71+ execCmd (t , tmpDir , "git" , "config" , "user.name" , "Test User" )
72+
6973 // Install pre-commit hook
7074 _ = executeCmdNilAssertion (t , "Installing pre-commit hook" , "hooks" , "pre-commit" , "secrets-install-git-hook" )
7175
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 )
76+ // Create a test file with a secret
77+ testSecret := "password=secret123"
78+ targetPath := filepath .Join (tmpDir , "test-secret.txt" )
79+ err := os .WriteFile (targetPath , []byte (testSecret ), 0644 )
8280 assert .NoError (t , err , "Failed to write secret file" )
8381
8482 // Add the file to git
85- execCmd (t , tmpDir , "git" , "add" , "mock -secret" )
83+ execCmd (t , tmpDir , "git" , "add" , "test -secret.txt " )
8684
8785 // Try to commit the file - should fail due to secret detection
8886 cmd := exec .Command ("git" , "commit" , "-m" , "Add secret file" )
@@ -92,6 +90,28 @@ func TestHooksPreCommitSecretDetection(t *testing.T) {
9290 assert .Contains (t , string (output ), "Secret detection failed" , "Error message should indicate secret detection failure" )
9391}
9492
93+ func TestHooksPreCommitSecretsScan (t * testing.T ) {
94+ // Store original environment variables
95+ originals := getOriginalEnvVars ()
96+
97+ // Set only the API key to invalid value
98+ setEnvVars (map [string ]string {
99+ params .AstAPIKeyEnv : invalidAPIKey ,
100+ })
101+
102+ // Restore original environment variables after test
103+ defer setEnvVars (originals )
104+
105+ // Define command arguments for running the secrets scan
106+ args := []string {
107+ "hooks" , "pre-commit" , "secrets-scan" ,
108+ }
109+
110+ // Execute the command and verify it fails with the expected error
111+ err , _ := executeCommand (t , args ... )
112+ assert .Error (t , err , "Error validating scan types: Token decoding error: token is malformed: token contains an invalid number of segments" )
113+ }
114+
95115// Helper functions
96116func execCmd (t * testing.T , dir string , name string , args ... string ) {
97117 cmd := exec .Command (name , args ... )
0 commit comments