-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathpmdRunner_test.go
More file actions
64 lines (54 loc) · 1.82 KB
/
pmdRunner_test.go
File metadata and controls
64 lines (54 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package tools
import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestRunPmdToFile(t *testing.T) {
homeDirectory, err := os.UserHomeDir()
if err != nil {
log.Fatal(err.Error())
}
currentDirectory, err := os.Getwd()
if err != nil {
log.Fatal(err.Error())
}
// Get absolute paths for test files
testDirectory := filepath.Join(currentDirectory, "testdata/repositories/pmd")
tempResultFile := filepath.Join(os.TempDir(), "pmd.sarif")
defer os.Remove(tempResultFile)
// Use absolute paths for repository and ruleset
repositoryToAnalyze := testDirectory
rulesetFile := filepath.Join(testDirectory, "pmd-ruleset.xml")
pmdBinary := filepath.Join(homeDirectory, ".cache/codacy/tools/pmd@7.12.0/pmd-bin-7.12.0/bin/pmd")
err = RunPmd(repositoryToAnalyze, pmdBinary, nil, tempResultFile, "sarif", rulesetFile)
// PMD returns exit status 4 when violations are found, which is expected in our test
if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok && exitErr.ExitCode() != 4 {
t.Fatalf("Failed to run PMD: %v", err)
}
}
// Check if the output file was created
obtainedSarifBytes, err := os.ReadFile(tempResultFile)
if err != nil {
t.Fatalf("Failed to read output file: %v", err)
}
obtainedSarif := string(obtainedSarifBytes)
filePrefix := "file://" + currentDirectory + "/"
fmt.Println(filePrefix)
actualSarif := strings.ReplaceAll(obtainedSarif, filePrefix, "")
actualSarif = strings.TrimSpace(actualSarif)
// Read the expected SARIF
expectedSarifFile := filepath.Join(testDirectory, "expected.sarif")
expectedSarifBytes, err := os.ReadFile(expectedSarifFile)
if err != nil {
log.Fatal(err)
}
expectedSarif := strings.TrimSpace(string(expectedSarifBytes))
assert.Equal(t, expectedSarif, actualSarif, "output did not match expected")
}