Skip to content

Commit

Permalink
Rely on run command so the stdout of eslint is shown on terminal
Browse files Browse the repository at this point in the history
  • Loading branch information
machadoit committed May 22, 2024
1 parent f1a1714 commit 986a9a6
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 68 deletions.
15 changes: 1 addition & 14 deletions cmd/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,6 @@ var analyzeCmd = &cobra.Command{
fmt.Printf("Output will be available at %s\n", outputFile)
}

if outputFile != "" {
err = tools.RunEslintToFile(workDirectory, eslintInstallationDirectory, nodeBinary, outputFile)
} else {
out, err2 := tools.RunEslintToString(workDirectory, eslintInstallationDirectory, nodeBinary)
if err2 != nil {
log.Fatal(err2)
}

fmt.Println(out)
}

if err != nil {
log.Fatal(err)
}
tools.RunEslint(workDirectory, eslintInstallationDirectory, nodeBinary, outputFile)
},
}
20 changes: 3 additions & 17 deletions tools/eslintRunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,10 @@ import (
"path/filepath"
)

func RunEslintToFile(repositoryToAnalyseDirectory string, eslintInstallationDirectory string, nodeBinary string, outputFile string) error {
_, err := runEslint(repositoryToAnalyseDirectory, eslintInstallationDirectory, nodeBinary, outputFile)
return err
}

func RunEslintToString(repositoryToAnalyseDirectory string, eslintInstallationDirectory string, nodeBinary string) (string, error) {
return runEslint(repositoryToAnalyseDirectory, eslintInstallationDirectory, nodeBinary, "")
}

// * Run from the root of the repo we want to analyse
// * NODE_PATH="<the installed eslint path>/node_modules"
// * The local installed ESLint should have the @microsoft/eslint-formatter-sarif installed
func runEslint(repositoryToAnalyseDirectory string, eslintInstallationDirectory string, nodeBinary string, outputFile string) (string, error) {
func RunEslint(repositoryToAnalyseDirectory string, eslintInstallationDirectory string, nodeBinary string, outputFile string) {
eslintInstallationNodeModules := filepath.Join(eslintInstallationDirectory, "node_modules")
eslintJsPath := filepath.Join(eslintInstallationNodeModules, ".bin", "eslint")

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

cmd.Dir = repositoryToAnalyseDirectory
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout

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

// TODO eslint returns 1 when it finds errors, so we're not propagating it
out, _ := cmd.Output()

//DEBUG:
//fmt.Println(cmd.Env)
//fmt.Println(cmd)

return string(out), nil
cmd.Run()
}
38 changes: 1 addition & 37 deletions tools/eslintRunner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,6 @@ import (
"github.com/stretchr/testify/assert"
)

func TestRunEslintToString(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())
}
testDirectory := "testdata/repositories/test1"
repositoryToAnalyze := filepath.Join(testDirectory, "src")
sarifOutputFile := filepath.Join(testDirectory, "sarif.json")
eslintInstallationDirectory := filepath.Join(homeDirectory, ".cache/codacy-cli-v2/tools/eslint")
nodeBinary := "node"

eslintOutput, err := RunEslintToString(repositoryToAnalyze, eslintInstallationDirectory, nodeBinary)
if err != nil {
log.Fatal(err.Error())
}

expectedSarifBytes, err := os.ReadFile(sarifOutputFile)
if err != nil {
log.Fatal(err.Error())
}

filePrefix := "file://" + currentDirectory + "/"
actualSarif := strings.ReplaceAll(eslintOutput, filePrefix, "")

expectedSarif := string(expectedSarifBytes)

assert.Equal(t, expectedSarif, actualSarif, "output did not match expected")
}

func TestRunEslintToFile(t *testing.T) {
homeDirectory, err := os.UserHomeDir()
if err != nil {
Expand All @@ -61,10 +28,7 @@ func TestRunEslintToFile(t *testing.T) {
eslintInstallationDirectory := filepath.Join(homeDirectory, ".cache/codacy-cli-v2/tools/eslint")
nodeBinary := "node"

err = RunEslintToFile(repositoryToAnalyze, eslintInstallationDirectory, nodeBinary, tempDir)
if err != nil {
log.Fatal(err.Error())
}
RunEslint(repositoryToAnalyze, eslintInstallationDirectory, nodeBinary, sarifOutputFile)

expectedSarifBytes, err := os.ReadFile(sarifOutputFile)
if err != nil {
Expand Down

0 comments on commit 986a9a6

Please sign in to comment.