-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
173 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* © 2024 Snyk Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package command | ||
|
||
import ( | ||
"context" | ||
"github.com/rs/zerolog" | ||
"github.com/snyk/snyk-ls/application/config" | ||
"github.com/snyk/snyk-ls/domain/ide/workspace" | ||
"github.com/snyk/snyk-ls/internal/types" | ||
"github.com/sourcegraph/go-lsp" | ||
"net/url" | ||
) | ||
|
||
type clearCache struct { | ||
command types.CommandData | ||
} | ||
|
||
func (cmd *clearCache) Command() types.CommandData { | ||
return cmd.command | ||
} | ||
|
||
// Execute Deletes persisted, inMemory Cache or both. | ||
// Parameters: folderUri either folder Uri or empty for all folders | ||
// cacheType: either inMemory or persisted or empty for both. | ||
func (cmd *clearCache) Execute(_ context.Context) (any, error) { | ||
logger := config.CurrentConfig().Logger().With().Str("method", "clearCache.Execute").Logger() | ||
args := cmd.command.Arguments | ||
var parsedFolderUri *lsp.DocumentURI | ||
folderURI := args[0].(string) | ||
|
||
if folderURI != "" { | ||
decodedPath, err := url.PathUnescape(folderURI) | ||
if err != nil { | ||
logger.Error().Err(err).Msgf("could not decode folder Uri %s", folderURI) | ||
return nil, err | ||
} | ||
uri := lsp.DocumentURI(decodedPath) | ||
parsedFolderUri = &uri | ||
} | ||
|
||
cacheType := args[1].(string) | ||
|
||
if cacheType == "" { | ||
cmd.purgeInMemoryCache(&logger, parsedFolderUri) | ||
cmd.purgePersistedCache(&logger, parsedFolderUri) | ||
} else if cacheType == "inMemory" { | ||
cmd.purgeInMemoryCache(&logger, parsedFolderUri) | ||
} else if cacheType == "persisted" { | ||
cmd.purgePersistedCache(&logger, parsedFolderUri) | ||
} | ||
|
||
return nil, nil | ||
} | ||
|
||
func (cmd *clearCache) purgeInMemoryCache(logger *zerolog.Logger, folderUri *lsp.DocumentURI) { | ||
ws := workspace.Get() | ||
for _, folder := range ws.Folders() { | ||
if folderUri != nil && *folderUri != folder.Uri() { | ||
continue | ||
} | ||
logger.Info().Msgf("deleting in-memory cache for folder %s", folder.Path()) | ||
folder.Clear() | ||
} | ||
} | ||
|
||
func (cmd *clearCache) purgePersistedCache(logger *zerolog.Logger, folderUri *lsp.DocumentURI) { | ||
var folderList []string | ||
ws := workspace.Get() | ||
scanPersister := ws.ScanPersister() | ||
if scanPersister == nil { | ||
logger.Error().Msgf("could not find scan persister") | ||
return | ||
} | ||
for _, folder := range ws.Folders() { | ||
if folderUri != nil && *folderUri != folder.Uri() { | ||
continue | ||
} | ||
folderList = append(folderList, folder.Path()) | ||
} | ||
logger.Info().Msgf("deleting perrsisted cache for folders %v", folderList) | ||
scanPersister.Clear(folderList, false) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* © 2024 Snyk Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package command | ||
|
||
import ( | ||
"context" | ||
"github.com/snyk/snyk-ls/domain/ide/workspace" | ||
"github.com/snyk/snyk-ls/domain/snyk/persistence" | ||
"github.com/snyk/snyk-ls/domain/snyk/scanner" | ||
"github.com/snyk/snyk-ls/internal/notification" | ||
"github.com/snyk/snyk-ls/internal/observability/performance" | ||
"github.com/snyk/snyk-ls/internal/testutil" | ||
"github.com/snyk/snyk-ls/internal/types" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func Test_ClearCache_DeleteAll_NoError(t *testing.T) { | ||
c := testutil.UnitTest(t) | ||
|
||
// Arrange | ||
sc := &scanner.TestScanner{} | ||
scanNotifier := scanner.NewMockScanNotifier() | ||
scanPersister := persistence.NewGitPersistenceProvider(c.Logger()) | ||
w := workspace.New(c, performance.NewInstrumentor(), sc, nil, scanNotifier, notification.NewMockNotifier(), scanPersister) | ||
folder := workspace.NewFolder(c, "dummy", "dummy", sc, nil, scanNotifier, notification.NewMockNotifier(), scanPersister) | ||
w.AddFolder(folder) | ||
workspace.Set(w) | ||
|
||
clearCacheCommand := setupClearCacheCommand(t, "", "") | ||
|
||
// Execute the command | ||
_, err := clearCacheCommand.Execute(context.Background()) | ||
|
||
// Assert | ||
assert.NoError(t, err) | ||
} | ||
|
||
func setupClearCacheCommand(t *testing.T, folderUri, cacheType string) clearCache { | ||
t.Helper() | ||
clearCacheCmd := clearCache{ | ||
command: types.CommandData{Arguments: []interface{}{folderUri, cacheType}}, | ||
} | ||
return clearCacheCmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters