Skip to content

Commit 394653b

Browse files
authored
Merge pull request #2 from codacy/eslint-to-file
feature: run Eslint to file
2 parents 1763280 + dc702ec commit 394653b

File tree

2 files changed

+62
-7
lines changed

2 files changed

+62
-7
lines changed

eslintRunner.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,37 @@
11
package main
22

33
import (
4-
"fmt"
54
"os"
65
"os/exec"
76
"path/filepath"
87
)
98

9+
func RunEslintToFile(repositoryToAnalyseDirectory string, eslintInstallationDirectory string, nodeBinary string, outputFolder string) error {
10+
_, err := runEslint(repositoryToAnalyseDirectory, eslintInstallationDirectory, nodeBinary, outputFolder)
11+
return err
12+
}
13+
14+
func RunEslintToString(repositoryToAnalyseDirectory string, eslintInstallationDirectory string, nodeBinary string) (string, error) {
15+
return runEslint(repositoryToAnalyseDirectory, eslintInstallationDirectory, nodeBinary, "")
16+
}
17+
1018
// * Run from the root of the repo we want to analyse
1119
// * NODE_PATH="<the installed eslint path>/node_modules"
1220
// * The local installed ESLint should have the @microsoft/eslint-formatter-sarif installed
13-
func runEslint(repositoryToAnalyseDirectory string, eslintInstallationDirectory string, nodeBinary string) (string, error) {
21+
func runEslint(repositoryToAnalyseDirectory string, eslintInstallationDirectory string, nodeBinary string, outputFolder string) (string, error) {
1422
eslintInstallationNodeModules := filepath.Join(eslintInstallationDirectory, "node_modules")
1523
eslintJsPath := filepath.Join(eslintInstallationNodeModules, ".bin/eslint")
1624

17-
cmd := exec.Command(nodeBinary, eslintJsPath, "-f", "@microsoft/eslint-formatter-sarif", ".")
25+
cmd := exec.Command(nodeBinary, eslintJsPath, "-f", "@microsoft/eslint-formatter-sarif")
26+
27+
if outputFolder != "" {
28+
outputFile := filepath.Join(outputFolder, "eslint.sarif")
29+
cmd.Args = append(cmd.Args, "-o", outputFile)
30+
}
31+
1832
cmd.Dir = repositoryToAnalyseDirectory
1933
cmd.Stderr = os.Stderr
2034

21-
fmt.Println(repositoryToAnalyseDirectory)
22-
2335
nodePathEnv := "NODE_PATH=" + eslintInstallationNodeModules
2436
cmd.Env = append(cmd.Env, nodePathEnv)
2537

eslintRunner_test.go

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/stretchr/testify/assert"
1111
)
1212

13-
func TestRunEslint(t *testing.T) {
13+
func TestRunEslintToString(t *testing.T) {
1414
homeDirectory, err := os.UserHomeDir()
1515
if err != nil {
1616
log.Fatal(err.Error())
@@ -25,7 +25,7 @@ func TestRunEslint(t *testing.T) {
2525
eslintInstallationDirectory := filepath.Join(homeDirectory, ".cache/codacy-cli-v2/tools/eslint")
2626
nodeBinary := "node"
2727

28-
eslintOutput, err := runEslint(repositoryToAnalyze, eslintInstallationDirectory, nodeBinary)
28+
eslintOutput, err := RunEslintToString(repositoryToAnalyze, eslintInstallationDirectory, nodeBinary)
2929
if err != nil {
3030
log.Fatal(err.Error())
3131
}
@@ -42,3 +42,46 @@ func TestRunEslint(t *testing.T) {
4242

4343
assert.Equal(t, expectedSarif, actualSarif, "output did not match expected")
4444
}
45+
46+
func TestRunEslintToFile(t *testing.T) {
47+
homeDirectory, err := os.UserHomeDir()
48+
if err != nil {
49+
log.Fatal(err.Error())
50+
}
51+
currentDirectory, err := os.Getwd()
52+
if err != nil {
53+
log.Fatal(err.Error())
54+
}
55+
testDirectory := "testdata/repositories/test1"
56+
tempDir := os.TempDir()
57+
defer os.RemoveAll(tempDir)
58+
59+
repositoryToAnalyze := filepath.Join(testDirectory, "src")
60+
sarifOutputFile := filepath.Join(testDirectory, "sarif.json")
61+
eslintInstallationDirectory := filepath.Join(homeDirectory, ".cache/codacy-cli-v2/tools/eslint")
62+
nodeBinary := "node"
63+
64+
err = RunEslintToFile(repositoryToAnalyze, eslintInstallationDirectory, nodeBinary, tempDir)
65+
if err != nil {
66+
log.Fatal(err.Error())
67+
}
68+
69+
expectedSarifBytes, err := os.ReadFile(sarifOutputFile)
70+
if err != nil {
71+
log.Fatal(err.Error())
72+
}
73+
74+
eslintOutputPath := filepath.Join(tempDir, "eslint.sarif")
75+
76+
eslintOutputBytes, err := os.ReadFile(eslintOutputPath)
77+
if err != nil {
78+
log.Fatal(err.Error())
79+
}
80+
eslintOutput := string(eslintOutputBytes)
81+
filePrefix := "file://" + currentDirectory + "/"
82+
actualSarif := strings.ReplaceAll(eslintOutput, filePrefix, "")
83+
84+
expectedSarif := strings.TrimSpace(string(expectedSarifBytes))
85+
86+
assert.Equal(t, expectedSarif, actualSarif, "output did not match expected")
87+
}

0 commit comments

Comments
 (0)