@@ -17,6 +17,7 @@ package codeclient_test
1717
1818import (
1919 "context"
20+ "github.com/google/uuid"
2021 "os"
2122 "path/filepath"
2223 "testing"
@@ -50,15 +51,16 @@ func Test_UploadAndAnalyze(t *testing.T) {
5051
5152 logger := zerolog .Nop ()
5253
54+ testOrgId := uuid .NewString ()
55+
5356 ctrl := gomock .NewController (t )
5457 mockHTTPClient := httpmocks .NewMockHTTPClient (ctrl )
5558 mockConfig := confMocks .NewMockConfig (ctrl )
5659 mockConfig .EXPECT ().SnykCodeApi ().AnyTimes ().Return ("" )
5760 mockConfig .EXPECT ().IsFedramp ().AnyTimes ().Return (false )
58- mockConfig .EXPECT ().Organization ().AnyTimes ().Return ("4a72d1db-b465-4764-99e1-ecedad03b06a" )
61+ mockConfig .EXPECT ().Organization ().AnyTimes ().Return (testOrgId )
5962 mockConfig .EXPECT ().SnykApi ().AnyTimes ().Return ("" )
6063 mockSpan := mocks .NewMockSpan (ctrl )
61- mockSpan .EXPECT ().GetTraceId ().Return ("testRequestId" ).AnyTimes ()
6264 mockSpan .EXPECT ().Context ().Return (context .Background ()).AnyTimes ()
6365 mockInstrumentor := mocks .NewMockInstrumentor (ctrl )
6466 mockInstrumentor .EXPECT ().StartSpan (gomock .Any (), gomock .Any ()).Return (mockSpan ).AnyTimes ()
@@ -70,10 +72,11 @@ func Test_UploadAndAnalyze(t *testing.T) {
7072
7173 t .Run (
7274 "should just create bundle when hash empty" , func (t * testing.T ) {
75+ requestId := uuid .NewString ()
7376 mockBundle := bundle .NewBundle (deepcodeMocks .NewMockDeepcodeClient (ctrl ), mockInstrumentor , mockErrorReporter , & logger , "testRootPath" , "" , files , []string {}, []string {})
7477 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 )
7780
7881 codeScanner := codeclient .NewCodeScanner (
7982 mockConfig ,
@@ -84,24 +87,50 @@ func Test_UploadAndAnalyze(t *testing.T) {
8487 codeclient .WithLogger (& logger ),
8588 )
8689
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 {})
8891 require .NoError (t , err )
8992 assert .Equal (t , "" , bundleHash )
9093 assert .Nil (t , response )
9194 },
9295 )
9396
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+
94122 t .Run (
95123 "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 {})
97126 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 )
100129
101130 mockAnalysisOrchestrator := mockAnalysis .NewMockAnalysisOrchestrator (ctrl )
102131 mockAnalysisOrchestrator .EXPECT ().RunTest (
103132 gomock .Any (),
104- "4a72d1db-b465-4764-99e1-ecedad03b06a" ,
133+ testOrgId ,
105134 gomock .Any (),
106135 gomock .Any (),
107136 gomock .Any (),
@@ -119,26 +148,26 @@ func Test_UploadAndAnalyze(t *testing.T) {
119148 response , bundleHash , err := codeScanner .
120149 WithBundleManager (mockBundleManager ).
121150 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 {})
123152 require .NoError (t , err )
124153 assert .Equal (t , "COMPLETE" , response .Status )
125- assert .Equal (t , "testBundleHash" , bundleHash )
154+ assert .Equal (t , mockBundle . GetBundleHash () , bundleHash )
126155 },
127156 )
128157
129158 t .Run (
130159 "should send the changed files to the analysis" , func (t * testing.T ) {
131160 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 {})
134163 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 )
137166
138167 mockAnalysisOrchestrator := mockAnalysis .NewMockAnalysisOrchestrator (ctrl )
139168 mockAnalysisOrchestrator .EXPECT ().RunTest (
140169 gomock .Any (),
141- "4a72d1db-b465-4764-99e1-ecedad03b06a" ,
170+ testOrgId ,
142171 gomock .Any (),
143172 gomock .Any (),
144173 gomock .Any (),
@@ -156,7 +185,7 @@ func Test_UploadAndAnalyze(t *testing.T) {
156185 response , _ , err := codeScanner .
157186 WithBundleManager (mockBundleManager ).
158187 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 {})
160189 require .NoError (t , err )
161190 assert .Equal (t , "COMPLETE" , response .Status )
162191 },
@@ -212,6 +241,8 @@ func TestAnalyzeRemote(t *testing.T) {
212241 gomock .Any (),
213242 ).Return (nil , nil , assert .AnError )
214243
244+ mockErrorReporter .EXPECT ().CaptureError (gomock .Any (), gomock .Any ())
245+
215246 response , _ , err := codeScanner .AnalyzeRemote (context .Background ())
216247 assert .Nil (t , response )
217248 assert .Error (t , err )
0 commit comments