Skip to content

Commit

Permalink
added test
Browse files Browse the repository at this point in the history
  • Loading branch information
psmf22 committed Feb 7, 2024
1 parent 76bb1c2 commit 33b05c8
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions internal/scan/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,73 @@ func TestScan(t *testing.T) {
}
}

func TestScanWithJsonPath(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skipf("TestScan is skipped due to Windows env")
}
clientMock := testdata.NewDebClientMock()
addMockedFormatsResponse(clientMock, "package\\.json")
addMockedFileUploadResponse(clientMock)
addMockedFinishResponse(clientMock, http.StatusNoContent)
addMockedStatusResponse(clientMock, http.StatusOK, 50)
addMockedStatusResponse(clientMock, http.StatusOK, 100)
scanner := makeScanner(clientMock, nil, nil)

path := testdataNpm
repositoryName := path
cwd, _ := os.Getwd()
jsonPath := filepath.Join(cwd, "result.json")

// reset working directory that has been manipulated in scanner.Scan
defer resetWd(t, cwd)
opts := DebrickedOptions{
Path: path,
Exclusions: nil,
RepositoryName: repositoryName,
CommitName: "commit",
BranchName: "",
CommitAuthor: "",
RepositoryUrl: "",
IntegrationName: "",
CallGraphUploadTimeout: 10 * 60,
CallGraphGenerateTimeout: 10 * 60,
JsonFilePath: "result.json",
}

rescueStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

err := scanner.Scan(opts)

_ = w.Close()
output, _ := io.ReadAll(r)
os.Stdout = rescueStdout

if err != nil {
t.Error("failed to assert that scan ran without errors. Error:", err)
}

outputAssertions := []string{
"Working directory: /",
"cli/internal/scan",
"Successfully uploaded",
"package.json\n",
"Successfully initialized scan\n",
"Scanning...",
"0% |",
"50% |",
"100% |",
"32m✔",
"0 vulnerabilities found\n\n",
"For full details, visit:",
}
for _, assertion := range outputAssertions {
assert.Contains(t, string(output), assertion)
}
assert.FileExists(t, jsonPath)
}

func TestScanFailingMetaObject(t *testing.T) {
var debClient client.IDebClient = testdata.NewDebClientMock()
scanner := NewDebrickedScanner(&debClient, nil, nil, ciService, nil, nil, nil)
Expand Down

0 comments on commit 33b05c8

Please sign in to comment.