Skip to content

Commit 986a9a6

Browse files
committed
Rely on run command so the stdout of eslint is shown on terminal
1 parent f1a1714 commit 986a9a6

File tree

3 files changed

+5
-68
lines changed

3 files changed

+5
-68
lines changed

cmd/analyze.go

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,6 @@ var analyzeCmd = &cobra.Command{
4545
fmt.Printf("Output will be available at %s\n", outputFile)
4646
}
4747

48-
if outputFile != "" {
49-
err = tools.RunEslintToFile(workDirectory, eslintInstallationDirectory, nodeBinary, outputFile)
50-
} else {
51-
out, err2 := tools.RunEslintToString(workDirectory, eslintInstallationDirectory, nodeBinary)
52-
if err2 != nil {
53-
log.Fatal(err2)
54-
}
55-
56-
fmt.Println(out)
57-
}
58-
59-
if err != nil {
60-
log.Fatal(err)
61-
}
48+
tools.RunEslint(workDirectory, eslintInstallationDirectory, nodeBinary, outputFile)
6249
},
6350
}

tools/eslintRunner.go

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,10 @@ import (
66
"path/filepath"
77
)
88

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

@@ -30,16 +21,11 @@ func runEslint(repositoryToAnalyseDirectory string, eslintInstallationDirectory
3021

3122
cmd.Dir = repositoryToAnalyseDirectory
3223
cmd.Stderr = os.Stderr
24+
cmd.Stdout = os.Stdout
3325

3426
nodePathEnv := "NODE_PATH=" + eslintInstallationNodeModules
3527
cmd.Env = append(cmd.Env, nodePathEnv)
3628

3729
// TODO eslint returns 1 when it finds errors, so we're not propagating it
38-
out, _ := cmd.Output()
39-
40-
//DEBUG:
41-
//fmt.Println(cmd.Env)
42-
//fmt.Println(cmd)
43-
44-
return string(out), nil
30+
cmd.Run()
4531
}

tools/eslintRunner_test.go

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,6 @@ import (
1010
"github.com/stretchr/testify/assert"
1111
)
1212

13-
func TestRunEslintToString(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-
testDirectory := "testdata/repositories/test1"
23-
repositoryToAnalyze := filepath.Join(testDirectory, "src")
24-
sarifOutputFile := filepath.Join(testDirectory, "sarif.json")
25-
eslintInstallationDirectory := filepath.Join(homeDirectory, ".cache/codacy-cli-v2/tools/eslint")
26-
nodeBinary := "node"
27-
28-
eslintOutput, err := RunEslintToString(repositoryToAnalyze, eslintInstallationDirectory, nodeBinary)
29-
if err != nil {
30-
log.Fatal(err.Error())
31-
}
32-
33-
expectedSarifBytes, err := os.ReadFile(sarifOutputFile)
34-
if err != nil {
35-
log.Fatal(err.Error())
36-
}
37-
38-
filePrefix := "file://" + currentDirectory + "/"
39-
actualSarif := strings.ReplaceAll(eslintOutput, filePrefix, "")
40-
41-
expectedSarif := string(expectedSarifBytes)
42-
43-
assert.Equal(t, expectedSarif, actualSarif, "output did not match expected")
44-
}
45-
4613
func TestRunEslintToFile(t *testing.T) {
4714
homeDirectory, err := os.UserHomeDir()
4815
if err != nil {
@@ -61,10 +28,7 @@ func TestRunEslintToFile(t *testing.T) {
6128
eslintInstallationDirectory := filepath.Join(homeDirectory, ".cache/codacy-cli-v2/tools/eslint")
6229
nodeBinary := "node"
6330

64-
err = RunEslintToFile(repositoryToAnalyze, eslintInstallationDirectory, nodeBinary, tempDir)
65-
if err != nil {
66-
log.Fatal(err.Error())
67-
}
31+
RunEslint(repositoryToAnalyze, eslintInstallationDirectory, nodeBinary, sarifOutputFile)
6832

6933
expectedSarifBytes, err := os.ReadFile(sarifOutputFile)
7034
if err != nil {

0 commit comments

Comments
 (0)