From 3c72dcccca35a404dc4a9a7f73e8f625c71514f0 Mon Sep 17 00:00:00 2001 From: sburuiana-snyk Date: Thu, 15 May 2025 11:56:44 +0000 Subject: [PATCH 1/2] feat: add method to create batch from raw content --- bundle/bundle.go | 18 ++++++++++++++++++ bundle/bundle_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/bundle/bundle.go b/bundle/bundle.go index b5f4681..a516e34 100644 --- a/bundle/bundle.go +++ b/bundle/bundle.go @@ -18,6 +18,7 @@ package bundle import ( "context" + "fmt" "github.com/rs/zerolog" @@ -134,6 +135,23 @@ func NewBatch(documents map[string]deepcode.BundleFile) *Batch { } } +func NewBatchFromRawContent(documents map[string][]byte) (*Batch, error) { + bundleFiles := make(map[string]deepcode.BundleFile) + + for key, rawData := range documents { + bundleFile, err := deepcode.BundleFileFrom(rawData) + if err != nil { + return nil, fmt.Errorf("failed to create file from raw data: %v", err) + } + + bundleFiles[key] = bundleFile + } + + return &Batch{ + documents: bundleFiles, + }, nil +} + // todo simplify the size computation // maybe consider an addFile / canFitFile interface with proper error handling func (b *Batch) canFitFile(uri string, content []byte) bool { diff --git a/bundle/bundle_test.go b/bundle/bundle_test.go index 8ea1e03..7f9f49e 100644 --- a/bundle/bundle_test.go +++ b/bundle/bundle_test.go @@ -20,6 +20,7 @@ import ( "context" "crypto/sha256" "encoding/hex" + "fmt" "os" "testing" @@ -39,6 +40,29 @@ var bundleWithMultipleFiles = bundle.NewBatch(map[string]deepcode.BundleFile{ "file": {}, "another": {}, }) +var bundleFromRawContent, batchErr = bundle.NewBatchFromRawContent(map[string][]byte{"hello": []byte("world")}) + +// Matcher for BundleFile that matches on key and content (ignores hash) +type bundleFilePartialMatcher struct { + expectedKey string + expectedContent string +} + +func (m bundleFilePartialMatcher) Matches(x interface{}) bool { + files, ok := x.(map[string]deepcode.BundleFile) + if !ok { + return false + } + file, exists := files[m.expectedKey] + if !exists { + return false + } + return file.Content == m.expectedContent +} + +func (m bundleFilePartialMatcher) String() string { + return fmt.Sprintf("{ Key : '%s', Content : '%s' }", m.expectedKey, m.expectedContent) +} func Test_UploadBatch(t *testing.T) { testLogger := zerolog.Nop() @@ -89,6 +113,7 @@ func Test_UploadBatch(t *testing.T) { mockSnykCodeClient.EXPECT().ExtendBundle(gomock.Any(), "testBundleHash", map[string]deepcode.BundleFile{ "file": {}, }, []string{}).Return("testBundleHash", []string{}, nil).Times(1) + mockSnykCodeClient.EXPECT().ExtendBundle(gomock.Any(), "bundleWithMultipleFilesHash", bundleFilePartialMatcher{expectedKey: "hello", expectedContent: "world"}, []string{}).Return("bundleWithAllFilesHash", []string{}, nil).Times(1) mockSpan := mocks.NewMockSpan(ctrl) mockSpan.EXPECT().Context().AnyTimes() @@ -105,6 +130,11 @@ func Test_UploadBatch(t *testing.T) { require.NoError(t, err) newHash := b.GetBundleHash() assert.NotEqual(t, oldHash, newHash) + require.NoError(t, batchErr) + err = b.UploadBatch(context.Background(), "testRequestId", bundleFromRawContent) + require.NoError(t, err) + newestHash := b.GetBundleHash() + assert.NotEqual(t, newHash, newestHash) }) } From b75803de0726539291b9b7bb6374033caabc0bbb Mon Sep 17 00:00:00 2001 From: sburuiana-snyk Date: Mon, 19 May 2025 07:56:54 +0000 Subject: [PATCH 2/2] chore: move test to a separate test --- bundle/bundle_test.go | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/bundle/bundle_test.go b/bundle/bundle_test.go index 7f9f49e..b50cfcc 100644 --- a/bundle/bundle_test.go +++ b/bundle/bundle_test.go @@ -113,7 +113,6 @@ func Test_UploadBatch(t *testing.T) { mockSnykCodeClient.EXPECT().ExtendBundle(gomock.Any(), "testBundleHash", map[string]deepcode.BundleFile{ "file": {}, }, []string{}).Return("testBundleHash", []string{}, nil).Times(1) - mockSnykCodeClient.EXPECT().ExtendBundle(gomock.Any(), "bundleWithMultipleFilesHash", bundleFilePartialMatcher{expectedKey: "hello", expectedContent: "world"}, []string{}).Return("bundleWithAllFilesHash", []string{}, nil).Times(1) mockSpan := mocks.NewMockSpan(ctrl) mockSpan.EXPECT().Context().AnyTimes() @@ -130,11 +129,31 @@ func Test_UploadBatch(t *testing.T) { require.NoError(t, err) newHash := b.GetBundleHash() assert.NotEqual(t, oldHash, newHash) + }) +} + +func Test_RawContentBatch(t *testing.T) { + testLogger := zerolog.Nop() + + t.Run("create a batch from raw content and upload the bundle", func(t *testing.T) { + ctrl := gomock.NewController(t) + mockSnykCodeClient := deepcodeMocks.NewMockDeepcodeClient(ctrl) + mockSnykCodeClient.EXPECT().ExtendBundle(gomock.Any(), "testBundleHash", bundleFilePartialMatcher{expectedKey: "hello", expectedContent: "world"}, []string{}).Return("newBundleHash", []string{}, nil).Times(1) + + mockSpan := mocks.NewMockSpan(ctrl) + mockSpan.EXPECT().Context().AnyTimes() + mockInstrumentor := mocks.NewMockInstrumentor(ctrl) + mockInstrumentor.EXPECT().StartSpan(gomock.Any(), gomock.Any()).Return(mockSpan).AnyTimes() + mockInstrumentor.EXPECT().Finish(gomock.Any()).AnyTimes() + mockErrorReporter := mocks.NewMockErrorReporter(ctrl) + b := bundle.NewBundle(mockSnykCodeClient, mockInstrumentor, mockErrorReporter, &testLogger, "testRootPath", "testBundleHash", map[string]deepcode.BundleFile{}, []string{}, []string{}) + require.NoError(t, batchErr) - err = b.UploadBatch(context.Background(), "testRequestId", bundleFromRawContent) + oldHash := b.GetBundleHash() + err := b.UploadBatch(context.Background(), "testRequestId", bundleFromRawContent) require.NoError(t, err) - newestHash := b.GetBundleHash() - assert.NotEqual(t, newHash, newestHash) + newHash := b.GetBundleHash() + assert.NotEqual(t, oldHash, newHash) }) }