From e0dd7c789b8c3595b2574dac868edb1d4d26bfb7 Mon Sep 17 00:00:00 2001 From: Pavel Sorokin <60606414+pavel-snyk@users.noreply.github.com> Date: Tue, 1 Mar 2022 10:39:13 +0100 Subject: [PATCH] feat: integrate GoReleaser for releasing binaries (#6) * chore: add release workflow with GoReleaser tool * Fix linting issues * Activate release workflow on push in main branch * Disable releasing from old GH actions workflow --- .github/workflows/release.yaml | 65 +++++++++++++++++++++++++++ .github/workflows/workflow.yaml | 19 ++++---- .goreleaser.yaml | 42 +++++++++++++++++ .idea/.gitignore | 9 ---- code/backend_service.go | 8 ++-- code/backend_service_interface.go | 3 +- code/backend_service_test.go | 8 ++-- code/bundle.go | 10 +++-- code/bundle_test.go | 10 +++-- code/fake_backend_service.go | 10 +++-- diagnostics/code_lenses_test.go | 3 +- diagnostics/diagnostics.go | 6 ++- diagnostics/diagnostics_test.go | 8 ++-- iac/iac.go | 16 ++++--- iac/iac_test.go | 12 ++--- main.go | 6 ++- main_test.go | 19 +++++--- oss/oss.go | 16 ++++--- oss/oss_test.go | 10 +++-- server/server.go | 8 ++-- server/server_test.go | 75 +++++++++++++++++++++---------- util/config.go | 5 ++- util/config_test.go | 27 +++++++---- util/util_test.go | 3 +- 24 files changed, 285 insertions(+), 113 deletions(-) create mode 100644 .github/workflows/release.yaml create mode 100644 .goreleaser.yaml delete mode 100644 .idea/.gitignore diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml new file mode 100644 index 000000000..122b7baf7 --- /dev/null +++ b/.github/workflows/release.yaml @@ -0,0 +1,65 @@ +name: Release +on: + push: + branches: + - 'main' + +jobs: + release: + name: goreleaser + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Unshallow + run: git fetch --prune --unshallow + + # this step can be removed if setup-go will support reading go-version from go.mod + - name: Determine Go version + run: | + sed -En 's/^go[[:space:]]+([[:digit:].]+)$/GO_VERSION=\1/p' go.mod >> $GITHUB_ENV + + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: ${{ env.GO_VERSION }} + + - name: Set up Snyk CLI + uses: snyk/actions/setup@master + + - name: Cache Go modules + uses: actions/cache@v2 + with: + path: | + ~/.cache/go-build + ~/go/pkg/mod + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} + restore-keys: ${{ runner.os }}-go- + + - name: Lint source code + run: | + make tools lint + + - name: Run tests + env: + DEEPROXY_API_URL: ${{secrets.DEEPROXY_API_URL}} + SNYK_TOKEN: ${{secrets.SNYK_TOKEN }} + run: | + make clean test + + - name: Set up Git actions user + uses: fregante/setup-git-user@v1 + + - name: Create release tag + run: | + git tag "v$(date +'%Y%m%d.%H%M%S')" + git push --tags + sudo mv snyk-linux* /usr/local/bin/ + + - name: Release binaries with GoReleaser + uses: goreleaser/goreleaser-action@v2 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + args: release --rm-dist diff --git a/.github/workflows/workflow.yaml b/.github/workflows/workflow.yaml index d1e456edb..71dd3f1ab 100644 --- a/.github/workflows/workflow.yaml +++ b/.github/workflows/workflow.yaml @@ -68,12 +68,13 @@ jobs: version: latest files: output/* args: "-9f --lzma" - - uses: "marvinpinto/action-automatic-releases@latest" - if: ${{ github.event_name == 'push' && !env.ACT }} - with: - repo_token: "${{ secrets.GITHUB_TOKEN }}" - automatic_release_tag: "${{ env.NOW }}" - prerelease: false - title: "${{ env.NOW }}" - files: | - output/* + # TODO(pavel): refactor the current workflow after https://github.com/snyk/snyk-lsp/pull/6 is merged +# - uses: "marvinpinto/action-automatic-releases@latest" +# if: ${{ github.event_name == 'push' && !env.ACT }} +# with: +# repo_token: "${{ secrets.GITHUB_TOKEN }}" +# automatic_release_tag: "${{ env.NOW }}" +# prerelease: false +# title: "${{ env.NOW }}" +# files: | +# output/* diff --git a/.goreleaser.yaml b/.goreleaser.yaml new file mode 100644 index 000000000..01de0f70c --- /dev/null +++ b/.goreleaser.yaml @@ -0,0 +1,42 @@ +project_name: "snyk-lsp" + +archives: + - format: zip + name_template: "{{ .ProjectName }}_{{ .Version}}_{{ .Os }}_{{ .Arch }}" + +before: + hooks: + - go mod tidy + +builds: + - flags: + - -trimpath + goarch: + - "386" + - amd64 + - arm64 + goos: + - darwin + - linux + - windows + ignore: + - goarch: "386" + goos: darwin + - goarch: arm64 + goos: windows + ldflags: + - -s -w -X main.gitinfo={{.Version}} + mod_timestamp: "{{ .CommitTimestamp }}" + hooks: + post: + - upx -9fv --lzma "{{ .Path }}" + +checksum: + name_template: "{{ .ProjectName }}_{{ .Version }}_SHA256SUMS" + algorithm: sha256 + +dist: build + +env: + - GO111MODULE=on + - CGO_ENABLED=0 diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 7a86d94a7..000000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,9 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml -# Editor-based HTTP Client requests -/httpRequests/ -.idea \ No newline at end of file diff --git a/code/backend_service.go b/code/backend_service.go index cb9ba6430..ebe385fae 100644 --- a/code/backend_service.go +++ b/code/backend_service.go @@ -4,12 +4,14 @@ import ( "bytes" "encoding/json" "fmt" + "io/ioutil" + "net/http" + "github.com/rs/zerolog/log" + sglsp "github.com/sourcegraph/go-lsp" + "github.com/snyk/snyk-lsp/lsp" "github.com/snyk/snyk-lsp/util" - sglsp "github.com/sourcegraph/go-lsp" - "io/ioutil" - "net/http" ) var ( diff --git a/code/backend_service_interface.go b/code/backend_service_interface.go index 02fdb9598..d2ec70fc0 100644 --- a/code/backend_service_interface.go +++ b/code/backend_service_interface.go @@ -1,8 +1,9 @@ package code import ( - "github.com/snyk/snyk-lsp/lsp" sglsp "github.com/sourcegraph/go-lsp" + + "github.com/snyk/snyk-lsp/lsp" ) type BackendService interface { diff --git a/code/backend_service_test.go b/code/backend_service_test.go index eba1bfad9..5c0028960 100644 --- a/code/backend_service_test.go +++ b/code/backend_service_test.go @@ -2,13 +2,15 @@ package code import ( "encoding/json" - "github.com/snyk/snyk-lsp/util" - sglsp "github.com/sourcegraph/go-lsp" - "github.com/stretchr/testify/assert" "net/http" "os" "testing" "time" + + sglsp "github.com/sourcegraph/go-lsp" + "github.com/stretchr/testify/assert" + + "github.com/snyk/snyk-lsp/util" ) const ( diff --git a/code/bundle.go b/code/bundle.go index 3fb2cf324..0e1c94739 100644 --- a/code/bundle.go +++ b/code/bundle.go @@ -1,13 +1,15 @@ package code import ( - "github.com/rs/zerolog/log" - "github.com/snyk/snyk-lsp/lsp" - "github.com/snyk/snyk-lsp/util" - sglsp "github.com/sourcegraph/go-lsp" "path/filepath" "sync" "time" + + "github.com/rs/zerolog/log" + sglsp "github.com/sourcegraph/go-lsp" + + "github.com/snyk/snyk-lsp/lsp" + "github.com/snyk/snyk-lsp/util" ) var ( diff --git a/code/bundle_test.go b/code/bundle_test.go index 273395ea9..93a24b52b 100644 --- a/code/bundle_test.go +++ b/code/bundle_test.go @@ -1,13 +1,15 @@ package code import ( - lsp2 "github.com/snyk/snyk-lsp/lsp" - "github.com/snyk/snyk-lsp/util" - "github.com/sourcegraph/go-lsp" - "github.com/stretchr/testify/assert" "reflect" "sync" "testing" + + "github.com/sourcegraph/go-lsp" + "github.com/stretchr/testify/assert" + + lsp2 "github.com/snyk/snyk-lsp/lsp" + "github.com/snyk/snyk-lsp/util" ) var ( diff --git a/code/fake_backend_service.go b/code/fake_backend_service.go index 73752e18a..9cb9f5903 100644 --- a/code/fake_backend_service.go +++ b/code/fake_backend_service.go @@ -2,11 +2,13 @@ package code import ( "fmt" - "github.com/snyk/snyk-lsp/lsp" - "github.com/snyk/snyk-lsp/util" - sglsp "github.com/sourcegraph/go-lsp" "math/rand" "path/filepath" + + sglsp "github.com/sourcegraph/go-lsp" + + "github.com/snyk/snyk-lsp/lsp" + "github.com/snyk/snyk-lsp/util" ) var ( @@ -27,7 +29,7 @@ var ( Code: "SNYK-123", Source: "snyk code", Message: "This is a dummy error (severity error)", - //CodeDescription: lsp.CodeDescription{Href: "https://snyk.io"}, + // CodeDescription: lsp.CodeDescription{Href: "https://snyk.io"}, } FakeCodeLens = sglsp.CodeLens{ Range: sglsp.Range{ diff --git a/diagnostics/code_lenses_test.go b/diagnostics/code_lenses_test.go index 6fc66d6c1..4cae68610 100644 --- a/diagnostics/code_lenses_test.go +++ b/diagnostics/code_lenses_test.go @@ -1,9 +1,10 @@ package diagnostics import ( + "testing" + sglsp "github.com/sourcegraph/go-lsp" "github.com/stretchr/testify/assert" - "testing" ) var ( diff --git a/diagnostics/diagnostics.go b/diagnostics/diagnostics.go index 1ef98a1f4..716fb1f92 100644 --- a/diagnostics/diagnostics.go +++ b/diagnostics/diagnostics.go @@ -1,13 +1,15 @@ package diagnostics import ( + "sync" + "github.com/rs/zerolog/log" + sglsp "github.com/sourcegraph/go-lsp" + "github.com/snyk/snyk-lsp/code" "github.com/snyk/snyk-lsp/iac" "github.com/snyk/snyk-lsp/lsp" "github.com/snyk/snyk-lsp/oss" - sglsp "github.com/sourcegraph/go-lsp" - "sync" ) var ( diff --git a/diagnostics/diagnostics_test.go b/diagnostics/diagnostics_test.go index 1ed097e34..34bbf343c 100644 --- a/diagnostics/diagnostics_test.go +++ b/diagnostics/diagnostics_test.go @@ -1,11 +1,13 @@ package diagnostics import ( - "github.com/snyk/snyk-lsp/code" - "github.com/snyk/snyk-lsp/lsp" + "testing" + sglsp "github.com/sourcegraph/go-lsp" "github.com/stretchr/testify/assert" - "testing" + + "github.com/snyk/snyk-lsp/code" + "github.com/snyk/snyk-lsp/lsp" ) var ( diff --git a/iac/iac.go b/iac/iac.go index 4d1f871ad..6377a8d4a 100644 --- a/iac/iac.go +++ b/iac/iac.go @@ -3,15 +3,17 @@ package iac import ( "encoding/json" "fmt" - "github.com/gomarkdown/markdown" - "github.com/rs/zerolog/log" - "github.com/snyk/snyk-lsp/lsp" - "github.com/snyk/snyk-lsp/util" - sglsp "github.com/sourcegraph/go-lsp" "os/exec" "path/filepath" "strings" "sync" + + "github.com/gomarkdown/markdown" + "github.com/rs/zerolog/log" + sglsp "github.com/sourcegraph/go-lsp" + + "github.com/snyk/snyk-lsp/lsp" + "github.com/snyk/snyk-lsp/util" ) var ( @@ -142,9 +144,9 @@ func convertDiagnostics(res testResult) []lsp.Diagnostic { }, Severity: lspSeverity(issue.Severity), Code: issue.PublicID, - //CodeDescription: lsp.CodeDescription{ + // CodeDescription: lsp.CodeDescription{ // Href: issue.Documentation, - //}, + // }, } diagnostics = append(diagnostics, diagnostic) } diff --git a/iac/iac_test.go b/iac/iac_test.go index a91bb1960..f4f4e6bc2 100644 --- a/iac/iac_test.go +++ b/iac/iac_test.go @@ -2,15 +2,17 @@ package iac import ( "encoding/json" - lsp2 "github.com/snyk/snyk-lsp/lsp" - "github.com/snyk/snyk-lsp/util" - "github.com/sourcegraph/go-lsp" - "github.com/stretchr/testify/assert" "os" "path/filepath" "strings" "sync" "testing" + + "github.com/sourcegraph/go-lsp" + "github.com/stretchr/testify/assert" + + lsp2 "github.com/snyk/snyk-lsp/lsp" + "github.com/snyk/snyk-lsp/util" ) func Test_HandleFile(t *testing.T) { @@ -52,7 +54,7 @@ func Test_convertCodeLenses_shouldOneCodeLensPerIssue(t *testing.T) { bytes, _ := os.ReadFile("testdata/RBAC-iac-result.json") var iacResult testResult - json.Unmarshal(bytes, &iacResult) + _ = json.Unmarshal(bytes, &iacResult) assert.NotNil(t, iacResult) assert.True(t, len(iacResult.IacIssues) > 0) diff --git a/main.go b/main.go index 9fa07fa42..2fbe1ddb8 100644 --- a/main.go +++ b/main.go @@ -4,12 +4,14 @@ import ( "bytes" "flag" "fmt" + "os" + "time" + "github.com/rs/zerolog" "github.com/rs/zerolog/log" + "github.com/snyk/snyk-lsp/server" "github.com/snyk/snyk-lsp/util" - "os" - "time" ) var gitinfo = "SNAPSHOT" // set by build via go build -ldflags "-X main.gitinfo=xxx" diff --git a/main_test.go b/main_test.go index 79f11074c..59a5fa2f7 100644 --- a/main_test.go +++ b/main_test.go @@ -1,23 +1,25 @@ package main import ( - "github.com/rs/zerolog" - "github.com/snyk/snyk-lsp/util" - "github.com/stretchr/testify/assert" "os" "strings" "testing" + + "github.com/rs/zerolog" + "github.com/stretchr/testify/assert" + + "github.com/snyk/snyk-lsp/util" ) func Test_shouldSetLogLevelViaFlag(t *testing.T) { args := []string{"snyk-lsp", "-l", "debug"} - parseFlags(args) + _, _ = parseFlags(args) assert.Equal(t, zerolog.DebugLevel, zerolog.GlobalLevel()) } func Test_shouldSetOutputFormatViaFlag(t *testing.T) { args := []string{"snyk-lsp", "-o", util.FormatHtml} - parseFlags(args) + _, _ = parseFlags(args) assert.Equal(t, util.FormatHtml, util.Format) } @@ -35,8 +37,11 @@ func Test_shouldSetLoadConfigFromFlag(t *testing.T) { if err != nil { assert.Fail(t, "Couldn't create test file") } - defer file.Close() - defer os.Remove(file.Name()) + defer func(file *os.File) { + _ = file.Close() + _ = os.Remove(file.Name()) + }(file) + _, err = file.Write([]byte("AA=Bb")) if err != nil { assert.Fail(t, "Couldn't write to test file") diff --git a/oss/oss.go b/oss/oss.go index 0f5b839ab..9418d4c1a 100644 --- a/oss/oss.go +++ b/oss/oss.go @@ -3,15 +3,17 @@ package oss import ( "encoding/json" "fmt" - "github.com/gomarkdown/markdown" - "github.com/rs/zerolog/log" - "github.com/snyk/snyk-lsp/lsp" - "github.com/snyk/snyk-lsp/util" - sglsp "github.com/sourcegraph/go-lsp" "os/exec" "path/filepath" "strings" "sync" + + "github.com/gomarkdown/markdown" + "github.com/rs/zerolog/log" + sglsp "github.com/sourcegraph/go-lsp" + + "github.com/snyk/snyk-lsp/lsp" + "github.com/snyk/snyk-lsp/util" ) var ( @@ -123,9 +125,9 @@ func callSnykCLI(doc sglsp.TextDocumentItem) ([]lsp.Diagnostic, error) { Severity: lspSeverity(issue.Severity), Code: issue.Id, // Don't use it for now as it's not widely supported - //CodeDescription: lsp.CodeDescription{ + // CodeDescription: lsp.CodeDescription{ // Href: issue.References[0].Url, - //}, + // }, } diagnostics = append(diagnostics, diagnostic) } diff --git a/oss/oss_test.go b/oss/oss_test.go index 82b2f4a02..f865163f9 100644 --- a/oss/oss_test.go +++ b/oss/oss_test.go @@ -1,15 +1,17 @@ package oss import ( - lsp2 "github.com/snyk/snyk-lsp/lsp" - "github.com/snyk/snyk-lsp/util" - "github.com/sourcegraph/go-lsp" - "github.com/stretchr/testify/assert" "os" "path/filepath" "strings" "sync" "testing" + + "github.com/sourcegraph/go-lsp" + "github.com/stretchr/testify/assert" + + lsp2 "github.com/snyk/snyk-lsp/lsp" + "github.com/snyk/snyk-lsp/util" ) func Test_HandleFile(t *testing.T) { diff --git a/server/server.go b/server/server.go index a64fb10dd..d9d8a7ddc 100644 --- a/server/server.go +++ b/server/server.go @@ -2,15 +2,17 @@ package server import ( "context" + "os" + "github.com/creachadair/jrpc2" "github.com/creachadair/jrpc2/channel" "github.com/creachadair/jrpc2/handler" "github.com/rs/zerolog/log" + sglsp "github.com/sourcegraph/go-lsp" + "github.com/snyk/snyk-lsp/code" "github.com/snyk/snyk-lsp/diagnostics" "github.com/snyk/snyk-lsp/lsp" - sglsp "github.com/sourcegraph/go-lsp" - "os" ) var ( @@ -32,7 +34,7 @@ func Start() { "shutdown": Shutdown(), "exit": Exit(&srv), "textDocument/codeLens": TextDocumentCodeLens(), - //"codeLens/resolve": codeLensResolve(&server), + // "codeLens/resolve": codeLensResolve(&server), } srv = jrpc2.NewServer(lspHandlers, &jrpc2.ServerOptions{ diff --git a/server/server_test.go b/server/server_test.go index 4f1b6b861..671090389 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -3,17 +3,19 @@ package server import ( "context" "fmt" + "strings" + "testing" + "time" + "github.com/creachadair/jrpc2" "github.com/creachadair/jrpc2/handler" "github.com/creachadair/jrpc2/server" "github.com/rs/zerolog" "github.com/rs/zerolog/log" - "github.com/snyk/snyk-lsp/code" "github.com/sourcegraph/go-lsp" "github.com/stretchr/testify/assert" - "strings" - "testing" - "time" + + "github.com/snyk/snyk-lsp/code" ) var ( @@ -65,7 +67,7 @@ func startServer() server.Local { "shutdown": Shutdown(), "exit": Exit(&srv), "textDocument/codeLens": TextDocumentCodeLens(), - //"codeLens/resolve": codeLensResolve(&server), + // "codeLens/resolve": codeLensResolve(&server), } opts := &server.LocalOptions{ @@ -85,7 +87,10 @@ func startServer() server.Local { func Test_serverShouldStart(t *testing.T) { loc := startServer() - defer loc.Close() + // TODO(pavel): extract to setup/teardown methods + defer func(loc server.Local) { + _ = loc.Close() + }(loc) si := loc.Server.ServerInfo() @@ -94,7 +99,9 @@ func Test_serverShouldStart(t *testing.T) { func Test_dummy_shouldNotBeServed(t *testing.T) { loc := startServer() - defer loc.Close() + defer func(loc server.Local) { + _ = loc.Close() + }(loc) _, err := loc.Client.Call(ctx, "dummy", nil) if err == nil { @@ -104,7 +111,9 @@ func Test_dummy_shouldNotBeServed(t *testing.T) { func Test_initialize_shouldBeServed(t *testing.T) { loc := startServer() - defer loc.Close() + defer func(loc server.Local) { + _ = loc.Close() + }(loc) rsp, err := loc.Client.Call(ctx, "initialize", nil) if err != nil { @@ -118,7 +127,9 @@ func Test_initialize_shouldBeServed(t *testing.T) { func Test_initialize_shouldSupportDocumentOpening(t *testing.T) { loc := startServer() - defer loc.Close() + defer func(loc server.Local) { + _ = loc.Close() + }(loc) rsp, err := loc.Client.Call(ctx, "initialize", nil) if err != nil { @@ -133,7 +144,9 @@ func Test_initialize_shouldSupportDocumentOpening(t *testing.T) { func Test_initialize_shouldSupportDocumentChanges(t *testing.T) { loc := startServer() - defer loc.Close() + defer func(loc server.Local) { + _ = loc.Close() + }(loc) rsp, err := loc.Client.Call(ctx, "initialize", nil) if err != nil { @@ -148,7 +161,9 @@ func Test_initialize_shouldSupportDocumentChanges(t *testing.T) { func Test_initialize_shouldSupportDocumentSaving(t *testing.T) { loc := startServer() - defer loc.Close() + defer func(loc server.Local) { + _ = loc.Close() + }(loc) rsp, err := loc.Client.Call(ctx, "initialize", nil) if err != nil { @@ -165,7 +180,9 @@ func Test_initialize_shouldSupportDocumentSaving(t *testing.T) { func Test_initialize_shouldSupportCodeLens(t *testing.T) { loc := startServer() - defer loc.Close() + defer func(loc server.Local) { + _ = loc.Close() + }(loc) rsp, err := loc.Client.Call(ctx, "initialize", nil) if err != nil { @@ -180,7 +197,9 @@ func Test_initialize_shouldSupportCodeLens(t *testing.T) { func Test_textDocumentDidOpenHandler_shouldAcceptDocumentItemAndPublishDiagnostics(t *testing.T) { loc := startServer() - defer loc.Close() + defer func(loc server.Local) { + _ = loc.Close() + }(loc) didOpenParams := didOpenTextParams() @@ -194,13 +213,15 @@ func Test_textDocumentDidOpenHandler_shouldAcceptDocumentItemAndPublishDiagnosti // wait for publish assert.Eventually(t, func() bool { return notification != nil }, 5*time.Second, 10*time.Millisecond) - notification.UnmarshalParams(&diagnostics) + _ = notification.UnmarshalParams(&diagnostics) assert.Equal(t, didOpenParams.TextDocument.URI, diagnostics.URI) } func Test_textDocumentDidChangeHandler_shouldAcceptUri(t *testing.T) { loc := startServer() - defer loc.Close() + defer func(loc server.Local) { + _ = loc.Close() + }(loc) // register our dummy document didOpenParams := didOpenTextParams() @@ -222,7 +243,9 @@ func Test_textDocumentDidChangeHandler_shouldAcceptUri(t *testing.T) { func Test_textDocumentDidSaveHandler_shouldAcceptDocumentItemAndPublishDiagnostics(t *testing.T) { loc := startServer() - defer loc.Close() + defer func(loc server.Local) { + _ = loc.Close() + }(loc) didSaveParams := didSaveTextParams() @@ -236,13 +259,15 @@ func Test_textDocumentDidSaveHandler_shouldAcceptDocumentItemAndPublishDiagnosti // wait for publish assert.Eventually(t, func() bool { return notification != nil }, 5*time.Second, 10*time.Millisecond) - notification.UnmarshalParams(&diagnostics) + _ = notification.UnmarshalParams(&diagnostics) assert.Equal(t, didSaveParams.TextDocument.URI, diagnostics.URI) } func Test_textDocumentWillSaveWaitUntilHandler_shouldBeServed(t *testing.T) { loc := startServer() - defer loc.Close() + defer func(loc server.Local) { + _ = loc.Close() + }(loc) _, err := loc.Client.Call(ctx, "textDocument/willSaveWaitUntil", nil) if err != nil { @@ -252,7 +277,9 @@ func Test_textDocumentWillSaveWaitUntilHandler_shouldBeServed(t *testing.T) { func Test_textDocumentWillSaveHandler_shouldBeServed(t *testing.T) { loc := startServer() - defer loc.Close() + defer func(loc server.Local) { + _ = loc.Close() + }(loc) _, err := loc.Client.Call(ctx, "textDocument/willSave", nil) if err != nil { @@ -262,7 +289,9 @@ func Test_textDocumentWillSaveHandler_shouldBeServed(t *testing.T) { func Test_textDocumentCodeLens_shouldReturnCodeLenses(t *testing.T) { loc := startServer() - defer loc.Close() + defer func(loc server.Local) { + _ = loc.Close() + }(loc) codeLensParams := lsp.CodeLensParams{ TextDocument: docIdentifier.TextDocumentIdentifier, @@ -279,10 +308,10 @@ func Test_textDocumentCodeLens_shouldReturnCodeLenses(t *testing.T) { } var codeLenses []lsp.CodeLens - rsp.UnmarshalResult(&codeLenses) + _ = rsp.UnmarshalResult(&codeLenses) assert.Equal(t, 1, len(codeLenses)) } -//func Test_codeLensResolve_shouldResolve(t *testing.T) { +// func Test_codeLensResolve_shouldResolve(t *testing.T) { // assert.Fail(t, "Not implemented yet") -//} +// } diff --git a/util/config.go b/util/config.go index d78b1d5a7..a6af3406f 100644 --- a/util/config.go +++ b/util/config.go @@ -1,12 +1,13 @@ package util import ( - "github.com/rs/zerolog/log" - "github.com/subosito/gotenv" "os" "os/exec" "runtime" "strings" + + "github.com/rs/zerolog/log" + "github.com/subosito/gotenv" ) const ( diff --git a/util/config_test.go b/util/config_test.go index a0f3a43ee..044b235b0 100644 --- a/util/config_test.go +++ b/util/config_test.go @@ -1,11 +1,12 @@ package util import ( - "github.com/stretchr/testify/assert" "os" "path/filepath" "runtime" "testing" + + "github.com/stretchr/testify/assert" ) func init() { @@ -14,7 +15,7 @@ func init() { func TestToken(t *testing.T) { os.Clearenv() - os.Setenv(snykTokenKey, "test") + _ = os.Setenv(snykTokenKey, "test") assert.NotEqual(t, "", Token()) } @@ -25,7 +26,10 @@ func Test_addSnykCliPathToEnv_should_find_cli_and_add_path_to_env(t *testing.T) if err != nil { assert.Fail(t, "Couldn't create test directory") } - defer os.RemoveAll(temp) + defer func(path string) { + _ = os.RemoveAll(path) + }(temp) + cliFile := createDummyCliFile(t, temp) err = os.Setenv("PATH", temp) if err != nil { @@ -47,7 +51,10 @@ func Test_addSnykCliPathToEnv_should_respect_cli_path_in_env(t *testing.T) { if err != nil { assert.Fail(t, "Couldn't create test directory") } - defer os.RemoveAll(temp) + defer func(path string) { + _ = os.RemoveAll(path) + }(temp) + createDummyCliFile(t, temp) err = os.Setenv("PATH", temp) if err != nil { @@ -77,13 +84,13 @@ func createDummyCliFile(t *testing.T, temp string) string { if err != nil { assert.Fail(t, "Can't write dummy data to cli file") } - cliFile.Close() + _ = cliFile.Close() return cliFile.Name() } func Test_updatePath(t *testing.T) { os.Clearenv() - os.Setenv("PATH", "a") + _ = os.Setenv("PATH", "a") updatePath("b") assert.Equal(t, "a"+string(os.PathListSeparator)+"b", os.Getenv("PATH")) } @@ -95,12 +102,14 @@ func Test_loadFile(t *testing.T) { if err != nil { assert.Fail(t, "Couldn't create temp file", err) } - defer file.Close() - defer os.Remove(file.Name()) + defer func(file *os.File) { + _ = file.Close() + _ = os.Remove(file.Name()) + }(file) if err != nil { assert.Fail(t, "Couldn't create test file") } - file.Write(envData) + _, _ = file.Write(envData) if err != nil { assert.Fail(t, "Couldn't write to test file") } diff --git a/util/util_test.go b/util/util_test.go index 0436b77a6..510a43515 100644 --- a/util/util_test.go +++ b/util/util_test.go @@ -1,8 +1,9 @@ package util import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func Test_hash(t *testing.T) {