Skip to content

Commit 1aed4c6

Browse files
feat: update semgrep version and add tests
- Updated semgrep version in codacy.yaml from 1.33.2 to 1.78.0. - Added semgrep to the list of supported tools in tool-utils_test.go. - Implemented RunSemgrep function in semgrepRunner.go to execute Semgrep analysis. - Created tests for Semgrep functionality in semgrepRunner_test.go. - Added sample JavaScript file and expected SARIF output for testing.
1 parent 3faf5b0 commit 1aed4c6

File tree

6 files changed

+86
-6
lines changed

6 files changed

+86
-6
lines changed

.codacy/codacy.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ tools:
66
77
88
9-
- semgrep@1.33.2
9+
- semgrep@1.78.0

plugins/tool-utils_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ func TestGetSupportedTools(t *testing.T) {
166166
"pmd",
167167
"pylint",
168168
"trivy",
169+
"semgrep",
169170
},
170171
expectedError: false,
171172
},

tools/semgrepRunner.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@ import (
1010

1111
// RunSemgrep executes Semgrep analysis on the specified directory
1212
func RunSemgrep(workDirectory string, toolInfo *plugins.ToolInfo, files []string, outputFile string, outputFormat string) error {
13-
// Get Python binary from venv
14-
pythonPath := filepath.Join(toolInfo.InstallDir, "venv", "bin", "python3")
15-
1613
// Construct base command with -m semgrep to run semgrep module
17-
cmdArgs := []string{"-m", "semgrep", "scan"}
14+
cmdArgs := []string{"scan"}
1815

1916
// Add output format if specified
2017
if outputFormat == "sarif" {
@@ -31,8 +28,13 @@ func RunSemgrep(workDirectory string, toolInfo *plugins.ToolInfo, files []string
3128
cmdArgs = append(cmdArgs, ".")
3229
}
3330

31+
cmdArgs = append(cmdArgs, "--disable-version-check")
32+
33+
// Get Semgrep binary from the specified installation path
34+
semgrepPath := filepath.Join(toolInfo.InstallDir, "venv", "bin", "semgrep")
35+
3436
// Create Semgrep command
35-
cmd := exec.Command(pythonPath, cmdArgs...)
37+
cmd := exec.Command(semgrepPath, cmdArgs...)
3638
cmd.Dir = workDirectory
3739

3840
// If output file is specified, create it and redirect output

tools/semgrepRunner_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package tools
2+
3+
import (
4+
"codacy/cli-v2/plugins"
5+
"log"
6+
"os"
7+
"path/filepath"
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func TestRunSemgrepWithSpecificFiles(t *testing.T) {
14+
homeDirectory, err := os.UserHomeDir()
15+
if err != nil {
16+
log.Fatal(err.Error())
17+
}
18+
currentDirectory, err := os.Getwd()
19+
if err != nil {
20+
log.Fatal(err.Error())
21+
}
22+
23+
// Set up test directories and files
24+
testDirectory := filepath.Join(currentDirectory, "testdata", "repositories", "semgrep")
25+
tempResultFile := filepath.Join(os.TempDir(), "semgrep-specific.sarif")
26+
defer os.Remove(tempResultFile)
27+
28+
// Create tool info for semgrep
29+
toolInfo := &plugins.ToolInfo{
30+
InstallDir: filepath.Join(homeDirectory, ".cache/codacy/tools/[email protected]"),
31+
}
32+
33+
// Specify files to analyze
34+
filesToAnalyze := []string{"sample.js"}
35+
36+
// Run Semgrep analysis on specific files
37+
err = RunSemgrep(testDirectory, toolInfo, filesToAnalyze, tempResultFile, "sarif")
38+
if err != nil {
39+
t.Fatalf("Failed to run semgrep on specific files: %v", err)
40+
}
41+
42+
// Verify file exists and has content
43+
fileInfo, err := os.Stat(tempResultFile)
44+
assert.NoError(t, err, "Failed to stat output file")
45+
assert.Greater(t, fileInfo.Size(), int64(0), "Output file should not be empty")
46+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"version": "2.1.0",
3+
"$schema": "http://json.schemastore.org/sarif-2.1.0-rtm.5",
4+
"runs": [
5+
{
6+
"tool": {
7+
"driver": {
8+
"name": "Semgrep",
9+
"version": "1.41.0",
10+
"informationUri": "https://semgrep.dev",
11+
"rules": []
12+
}
13+
},
14+
"artifacts": [
15+
{
16+
"location": {
17+
"uri": "testdata/repositories/semgrep/sample.js"
18+
}
19+
}
20+
],
21+
"results": []
22+
}
23+
]
24+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Sample JavaScript file for Semgrep testing
2+
3+
function helloWorld() {
4+
console.log("Hello, world!");
5+
}
6+
7+
helloWorld();

0 commit comments

Comments
 (0)