@@ -101,41 +101,30 @@ func TestFilterRulesFromFile(t *testing.T) {
101101
102102// TestGetSemgrepConfig tests the GetSemgrepConfig function
103103func TestGetSemgrepConfig (t * testing.T ) {
104- // Override the function to find rules.yaml to use our test file
105- originalRulesFilePath := filepath .Join ("plugins" , "tools" , "semgrep" , "rules.yaml" )
106-
107104 // Create a temporary rules file
108105 tempDir := t .TempDir ()
109106 testRulesFile := filepath .Join (tempDir , "rules.yaml" )
110107 err := os .WriteFile (testRulesFile , []byte (sampleRulesYAML ), 0644 )
111108 assert .NoError (t , err )
112109
113- // Create a backup of the original file if it exists
114- backupFilePath := ""
115- if _ , err := os .Stat (originalRulesFilePath ); err == nil {
116- backupFilePath = originalRulesFilePath + ".bak"
117- err = os .Rename (originalRulesFilePath , backupFilePath )
118- assert .NoError (t , err )
110+ // Create a mock executable path that points to our temp directory
111+ originalGetExecutablePath := getExecutablePath
112+ getExecutablePath = func () (string , error ) {
113+ return filepath .Join (tempDir , "test-executable" ), nil
119114 }
115+ defer func () {
116+ getExecutablePath = originalGetExecutablePath
117+ }()
120118
121- // Ensure the directory exists
122- err = os .MkdirAll (filepath .Dir (originalRulesFilePath ), 0755 )
119+ // Create the plugins directory structure
120+ pluginsDir := filepath .Join (tempDir , "plugins" , "tools" , "semgrep" )
121+ err = os .MkdirAll (pluginsDir , 0755 )
123122 assert .NoError (t , err )
124123
125- // Copy our test file to the location
126- testFileContent , err := os .ReadFile (testRulesFile )
127- assert .NoError (t , err )
128- err = os .WriteFile (originalRulesFilePath , testFileContent , 0644 )
124+ // Copy our test file to the plugins directory
125+ err = os .WriteFile (filepath .Join (pluginsDir , "rules.yaml" ), []byte (sampleRulesYAML ), 0644 )
129126 assert .NoError (t , err )
130127
131- // Clean up after the test
132- defer func () {
133- os .Remove (originalRulesFilePath )
134- if backupFilePath != "" {
135- os .Rename (backupFilePath , originalRulesFilePath )
136- }
137- }()
138-
139128 // Test with valid configuration
140129 config := []domain.PatternConfiguration {
141130 {
@@ -162,41 +151,30 @@ func TestGetSemgrepConfig(t *testing.T) {
162151
163152// TestGetDefaultSemgrepConfig tests the GetDefaultSemgrepConfig function
164153func TestGetDefaultSemgrepConfig (t * testing.T ) {
165- // Override the function to find rules.yaml to use our test file
166- originalRulesFilePath := filepath .Join ("plugins" , "tools" , "semgrep" , "rules.yaml" )
167-
168154 // Create a temporary rules file
169155 tempDir := t .TempDir ()
170156 testRulesFile := filepath .Join (tempDir , "rules.yaml" )
171157 err := os .WriteFile (testRulesFile , []byte (sampleRulesYAML ), 0644 )
172158 assert .NoError (t , err )
173159
174- // Create a backup of the original file if it exists
175- backupFilePath := ""
176- if _ , err := os .Stat (originalRulesFilePath ); err == nil {
177- backupFilePath = originalRulesFilePath + ".bak"
178- err = os .Rename (originalRulesFilePath , backupFilePath )
179- assert .NoError (t , err )
160+ // Create a mock executable path that points to our temp directory
161+ originalGetExecutablePath := getExecutablePath
162+ getExecutablePath = func () (string , error ) {
163+ return filepath .Join (tempDir , "test-executable" ), nil
180164 }
165+ defer func () {
166+ getExecutablePath = originalGetExecutablePath
167+ }()
181168
182- // Ensure the directory exists
183- err = os .MkdirAll (filepath .Dir (originalRulesFilePath ), 0755 )
169+ // Create the plugins directory structure
170+ pluginsDir := filepath .Join (tempDir , "plugins" , "tools" , "semgrep" )
171+ err = os .MkdirAll (pluginsDir , 0755 )
184172 assert .NoError (t , err )
185173
186- // Copy our test file to the location
187- testFileContent , err := os .ReadFile (testRulesFile )
188- assert .NoError (t , err )
189- err = os .WriteFile (originalRulesFilePath , testFileContent , 0644 )
174+ // Copy our test file to the plugins directory
175+ err = os .WriteFile (filepath .Join (pluginsDir , "rules.yaml" ), []byte (sampleRulesYAML ), 0644 )
190176 assert .NoError (t , err )
191177
192- // Clean up after the test
193- defer func () {
194- os .Remove (originalRulesFilePath )
195- if backupFilePath != "" {
196- os .Rename (backupFilePath , originalRulesFilePath )
197- }
198- }()
199-
200178 // Test getting default config
201179 result , err := GetDefaultSemgrepConfig ()
202180 assert .NoError (t , err )
@@ -207,7 +185,7 @@ func TestGetDefaultSemgrepConfig(t *testing.T) {
207185 assert .Equal (t , 3 , len (parsedRules .Rules ))
208186
209187 // Test when rules.yaml doesn't exist
210- os .Remove (originalRulesFilePath )
188+ os .Remove (filepath . Join ( pluginsDir , "rules.yaml" ) )
211189 _ , err = GetDefaultSemgrepConfig ()
212190 assert .Error (t , err )
213191 assert .Contains (t , err .Error (), "rules.yaml not found" )
0 commit comments