@@ -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