From 986a9a603b57f0ae987756d816070f416a82f1e4 Mon Sep 17 00:00:00 2001 From: Joao Machado <13315199+machadoit@users.noreply.github.com> Date: Wed, 22 May 2024 15:47:12 +0100 Subject: [PATCH] Rely on run command so the stdout of eslint is shown on terminal --- cmd/analyze.go | 15 +-------------- tools/eslintRunner.go | 20 +++----------------- tools/eslintRunner_test.go | 38 +------------------------------------- 3 files changed, 5 insertions(+), 68 deletions(-) diff --git a/cmd/analyze.go b/cmd/analyze.go index 0e9291b..2ddc226 100644 --- a/cmd/analyze.go +++ b/cmd/analyze.go @@ -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) }, } diff --git a/tools/eslintRunner.go b/tools/eslintRunner.go index 5147426..84e4435 100644 --- a/tools/eslintRunner.go +++ b/tools/eslintRunner.go @@ -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="/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") @@ -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() } diff --git a/tools/eslintRunner_test.go b/tools/eslintRunner_test.go index ccced40..0b99725 100644 --- a/tools/eslintRunner_test.go +++ b/tools/eslintRunner_test.go @@ -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 { @@ -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 {