Skip to content

Commit 3c72dcc

Browse files
feat: add method to create batch from raw content
1 parent d8776ec commit 3c72dcc

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

bundle/bundle.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package bundle
1818

1919
import (
2020
"context"
21+
"fmt"
2122

2223
"github.com/rs/zerolog"
2324

@@ -134,6 +135,23 @@ func NewBatch(documents map[string]deepcode.BundleFile) *Batch {
134135
}
135136
}
136137

138+
func NewBatchFromRawContent(documents map[string][]byte) (*Batch, error) {
139+
bundleFiles := make(map[string]deepcode.BundleFile)
140+
141+
for key, rawData := range documents {
142+
bundleFile, err := deepcode.BundleFileFrom(rawData)
143+
if err != nil {
144+
return nil, fmt.Errorf("failed to create file from raw data: %v", err)
145+
}
146+
147+
bundleFiles[key] = bundleFile
148+
}
149+
150+
return &Batch{
151+
documents: bundleFiles,
152+
}, nil
153+
}
154+
137155
// todo simplify the size computation
138156
// maybe consider an addFile / canFitFile interface with proper error handling
139157
func (b *Batch) canFitFile(uri string, content []byte) bool {

bundle/bundle_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"crypto/sha256"
2222
"encoding/hex"
23+
"fmt"
2324
"os"
2425
"testing"
2526

@@ -39,6 +40,29 @@ var bundleWithMultipleFiles = bundle.NewBatch(map[string]deepcode.BundleFile{
3940
"file": {},
4041
"another": {},
4142
})
43+
var bundleFromRawContent, batchErr = bundle.NewBatchFromRawContent(map[string][]byte{"hello": []byte("world")})
44+
45+
// Matcher for BundleFile that matches on key and content (ignores hash)
46+
type bundleFilePartialMatcher struct {
47+
expectedKey string
48+
expectedContent string
49+
}
50+
51+
func (m bundleFilePartialMatcher) Matches(x interface{}) bool {
52+
files, ok := x.(map[string]deepcode.BundleFile)
53+
if !ok {
54+
return false
55+
}
56+
file, exists := files[m.expectedKey]
57+
if !exists {
58+
return false
59+
}
60+
return file.Content == m.expectedContent
61+
}
62+
63+
func (m bundleFilePartialMatcher) String() string {
64+
return fmt.Sprintf("{ Key : '%s', Content : '%s' }", m.expectedKey, m.expectedContent)
65+
}
4266

4367
func Test_UploadBatch(t *testing.T) {
4468
testLogger := zerolog.Nop()
@@ -89,6 +113,7 @@ func Test_UploadBatch(t *testing.T) {
89113
mockSnykCodeClient.EXPECT().ExtendBundle(gomock.Any(), "testBundleHash", map[string]deepcode.BundleFile{
90114
"file": {},
91115
}, []string{}).Return("testBundleHash", []string{}, nil).Times(1)
116+
mockSnykCodeClient.EXPECT().ExtendBundle(gomock.Any(), "bundleWithMultipleFilesHash", bundleFilePartialMatcher{expectedKey: "hello", expectedContent: "world"}, []string{}).Return("bundleWithAllFilesHash", []string{}, nil).Times(1)
92117

93118
mockSpan := mocks.NewMockSpan(ctrl)
94119
mockSpan.EXPECT().Context().AnyTimes()
@@ -105,6 +130,11 @@ func Test_UploadBatch(t *testing.T) {
105130
require.NoError(t, err)
106131
newHash := b.GetBundleHash()
107132
assert.NotEqual(t, oldHash, newHash)
133+
require.NoError(t, batchErr)
134+
err = b.UploadBatch(context.Background(), "testRequestId", bundleFromRawContent)
135+
require.NoError(t, err)
136+
newestHash := b.GetBundleHash()
137+
assert.NotEqual(t, newHash, newestHash)
108138
})
109139
}
110140

0 commit comments

Comments
 (0)