Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ repos:
name: lint-fix
entry: make lint-fix
language: system
stages: [ pre-commit, pre-push ]
stages: [ pre-push ]
verbose: true
pass_filenames: false
- id: tests
name: unit tests
entry: go test ./...
entry: go test -failfast ./...
language: system
stages: [ pre-commit, pre-push ]
stages: [ pre-push ]
verbose: true
pass_filenames: false
- repo: https://github.com/gitleaks/gitleaks
Expand Down
10 changes: 7 additions & 3 deletions application/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/denisbrodbeck/machineid"
"github.com/google/uuid"
"github.com/rs/zerolog"
"github.com/snyk/cli-extension-os-flows/pkg/osflows"
"github.com/snyk/go-application-framework/pkg/app"
"github.com/snyk/go-application-framework/pkg/auth"
"github.com/snyk/go-application-framework/pkg/configuration"
Expand Down Expand Up @@ -294,9 +295,7 @@ func initWorkFlowEngine(c *Config) {
c.m.Lock()
defer c.m.Unlock()

conf := configuration.NewWithOpts(
configuration.WithAutomaticEnv(),
)
conf := configuration.NewWithOpts(configuration.WithAutomaticEnv())

conf.PersistInStorage(storedConfig.ConfigMainKey)
conf.Set(cli_constants.EXECUTION_MODE_KEY, cli_constants.EXECUTION_MODE_VALUE_STANDALONE)
Expand Down Expand Up @@ -336,6 +335,11 @@ func initWorkflows(c *Config) error {
if err != nil {
return err
}

err = osflows.Init(c.engine)
if err != nil {
return err
}
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion application/server/authentication_smoke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func getDummyOAuth2Token(expiry time.Time) oauth2.Token {

func checkInvalidCredentialsMessageRequest(t *testing.T, expected string, tokenString string) {
t.Helper()
c := testutil.SmokeTest(t, false)
c := testutil.SmokeTest(t, "")
srv, jsonRpcRecorder := setupServer(t, c)

c.SetSnykIacEnabled(false)
Expand Down
2 changes: 1 addition & 1 deletion application/server/parallelization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (
)

func Test_Concurrent_CLI_Runs(t *testing.T) {
c := testutil.SmokeTest(t, false)
c := testutil.SmokeTest(t, "")
srv, jsonRPCRecorder := setupServer(t, c)
c.SetSnykIacEnabled(false)
c.SetSnykOssEnabled(true)
Expand Down
129 changes: 92 additions & 37 deletions application/server/server_smoke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package server
import (
"encoding/json"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
Expand All @@ -28,6 +29,8 @@ import (

"github.com/google/uuid"
"github.com/samber/lo"
"github.com/snyk/go-application-framework/pkg/workflow"
"github.com/spf13/pflag"

"github.com/snyk/snyk-ls/internal/util"

Expand Down Expand Up @@ -59,7 +62,7 @@ import (
)

func Test_SmokeInstanceTest(t *testing.T) {
c := testutil.SmokeTest(t, false)
c := testutil.SmokeTest(t, "")
ossFile := "package.json"
codeFile := "app.js"
testutil.CreateDummyProgressListener(t)
Expand Down Expand Up @@ -149,7 +152,11 @@ func Test_SmokeWorkspaceScan(t *testing.T) {
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
c := testutil.SmokeTest(t, false)
secretTokenName := ""
if tc.useConsistentIgnores {
secretTokenName = "SNYK_TOKEN_CONSISTENT_IGNORES"
}
c := testutil.SmokeTest(t, secretTokenName)
runSmokeTest(t, c, tc.repo, tc.commit, tc.file1, tc.file2, tc.hasVulns, "")
})
}
Expand All @@ -158,7 +165,7 @@ func Test_SmokeWorkspaceScan(t *testing.T) {
func Test_SmokePreScanCommand(t *testing.T) {
t.Run("executes pre scan command if configured", func(t *testing.T) {
testsupport.NotOnWindows(t, "we can enable windows if we have the correct error message")
c := testutil.SmokeTest(t, false)
c := testutil.SmokeTest(t, "")
loc, jsonRpcRecorder := setupServer(t, c)
c.EnableSnykCodeSecurity(false)
c.SetSnykOssEnabled(true)
Expand Down Expand Up @@ -207,7 +214,7 @@ func Test_SmokePreScanCommand(t *testing.T) {
func Test_SmokeIssueCaching(t *testing.T) {
testsupport.NotOnWindows(t, "git clone does not work here. dunno why. ") // FIXME
t.Run("adds issues to cache correctly", func(t *testing.T) {
c := testutil.SmokeTest(t, false)
c := testutil.SmokeTest(t, "")
loc, jsonRPCRecorder := setupServer(t, c)
c.EnableSnykCodeSecurity(true)
c.SetSnykOssEnabled(true)
Expand Down Expand Up @@ -282,7 +289,7 @@ func Test_SmokeIssueCaching(t *testing.T) {
})

t.Run("clears issues from cache correctly", func(t *testing.T) {
c := testutil.SmokeTest(t, false)
c := testutil.SmokeTest(t, "")
loc, jsonRPCRecorder := setupServer(t, c)
c.EnableSnykCodeSecurity(true)
c.SetSnykOssEnabled(true)
Expand Down Expand Up @@ -349,7 +356,7 @@ func Test_SmokeIssueCaching(t *testing.T) {
}

func Test_SmokeExecuteCLICommand(t *testing.T) {
c := testutil.SmokeTest(t, false)
c := testutil.SmokeTest(t, "")
loc, _ := setupServer(t, c)
c.EnableSnykCodeSecurity(false)
c.SetSnykIacEnabled(false)
Expand Down Expand Up @@ -494,36 +501,21 @@ func runSmokeTest(t *testing.T, c *config.Config, repo string, commit string, fi

cloneTargetDir := setupRepoAndInitialize(t, repo, commit, loc, c)
cloneTargetDirString := (string)(cloneTargetDir)

waitForScan(t, cloneTargetDirString, c)

notifications := jsonRPCRecorder.FindNotificationsByMethod("$/snyk.folderConfigs")
assert.Greater(t, len(notifications), 0)

foundFolderConfig := false
for _, notification := range notifications {
var folderConfigsParam types.FolderConfigsParam
err := notification.UnmarshalParams(&folderConfigsParam)
require.NoError(t, err)

for _, folderConfig := range folderConfigsParam.FolderConfigs {
assert.NotEmpty(t, folderConfigsParam.FolderConfigs[0].BaseBranch)
assert.NotEmpty(t, folderConfigsParam.FolderConfigs[0].LocalBranches)
assert.Eventuallyf(t, func() bool {
return receivedFolderConfigNotification(t, notifications, cloneTargetDir)
}, time.Second*5, time.Second, "did not receive folder configs")

// Normalize both paths for comparison since folder config paths are now normalized
normalizedCloneTargetDir := util.PathKey(cloneTargetDir)
if folderConfig.FolderPath == normalizedCloneTargetDir {
foundFolderConfig = true
break
}
}

if foundFolderConfig {
break
}
}
assert.Truef(t, foundFolderConfig, "could not find folder config for %s", cloneTargetDirString)
jsonRPCRecorder.ClearNotifications()
var testPath types.FilePath

// ------------------------------------------------------
// check snyk open source diagnostics (file1)
// ------------------------------------------------------
if file1 != "" {
testPath = types.FilePath(filepath.Join(cloneTargetDirString, file1))
waitForNetwork(c)
Expand All @@ -533,6 +525,10 @@ func runSmokeTest(t *testing.T, c *config.Config, repo string, commit string, fi
}

jsonRPCRecorder.ClearNotifications()

// ------------------------------------------------------
// check snyk code diagnostics (file2)
// ------------------------------------------------------
testPath = types.FilePath(filepath.Join(cloneTargetDirString, file2))
waitForNetwork(c)
textDocumentDidSave(t, &loc, testPath)
Expand All @@ -557,6 +553,65 @@ func runSmokeTest(t *testing.T, c *config.Config, repo string, commit string, fi
waitForDeltaScan(t, di.ScanStateAggregator())
}

func receivedFolderConfigNotification(t *testing.T, notifications []jrpc2.Request, cloneTargetDir types.FilePath) bool {
t.Helper()
foundFolderConfig := false
for _, notification := range notifications {
var folderConfigsParam types.FolderConfigsParam
err := notification.UnmarshalParams(&folderConfigsParam)
require.NoError(t, err)

for _, folderConfig := range folderConfigsParam.FolderConfigs {
assert.NotEmpty(t, folderConfigsParam.FolderConfigs[0].BaseBranch)
assert.NotEmpty(t, folderConfigsParam.FolderConfigs[0].LocalBranches)

// Normalize both paths for comparison since folder config paths are now normalized
normalizedCloneTargetDir := util.PathKey(cloneTargetDir)
if folderConfig.FolderPath == normalizedCloneTargetDir {
foundFolderConfig = true
break
}
}

if foundFolderConfig {
break
}
}
return foundFolderConfig
}

var (
// now register it with the engine
depGraphWorkFlowID = workflow.NewWorkflowIdentifier("depgraph")
depGraphDataID = workflow.NewTypeIdentifier(depGraphWorkFlowID, "depgraph")
)

// substituteDepGraphFlow generate depgraph. necessary, as depgraph workflow needs legacycli workflow which
// does not work without the TypeScript CLI
func substituteDepGraphFlow(t *testing.T, c *config.Config, cloneTargetDirString, displayTargetFile string) {
t.Helper()

flagset := workflow.ConfigurationOptionsFromFlagset(pflag.NewFlagSet("", pflag.ContinueOnError))
callback := func(invocation workflow.InvocationContext, workflowInputData []workflow.Data) ([]workflow.Data, error) {
cmd := exec.CommandContext(t.Context(), c.CliSettings().Path(), "depgraph")
cmd.Dir = cloneTargetDirString
cmd.Env = os.Environ()
depGraphJson, err := cmd.Output()
if err != nil {
t.Fatalf("couldn't retrieve the depgraph %s: ", err.Error())
}
depGraphData := workflow.NewData(depGraphDataID, "application/json", depGraphJson)
normalisedTargetFile := strings.TrimSpace(displayTargetFile)
depGraphData.SetMetaData("Content-Location", normalisedTargetFile)
depGraphData.SetMetaData("normalisedTargetFile", normalisedTargetFile) //Required for cli-extension-os-flow

return []workflow.Data{depGraphData}, nil
}

_, err := c.Engine().Register(depGraphWorkFlowID, flagset, callback)
require.NoError(t, err)
}

func waitForNetwork(c *config.Config) {
for c.Offline() {
time.Sleep(5 * time.Second)
Expand Down Expand Up @@ -841,7 +896,7 @@ func checkFeatureFlagStatus(t *testing.T, c *config.Config, loc *server.Local) {
}

func Test_SmokeSnykCodeFileScan(t *testing.T) {
c := testutil.SmokeTest(t, false)
c := testutil.SmokeTest(t, "")
loc, jsonRPCRecorder := setupServer(t, c)
c.SetSnykCodeEnabled(true)
cleanupChannels()
Expand Down Expand Up @@ -914,7 +969,7 @@ func Test_SmokeUncFilePath(t *testing.T) {
}

func Test_SmokeSnykCodeDelta_NewVulns(t *testing.T) {
c := testutil.SmokeTest(t, false)
c := testutil.SmokeTest(t, "")
loc, jsonRPCRecorder := setupServer(t, c)
c.SetSnykCodeEnabled(true)
c.SetSnykOssEnabled(false)
Expand Down Expand Up @@ -961,7 +1016,7 @@ func Test_SmokeSnykCodeDelta_NewVulns(t *testing.T) {
}

func Test_SmokeSnykCodeDelta_NoNewIssuesFound(t *testing.T) {
c := testutil.SmokeTest(t, false)
c := testutil.SmokeTest(t, "")
loc, jsonRPCRecorder := setupServer(t, c)
c.SetSnykCodeEnabled(true)
c.SetDeltaFindingsEnabled(true)
Expand Down Expand Up @@ -991,7 +1046,7 @@ func Test_SmokeSnykCodeDelta_NoNewIssuesFound(t *testing.T) {
}

func Test_SmokeSnykCodeDelta_NoNewIssuesFound_JavaGoof(t *testing.T) {
c := testutil.SmokeTest(t, false)
c := testutil.SmokeTest(t, "")
loc, jsonRPCRecorder := setupServer(t, c)
c.SetSnykCodeEnabled(true)
c.SetDeltaFindingsEnabled(true)
Expand Down Expand Up @@ -1019,7 +1074,7 @@ func Test_SmokeSnykCodeDelta_NoNewIssuesFound_JavaGoof(t *testing.T) {

func Test_SmokeScanUnmanaged(t *testing.T) {
testsupport.NotOnWindows(t, "git clone does not work here. dunno why. ") // FIXME
c := testutil.SmokeTest(t, false)
c := testutil.SmokeTest(t, "")
loc, jsonRPCRecorder := setupServer(t, c)
c.SetSnykIacEnabled(false)
cleanupChannels()
Expand Down Expand Up @@ -1081,7 +1136,7 @@ func requireFolderConfigNotification(t *testing.T, jsonRpcRecorder *testsupport.
func Test_SmokeOrgSelection(t *testing.T) {
setupOrgSelectionTest := func(t *testing.T) (*config.Config, server.Local, *testsupport.JsonRPCRecorder, types.FilePath, types.InitializeParams) {
t.Helper()
c := testutil.SmokeTest(t, false)
c := testutil.SmokeTest(t, "")
loc, jsonRpcRecorder := setupServer(t, c)
c.EnableSnykCodeSecurity(false)
c.SetSnykOssEnabled(true)
Expand Down Expand Up @@ -1504,9 +1559,9 @@ func ensureInitialized(t *testing.T, c *config.Config, loc server.Local, initPar
t.Helper()
t.Setenv("SNYK_LOG_LEVEL", "debug")
c.SetLogLevel(zerolog.LevelDebugValue)
c.ConfigureLogging(nil)
c.ConfigureLogging(loc.Server)
gafConfig := c.Engine().GetConfiguration()
gafConfig.Set(configuration.DEBUG, false)
gafConfig.Set(configuration.DEBUG, c.Logger().GetLevel() == zerolog.DebugLevel)

documentURI := initParams.WorkspaceFolders[0].Uri
commitHash := getCurrentCommitHash(t, uri.PathFromUri(documentURI))
Expand Down
Loading
Loading