@@ -17,6 +17,7 @@ package codeclient_test
17
17
18
18
import (
19
19
"context"
20
+ "github.com/google/uuid"
20
21
"os"
21
22
"path/filepath"
22
23
"testing"
@@ -50,15 +51,16 @@ func Test_UploadAndAnalyze(t *testing.T) {
50
51
51
52
logger := zerolog .Nop ()
52
53
54
+ testOrgId := uuid .NewString ()
55
+
53
56
ctrl := gomock .NewController (t )
54
57
mockHTTPClient := httpmocks .NewMockHTTPClient (ctrl )
55
58
mockConfig := confMocks .NewMockConfig (ctrl )
56
59
mockConfig .EXPECT ().SnykCodeApi ().AnyTimes ().Return ("" )
57
60
mockConfig .EXPECT ().IsFedramp ().AnyTimes ().Return (false )
58
- mockConfig .EXPECT ().Organization ().AnyTimes ().Return ("4a72d1db-b465-4764-99e1-ecedad03b06a" )
61
+ mockConfig .EXPECT ().Organization ().AnyTimes ().Return (testOrgId )
59
62
mockConfig .EXPECT ().SnykApi ().AnyTimes ().Return ("" )
60
63
mockSpan := mocks .NewMockSpan (ctrl )
61
- mockSpan .EXPECT ().GetTraceId ().Return ("testRequestId" ).AnyTimes ()
62
64
mockSpan .EXPECT ().Context ().Return (context .Background ()).AnyTimes ()
63
65
mockInstrumentor := mocks .NewMockInstrumentor (ctrl )
64
66
mockInstrumentor .EXPECT ().StartSpan (gomock .Any (), gomock .Any ()).Return (mockSpan ).AnyTimes ()
@@ -70,10 +72,11 @@ func Test_UploadAndAnalyze(t *testing.T) {
70
72
71
73
t .Run (
72
74
"should just create bundle when hash empty" , func (t * testing.T ) {
75
+ requestId := uuid .NewString ()
73
76
mockBundle := bundle .NewBundle (deepcodeMocks .NewMockDeepcodeClient (ctrl ), mockInstrumentor , mockErrorReporter , & logger , "testRootPath" , "" , files , []string {}, []string {})
74
77
mockBundleManager := bundleMocks .NewMockBundleManager (ctrl )
75
- mockBundleManager .EXPECT ().Create (gomock .Any (), "testRequestId" , baseDir , gomock .Any (), map [string ]bool {}).Return (mockBundle , nil )
76
- mockBundleManager .EXPECT ().Upload (gomock .Any (), "testRequestId" , mockBundle , files ).Return (mockBundle , nil )
78
+ mockBundleManager .EXPECT ().Create (gomock .Any (), requestId , baseDir , gomock .Any (), map [string ]bool {}).Return (mockBundle , nil )
79
+ mockBundleManager .EXPECT ().Upload (gomock .Any (), requestId , mockBundle , files ).Return (mockBundle , nil )
77
80
78
81
codeScanner := codeclient .NewCodeScanner (
79
82
mockConfig ,
@@ -84,24 +87,50 @@ func Test_UploadAndAnalyze(t *testing.T) {
84
87
codeclient .WithLogger (& logger ),
85
88
)
86
89
87
- response , bundleHash , err := codeScanner .WithBundleManager (mockBundleManager ).UploadAndAnalyze (context .Background (), "testRequestId" , target , docs , map [string ]bool {})
90
+ response , bundleHash , err := codeScanner .WithBundleManager (mockBundleManager ).UploadAndAnalyze (context .Background (), requestId , target , docs , map [string ]bool {})
88
91
require .NoError (t , err )
89
92
assert .Equal (t , "" , bundleHash )
90
93
assert .Nil (t , response )
91
94
},
92
95
)
93
96
97
+ t .Run (
98
+ "should be able to upload without analysis" , func (t * testing.T ) {
99
+ requestId := uuid .NewString ()
100
+ mockBundle := bundle .NewBundle (deepcodeMocks .NewMockDeepcodeClient (ctrl ), mockInstrumentor , mockErrorReporter , & logger , "testRootPath" , uuid .NewString (), files , []string {}, []string {})
101
+ mockBundleManager := bundleMocks .NewMockBundleManager (ctrl )
102
+ mockBundleManager .EXPECT ().Create (gomock .Any (), requestId , baseDir , gomock .Any (), map [string ]bool {}).Return (mockBundle , nil )
103
+ mockBundleManager .EXPECT ().Upload (gomock .Any (), requestId , mockBundle , files ).Return (mockBundle , nil )
104
+
105
+ codeScanner := codeclient .NewCodeScanner (
106
+ mockConfig ,
107
+ mockHTTPClient ,
108
+ codeclient .WithTrackerFactory (mockTrackerFactory ),
109
+ codeclient .WithInstrumentor (mockInstrumentor ),
110
+ codeclient .WithErrorReporter (mockErrorReporter ),
111
+ codeclient .WithLogger (& logger ),
112
+ )
113
+
114
+ uploadedBundle , err := codeScanner .
115
+ WithBundleManager (mockBundleManager ).
116
+ Upload (context .Background (), requestId , target , docs , map [string ]bool {})
117
+ require .NoError (t , err )
118
+ assert .Equal (t , mockBundle .GetBundleHash (), uploadedBundle .GetBundleHash ())
119
+ },
120
+ )
121
+
94
122
t .Run (
95
123
"should retrieve from backend" , func (t * testing.T ) {
96
- mockBundle := bundle .NewBundle (deepcodeMocks .NewMockDeepcodeClient (ctrl ), mockInstrumentor , mockErrorReporter , & logger , "testRootPath" , "testBundleHash" , files , []string {}, []string {})
124
+ requestId := uuid .NewString ()
125
+ mockBundle := bundle .NewBundle (deepcodeMocks .NewMockDeepcodeClient (ctrl ), mockInstrumentor , mockErrorReporter , & logger , "testRootPath" , uuid .NewString (), files , []string {}, []string {})
97
126
mockBundleManager := bundleMocks .NewMockBundleManager (ctrl )
98
- mockBundleManager .EXPECT ().Create (gomock .Any (), "b372d1db-b465-4764-99e1-ecedad03b06a" , baseDir , gomock .Any (), map [string ]bool {}).Return (mockBundle , nil )
99
- mockBundleManager .EXPECT ().Upload (gomock .Any (), "b372d1db-b465-4764-99e1-ecedad03b06a" , mockBundle , files ).Return (mockBundle , nil )
127
+ mockBundleManager .EXPECT ().Create (gomock .Any (), requestId , baseDir , gomock .Any (), map [string ]bool {}).Return (mockBundle , nil )
128
+ mockBundleManager .EXPECT ().Upload (gomock .Any (), requestId , mockBundle , files ).Return (mockBundle , nil )
100
129
101
130
mockAnalysisOrchestrator := mockAnalysis .NewMockAnalysisOrchestrator (ctrl )
102
131
mockAnalysisOrchestrator .EXPECT ().RunTest (
103
132
gomock .Any (),
104
- "4a72d1db-b465-4764-99e1-ecedad03b06a" ,
133
+ testOrgId ,
105
134
gomock .Any (),
106
135
gomock .Any (),
107
136
gomock .Any (),
@@ -119,26 +148,26 @@ func Test_UploadAndAnalyze(t *testing.T) {
119
148
response , bundleHash , err := codeScanner .
120
149
WithBundleManager (mockBundleManager ).
121
150
WithAnalysisOrchestrator (mockAnalysisOrchestrator ).
122
- UploadAndAnalyze (context .Background (), "b372d1db-b465-4764-99e1-ecedad03b06a" , target , docs , map [string ]bool {})
151
+ UploadAndAnalyze (context .Background (), requestId , target , docs , map [string ]bool {})
123
152
require .NoError (t , err )
124
153
assert .Equal (t , "COMPLETE" , response .Status )
125
- assert .Equal (t , "testBundleHash" , bundleHash )
154
+ assert .Equal (t , mockBundle . GetBundleHash () , bundleHash )
126
155
},
127
156
)
128
157
129
158
t .Run (
130
159
"should send the changed files to the analysis" , func (t * testing.T ) {
131
160
relativeChangedFile := "./nested/folder/nested/file.ts"
132
-
133
- mockBundle := bundle .NewBundle (deepcodeMocks .NewMockDeepcodeClient (ctrl ), mockInstrumentor , mockErrorReporter , & logger , "testRootPath" , "testBundleHash" , files , []string {relativeChangedFile }, []string {})
161
+ requestId := uuid . NewString ()
162
+ mockBundle := bundle .NewBundle (deepcodeMocks .NewMockDeepcodeClient (ctrl ), mockInstrumentor , mockErrorReporter , & logger , "testRootPath" , uuid . NewString () , files , []string {relativeChangedFile }, []string {})
134
163
mockBundleManager := bundleMocks .NewMockBundleManager (ctrl )
135
- mockBundleManager .EXPECT ().Create (gomock .Any (), "b372d1db-b465-4764-99e1-ecedad03b06a" , baseDir , gomock .Any (), map [string ]bool {}).Return (mockBundle , nil )
136
- mockBundleManager .EXPECT ().Upload (gomock .Any (), "b372d1db-b465-4764-99e1-ecedad03b06a" , mockBundle , files ).Return (mockBundle , nil )
164
+ mockBundleManager .EXPECT ().Create (gomock .Any (), requestId , baseDir , gomock .Any (), map [string ]bool {}).Return (mockBundle , nil )
165
+ mockBundleManager .EXPECT ().Upload (gomock .Any (), requestId , mockBundle , files ).Return (mockBundle , nil )
137
166
138
167
mockAnalysisOrchestrator := mockAnalysis .NewMockAnalysisOrchestrator (ctrl )
139
168
mockAnalysisOrchestrator .EXPECT ().RunTest (
140
169
gomock .Any (),
141
- "4a72d1db-b465-4764-99e1-ecedad03b06a" ,
170
+ testOrgId ,
142
171
gomock .Any (),
143
172
gomock .Any (),
144
173
gomock .Any (),
@@ -156,7 +185,7 @@ func Test_UploadAndAnalyze(t *testing.T) {
156
185
response , _ , err := codeScanner .
157
186
WithBundleManager (mockBundleManager ).
158
187
WithAnalysisOrchestrator (mockAnalysisOrchestrator ).
159
- UploadAndAnalyze (context .Background (), "b372d1db-b465-4764-99e1-ecedad03b06a" , target , docs , map [string ]bool {})
188
+ UploadAndAnalyze (context .Background (), requestId , target , docs , map [string ]bool {})
160
189
require .NoError (t , err )
161
190
assert .Equal (t , "COMPLETE" , response .Status )
162
191
},
@@ -212,6 +241,8 @@ func TestAnalyzeRemote(t *testing.T) {
212
241
gomock .Any (),
213
242
).Return (nil , nil , assert .AnError )
214
243
244
+ mockErrorReporter .EXPECT ().CaptureError (gomock .Any (), gomock .Any ())
245
+
215
246
response , _ , err := codeScanner .AnalyzeRemote (context .Background ())
216
247
assert .Nil (t , response )
217
248
assert .Error (t , err )
0 commit comments