github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/pkg/whitesource/utilsMock.go (about)

     1  //go:build !release
     2  // +build !release
     3  
     4  package whitesource
     5  
     6  import (
     7  	"net/http"
     8  	"os"
     9  
    10  	"github.com/SAP/jenkins-library/pkg/mock"
    11  	"github.com/SAP/jenkins-library/pkg/piperutils"
    12  	"github.com/pkg/errors"
    13  )
    14  
    15  func newTestScan(config *ScanOptions) *Scan {
    16  	return &Scan{
    17  		AggregateProjectName: config.ProjectName,
    18  		ProductVersion:       config.ProductVersion,
    19  	}
    20  }
    21  
    22  // NpmInstall records in which directory "npm install" has been invoked and for which package.json files.
    23  type NpmInstall struct {
    24  	CurrentDir  string
    25  	PackageJSON []string
    26  }
    27  
    28  // DownloadedFile records what URL has been downloaded to which file.
    29  type DownloadedFile struct {
    30  	SourceURL string
    31  	FilePath  string
    32  }
    33  
    34  // ScanUtilsMock is an implementation of the Utils interface that can be used during tests.
    35  type ScanUtilsMock struct {
    36  	*mock.FilesMock
    37  	*mock.ExecMockRunner
    38  	NpmInstalledModules []NpmInstall
    39  	DownloadedFiles     []DownloadedFile
    40  	DownloadError       map[string]error
    41  	RemoveAllDirs       []string
    42  	RemoveAllError      map[string]error
    43  }
    44  
    45  // RemoveAll mimics os.RemoveAll().
    46  func (m *ScanUtilsMock) RemoveAll(dir string) error {
    47  	// Can be removed once implemented in mock.FilesMock.
    48  	m.RemoveAllDirs = append(m.RemoveAllDirs, dir)
    49  	if m.RemoveAllError[dir] != nil {
    50  		return m.RemoveAllError[dir]
    51  	}
    52  	return nil
    53  }
    54  
    55  // FindPackageJSONFiles mimics npm.FindPackageJSONFiles() based on the FilesMock setup.
    56  func (m *ScanUtilsMock) FindPackageJSONFiles(options *ScanOptions) ([]string, error) {
    57  	unfilteredMatches, _ := m.Glob("**/package.json")
    58  	return piperutils.ExcludeFiles(unfilteredMatches, options.BuildDescriptorExcludeList)
    59  }
    60  
    61  // InstallAllNPMDependencies mimics npm.InstallAllNPMDependencies() and records the "npm install".
    62  func (m *ScanUtilsMock) InstallAllNPMDependencies(_ *ScanOptions, packageJSONs []string) error {
    63  	m.NpmInstalledModules = append(m.NpmInstalledModules, NpmInstall{
    64  		CurrentDir:  m.CurrentDir,
    65  		PackageJSON: packageJSONs,
    66  	})
    67  	return nil
    68  }
    69  
    70  // DownloadFile mimics http.Downloader and records the downloaded file.
    71  func (m *ScanUtilsMock) DownloadFile(url, filename string, _ http.Header, _ []*http.Cookie) error {
    72  	if url == "errorCopyFile" {
    73  		return errors.New("unable to copy content from url to file")
    74  	}
    75  	if url == "error404NotFound" {
    76  		return errors.New("returned with response 404 Not Found")
    77  	}
    78  	if url == "error403Forbidden" {
    79  		return errors.New("returned with response 403 Forbidden")
    80  	}
    81  	if m.DownloadError[url] != nil {
    82  		return m.DownloadError[url]
    83  	}
    84  	m.DownloadedFiles = append(m.DownloadedFiles, DownloadedFile{SourceURL: url, FilePath: filename})
    85  	return nil
    86  }
    87  
    88  // FileOpen mimics os.FileOpen() based on FilesMock OpenFile().
    89  func (m *ScanUtilsMock) FileOpen(name string, flag int, perm os.FileMode) (File, error) {
    90  	return m.OpenFile(name, flag, perm)
    91  }
    92  
    93  // NewScanUtilsMock returns an initialized ScanUtilsMock instance.
    94  func NewScanUtilsMock() *ScanUtilsMock {
    95  	return &ScanUtilsMock{
    96  		FilesMock:      &mock.FilesMock{},
    97  		ExecMockRunner: &mock.ExecMockRunner{},
    98  	}
    99  }