11package scan
22
33import (
4+ "bytes"
5+ "encoding/json"
46 "github.com/debricked/cli/pkg/ci"
57 "github.com/debricked/cli/pkg/ci/argo"
68 "github.com/debricked/cli/pkg/ci/azure"
@@ -12,8 +14,12 @@ import (
1214 "github.com/debricked/cli/pkg/ci/gitlab"
1315 "github.com/debricked/cli/pkg/ci/travis"
1416 "github.com/debricked/cli/pkg/client"
17+ "github.com/debricked/cli/pkg/client/testdata"
18+ "github.com/debricked/cli/pkg/file"
1519 "github.com/debricked/cli/pkg/git"
1620 "github.com/debricked/cli/pkg/upload"
21+ "io"
22+ "net/http"
1723 "os"
1824 "path/filepath"
1925 "strings"
@@ -166,6 +172,85 @@ func TestScanBadOpts(t *testing.T) {
166172 }
167173}
168174
175+ func TestScanEmptyResult (t * testing.T ) {
176+ var debClient client.IDebClient
177+ clientMock := testdata .NewDebClientMock ()
178+
179+ // Create mocked formats response
180+ formats := []file.Format {{
181+ Regex : "" ,
182+ LockFileRegexes : []string {"yarn\\ .lock" },
183+ }}
184+ formatsBytes , _ := json .Marshal (formats )
185+ formatsMockRes := testdata.MockResponse {
186+ StatusCode : http .StatusOK ,
187+ ResponseBody : io .NopCloser (bytes .NewReader (formatsBytes )),
188+ }
189+ clientMock .AddMockUriResponse ("/api/1.0/open/files/supported-formats" , formatsMockRes )
190+
191+ // Create mocked file upload response
192+ uploadMockRes := testdata.MockResponse {
193+ StatusCode : http .StatusOK ,
194+ ResponseBody : io .NopCloser (strings .NewReader ("{\" ciUploadId\" : 1}" )),
195+ }
196+ clientMock .AddMockUriResponse ("/api/1.0/open/uploads/dependencies/files" , uploadMockRes )
197+
198+ // Create a mocked finish response
199+ finishMockRes := testdata.MockResponse {
200+ StatusCode : http .StatusNoContent ,
201+ ResponseBody : io .NopCloser (strings .NewReader ("{}" )),
202+ }
203+ clientMock .AddMockUriResponse ("/api/1.0/open/finishes/dependencies/files/uploads" , finishMockRes )
204+
205+ // Create mocked scan result response, 201 is returned when the queue time are too long
206+ scanMockRes := testdata.MockResponse {
207+ StatusCode : http .StatusCreated ,
208+ ResponseBody : io .NopCloser (strings .NewReader ("{}" )),
209+ }
210+ clientMock .AddMockUriResponse ("/api/1.0/open/ci/upload/status" , scanMockRes )
211+
212+ debClient = clientMock
213+
214+ var ciService ci.IService
215+ ciService = ci .NewService (nil )
216+ scanner , _ := NewDebrickedScanner (& debClient , ciService )
217+ path := "testdata/yarn"
218+ repositoryName := path
219+ commitName := "testdata/yarn-commit"
220+ cwd , _ := os .Getwd ()
221+ opts := DebrickedOptions {
222+ Path : path ,
223+ Exclusions : nil ,
224+ RepositoryName : repositoryName ,
225+ CommitName : commitName ,
226+ BranchName : "" ,
227+ CommitAuthor : "" ,
228+ RepositoryUrl : "" ,
229+ IntegrationName : "" ,
230+ }
231+
232+ rescueStdout := os .Stdout
233+ r , w , _ := os .Pipe ()
234+ os .Stdout = w
235+
236+ err := scanner .Scan (opts )
237+
238+ _ = w .Close ()
239+ out , _ := io .ReadAll (r )
240+ os .Stdout = rescueStdout
241+
242+ existsMessageInCMDOutput := strings .Contains (
243+ string (out ),
244+ "Progress polling terminated due to long scan times. Please try again later" )
245+
246+ if err != nil || ! existsMessageInCMDOutput {
247+ t .Error ("failed to assert that scan ran without errors. Error:" , err )
248+ }
249+
250+ // reset working directory that has been manipulated in scanner.Scan
251+ _ = os .Chdir (cwd )
252+ }
253+
169254func TestMapEnvToOptions (t * testing.T ) {
170255 dOptionsTemplate := DebrickedOptions {
171256 Path : "path" ,
@@ -328,7 +413,12 @@ func TestSetWorkingDirectory(t *testing.T) {
328413 for _ , c := range cases {
329414 t .Run (c .name , func (t * testing.T ) {
330415 err := SetWorkingDirectory (& c .opts )
331- defer os .Chdir (cwd )
416+ defer func (dir string ) {
417+ err := os .Chdir (dir )
418+ if err != nil {
419+ t .Fatal ("Can not read the directory: " , dir )
420+ }
421+ }(cwd )
332422
333423 if len (c .errMessages ) > 0 {
334424 containsCorrectErrMsg := false
0 commit comments