@@ -20,6 +20,7 @@ import (
20
20
"context"
21
21
"crypto/sha256"
22
22
"encoding/hex"
23
+ "fmt"
23
24
"os"
24
25
"testing"
25
26
@@ -39,6 +40,29 @@ var bundleWithMultipleFiles = bundle.NewBatch(map[string]deepcode.BundleFile{
39
40
"file" : {},
40
41
"another" : {},
41
42
})
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
+ }
42
66
43
67
func Test_UploadBatch (t * testing.T ) {
44
68
testLogger := zerolog .Nop ()
@@ -108,6 +132,31 @@ func Test_UploadBatch(t *testing.T) {
108
132
})
109
133
}
110
134
135
+ func Test_RawContentBatch (t * testing.T ) {
136
+ testLogger := zerolog .Nop ()
137
+
138
+ t .Run ("create a batch from raw content and upload the bundle" , func (t * testing.T ) {
139
+ ctrl := gomock .NewController (t )
140
+ mockSnykCodeClient := deepcodeMocks .NewMockDeepcodeClient (ctrl )
141
+ mockSnykCodeClient .EXPECT ().ExtendBundle (gomock .Any (), "testBundleHash" , bundleFilePartialMatcher {expectedKey : "hello" , expectedContent : "world" }, []string {}).Return ("newBundleHash" , []string {}, nil ).Times (1 )
142
+
143
+ mockSpan := mocks .NewMockSpan (ctrl )
144
+ mockSpan .EXPECT ().Context ().AnyTimes ()
145
+ mockInstrumentor := mocks .NewMockInstrumentor (ctrl )
146
+ mockInstrumentor .EXPECT ().StartSpan (gomock .Any (), gomock .Any ()).Return (mockSpan ).AnyTimes ()
147
+ mockInstrumentor .EXPECT ().Finish (gomock .Any ()).AnyTimes ()
148
+ mockErrorReporter := mocks .NewMockErrorReporter (ctrl )
149
+ b := bundle .NewBundle (mockSnykCodeClient , mockInstrumentor , mockErrorReporter , & testLogger , "testRootPath" , "testBundleHash" , map [string ]deepcode.BundleFile {}, []string {}, []string {})
150
+
151
+ require .NoError (t , batchErr )
152
+ oldHash := b .GetBundleHash ()
153
+ err := b .UploadBatch (context .Background (), "testRequestId" , bundleFromRawContent )
154
+ require .NoError (t , err )
155
+ newHash := b .GetBundleHash ()
156
+ assert .NotEqual (t , oldHash , newHash )
157
+ })
158
+ }
159
+
111
160
func Test_BundleEncoding (t * testing.T ) {
112
161
t .Run ("utf-8 encoded content" , func (t * testing.T ) {
113
162
content := []byte ("hello" )
0 commit comments