Skip to content

Commit 50ebdd8

Browse files
committed
[PLUTO-1412] fix semgrep test
1 parent 3e26afc commit 50ebdd8

File tree

2 files changed

+31
-49
lines changed

2 files changed

+31
-49
lines changed

tools/semgrepConfigCreator.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ type semgrepRulesFile struct {
1515
Rules []map[string]interface{} `yaml:"rules"`
1616
}
1717

18+
// getExecutablePath is a variable that holds the function to get the executable path
19+
// This is used for testing purposes
20+
var getExecutablePath = os.Executable
21+
1822
// FilterRulesFromFile extracts enabled rules from a rules.yaml file based on configuration
1923
func FilterRulesFromFile(rulesFilePath string, config []domain.PatternConfiguration) ([]byte, error) {
2024
// Read the rules.yaml file
@@ -66,7 +70,7 @@ func FilterRulesFromFile(rulesFilePath string, config []domain.PatternConfigurat
6670
// GetSemgrepConfig gets the Semgrep configuration based on the pattern configuration
6771
func GetSemgrepConfig(config []domain.PatternConfiguration) ([]byte, error) {
6872
// Get the executable's directory
69-
execPath, err := os.Executable()
73+
execPath, err := getExecutablePath()
7074
if err != nil {
7175
return nil, fmt.Errorf("failed to get executable path: %w", err)
7276
}
@@ -88,7 +92,7 @@ func GetSemgrepConfig(config []domain.PatternConfiguration) ([]byte, error) {
8892
// GetDefaultSemgrepConfig gets the default Semgrep configuration
8993
func GetDefaultSemgrepConfig() ([]byte, error) {
9094
// Get the executable's directory
91-
execPath, err := os.Executable()
95+
execPath, err := getExecutablePath()
9296
if err != nil {
9397
return nil, fmt.Errorf("failed to get executable path: %w", err)
9498
}

tools/semgrepConfigCreator_test.go

Lines changed: 25 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -101,41 +101,30 @@ func TestFilterRulesFromFile(t *testing.T) {
101101

102102
// TestGetSemgrepConfig tests the GetSemgrepConfig function
103103
func 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
164153
func 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

Comments
 (0)