1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/pkg/whitesource/utilsMock.go
Sven Merk a1988f6808
feat(whitesourceExecuteScan): GitHub issue creation + SARIF (#3535)
* Add GH issue creation + SARIF

* Code cleanup

* Fix fmt, add debug

* Code enhancements

* Fix

* Added debug info

* Rework UA log scan

* Fix code

* read UA version

* Fix nil reference

* Extraction

* Credentials

* Issue creation

* Error handling

* Fix issue creation

* query escape

* Query escape 2

* Revert

* Test avoid update

* HTTP client

* Add support for custom TLS certs

* Fix code

* Fix code 2

* Fix code 3

* Disable cert check

* Fix auth

* Remove implicit trust

* Skip verification

* Fix

* Fix client

* Fix HTTP auth

* Fix trusted certs

* Trim version

* Code

* Add token

* Added token handling to client

* Fix token

* Cleanup

* Fix token

* Token rework

* Fix code

* Kick out oauth client

* Kick out oauth client

* Transport wrapping

* Token

* Simplification

* Refactor

* Variation

* Check

* Fix

* Debug

* Switch client

* Variation

* Debug

* Switch to cert check

* Add debug

* Parse self

* Cleanup

* Update resources/metadata/whitesourceExecuteScan.yaml

* Add debug

* Expose subjects

* Patch

* Debug

* Debug2

* Debug3

* Fix logging response body

* Cleanup

* Cleanup

* Fix request body logging

* Cleanup import

* Fix import cycle

* Cleanup

* Fix fmt

* Fix NopCloser reference

* Regenerate

* Reintroduce

* Fix test

* Fix tests

* Correction

* Fix error

* Code fix

* Fix tests

* Add tests

* Fix code climate issues

* Code climate

* Code climate again

* Code climate again

* Fix fmt

* Fix fmt 2

Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
2022-02-23 09:30:19 +01:00

100 lines
2.9 KiB
Go

//go:build !release
// +build !release
package whitesource
import (
"net/http"
"os"
"github.com/SAP/jenkins-library/pkg/mock"
"github.com/SAP/jenkins-library/pkg/piperutils"
"github.com/pkg/errors"
)
func newTestScan(config *ScanOptions) *Scan {
return &Scan{
AggregateProjectName: config.ProjectName,
ProductVersion: config.ProductVersion,
}
}
// NpmInstall records in which directory "npm install" has been invoked and for which package.json files.
type NpmInstall struct {
CurrentDir string
PackageJSON []string
}
// DownloadedFile records what URL has been downloaded to which file.
type DownloadedFile struct {
SourceURL string
FilePath string
}
// ScanUtilsMock is an implementation of the Utils interface that can be used during tests.
type ScanUtilsMock struct {
*mock.FilesMock
*mock.ExecMockRunner
NpmInstalledModules []NpmInstall
DownloadedFiles []DownloadedFile
DownloadError map[string]error
RemoveAllDirs []string
RemoveAllError map[string]error
}
// RemoveAll mimics os.RemoveAll().
func (m *ScanUtilsMock) RemoveAll(dir string) error {
// Can be removed once implemented in mock.FilesMock.
m.RemoveAllDirs = append(m.RemoveAllDirs, dir)
if m.RemoveAllError[dir] != nil {
return m.RemoveAllError[dir]
}
return nil
}
// FindPackageJSONFiles mimics npm.FindPackageJSONFiles() based on the FilesMock setup.
func (m *ScanUtilsMock) FindPackageJSONFiles(options *ScanOptions) ([]string, error) {
unfilteredMatches, _ := m.Glob("**/package.json")
return piperutils.ExcludeFiles(unfilteredMatches, options.BuildDescriptorExcludeList)
}
// InstallAllNPMDependencies mimics npm.InstallAllNPMDependencies() and records the "npm install".
func (m *ScanUtilsMock) InstallAllNPMDependencies(_ *ScanOptions, packageJSONs []string) error {
m.NpmInstalledModules = append(m.NpmInstalledModules, NpmInstall{
CurrentDir: m.CurrentDir,
PackageJSON: packageJSONs,
})
return nil
}
// DownloadFile mimics http.Downloader and records the downloaded file.
func (m *ScanUtilsMock) DownloadFile(url, filename string, _ http.Header, _ []*http.Cookie) error {
if url == "errorCopyFile" {
return errors.New("unable to copy content from url to file")
}
if url == "error404NotFound" {
return errors.New("returned with response 404 Not Found")
}
if url == "error403Forbidden" {
return errors.New("returned with response 403 Forbidden")
}
if m.DownloadError[url] != nil {
return m.DownloadError[url]
}
m.DownloadedFiles = append(m.DownloadedFiles, DownloadedFile{SourceURL: url, FilePath: filename})
return nil
}
// FileOpen mimics os.FileOpen() based on FilesMock OpenFile().
func (m *ScanUtilsMock) FileOpen(name string, flag int, perm os.FileMode) (File, error) {
return m.OpenFile(name, flag, perm)
}
// NewScanUtilsMock returns an initialized ScanUtilsMock instance.
func NewScanUtilsMock() *ScanUtilsMock {
return &ScanUtilsMock{
FilesMock: &mock.FilesMock{},
ExecMockRunner: &mock.ExecMockRunner{},
}
}