Skip to content

Commit 83f5c38

Browse files
Merge pull request #1 from codacy/add-method-run-eslint
feature: Add method to run eslint
2 parents edb7fb9 + a85741a commit 83f5c38

File tree

8 files changed

+138
-3
lines changed

8 files changed

+138
-3
lines changed

.github/workflows/go.yml

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
on:
22
pull_request:
33
push:
4-
branches: [ main ]
4+
branches: [main]
55

66
permissions:
77
contents: write
88

99
jobs:
10-
goreleaser:
10+
test:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout
14+
uses: actions/checkout@v4
15+
- name: Set up Go
16+
uses: actions/setup-go@v4
17+
- name: "Run tests"
18+
run: |
19+
go test
20+
21+
release:
1122
runs-on: ubuntu-latest
1223
steps:
1324
- name: Checkout
@@ -28,7 +39,7 @@ jobs:
2839
with:
2940
distribution: goreleaser
3041
# 'latest', 'nightly', or a semver
31-
version: 'latest'
42+
version: "latest"
3243
args: release --clean
3344
env:
3445
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

eslintRunner.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"os/exec"
7+
"path/filepath"
8+
)
9+
10+
// * Run from the root of the repo we want to analyse
11+
// * NODE_PATH="<the installed eslint path>/node_modules"
12+
// * The local installed ESLint should have the @microsoft/eslint-formatter-sarif installed
13+
func runEslint(repositoryToAnalyseDirectory string, eslintInstallationDirectory string, nodeBinary string) (string, error) {
14+
eslintInstallationNodeModules := filepath.Join(eslintInstallationDirectory, "node_modules")
15+
eslintJsPath := filepath.Join(eslintInstallationNodeModules, ".bin/eslint")
16+
17+
cmd := exec.Command(nodeBinary, eslintJsPath, "-f", "@microsoft/eslint-formatter-sarif", ".")
18+
cmd.Dir = repositoryToAnalyseDirectory
19+
cmd.Stderr = os.Stderr
20+
21+
fmt.Println(repositoryToAnalyseDirectory)
22+
23+
nodePathEnv := "NODE_PATH=" + eslintInstallationNodeModules
24+
cmd.Env = append(cmd.Env, nodePathEnv)
25+
26+
out, err := cmd.Output()
27+
if err != nil {
28+
return "", err
29+
}
30+
31+
return string(out), nil
32+
}

eslintRunner_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"os"
6+
"path/filepath"
7+
"strings"
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func TestRunEslint(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 := runEslint(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+
}

go.mod

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ go 1.22.3
44

55
require github.com/spf13/cobra v1.8.0
66

7+
require (
8+
github.com/davecgh/go-spew v1.1.1 // indirect
9+
github.com/pmezard/go-difflib v1.0.0 // indirect
10+
)
11+
712
require (
813
github.com/andybalholm/brotli v1.0.4 // indirect
914
github.com/bodgit/plumbing v1.2.0 // indirect
@@ -25,6 +30,7 @@ require (
2530
github.com/nwaples/rardecode/v2 v2.0.0-beta.2 // indirect
2631
github.com/pierrec/lz4/v4 v4.1.15 // indirect
2732
github.com/spf13/pflag v1.0.5 // indirect
33+
github.com/stretchr/testify v1.9.0
2834
github.com/therootcompany/xz v1.0.1 // indirect
2935
github.com/ulikunitz/xz v0.5.10 // indirect
3036
go4.org v0.0.0-20200411211856-f5505b9728dd // indirect

go.sum

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ github.com/connesc/cipherio v0.2.1 h1:FGtpTPMbKNNWByNrr9aEBtaJtXjqOzkIXNYJp6OEyc
3434
github.com/connesc/cipherio v0.2.1/go.mod h1:ukY0MWJDFnJEbXMQtOcn2VmTpRfzcTz4OoVrWGGJZcA=
3535
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
3636
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
37+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
38+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3739
github.com/dsnet/compress v0.0.1 h1:PlZu0n3Tuv04TzpfPbrnI0HW/YwodEXDS+oPKahKF0Q=
3840
github.com/dsnet/compress v0.0.1/go.mod h1:Aw8dCMJ7RioblQeTqt88akK31OvO8Dhf5JflhBbQEHo=
3941
github.com/dsnet/golib v0.0.0-20171103203638-1ea166775780/go.mod h1:Lj+Z9rebOhdfkVLjJ8T6VcRQv3SXugXy999NBtR9aFY=
@@ -105,6 +107,7 @@ github.com/nwaples/rardecode/v2 v2.0.0-beta.2 h1:e3mzJFJs4k83GXBEiTaQ5HgSc/kOK8q
105107
github.com/nwaples/rardecode/v2 v2.0.0-beta.2/go.mod h1:yntwv/HfMc/Hbvtq9I19D1n58te3h6KsqCf3GxyfBGY=
106108
github.com/pierrec/lz4/v4 v4.1.15 h1:MO0/ucJhngq7299dKLwIMtgTfbkoSPF6AoMYDd8Q4q0=
107109
github.com/pierrec/lz4/v4 v4.1.15/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
110+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
108111
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
109112
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
110113
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
@@ -117,6 +120,8 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
117120
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
118121
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
119122
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
123+
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
124+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
120125
github.com/therootcompany/xz v1.0.1 h1:CmOtsn1CbtmyYiusbfmhmkpAAETj0wBIH6kCYaX+xzw=
121126
github.com/therootcompany/xz v1.0.1/go.mod h1:3K3UH1yCKgBneZYhuQUvJ9HPD19UEXEI0BWbMn8qNMY=
122127
github.com/ulikunitz/xz v0.5.6/go.mod h1:2bypXElzHzzJZwzH67Y6wb67pO62Rzfn7BSiF4ABRW8=
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"version": "2.1.0",
3+
"$schema": "http://json.schemastore.org/sarif-2.1.0-rtm.5",
4+
"runs": [
5+
{
6+
"tool": {
7+
"driver": {
8+
"name": "ESLint",
9+
"informationUri": "https://eslint.org",
10+
"rules": [],
11+
"version": "9.3.0"
12+
}
13+
},
14+
"artifacts": [
15+
{
16+
"location": {
17+
"uri": "testdata/repositories/test1/src/eslint.config.mjs"
18+
}
19+
},
20+
{
21+
"location": {
22+
"uri": "testdata/repositories/test1/src/test.js"
23+
}
24+
}
25+
],
26+
"results": []
27+
}
28+
]
29+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default [
2+
{
3+
rules: {
4+
"prefer-const": "error"
5+
}
6+
}
7+
];
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var foo = "bar";

0 commit comments

Comments
 (0)