1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-01-18 05:18:24 +02:00

Pass correct module path to npm.InstallAllDependencies() (#2144)

We are already in the module's directory.
This commit is contained in:
Stephan Aßmus 2020-10-09 08:33:36 +02:00 committed by GitHub
parent a70c0dfaed
commit ed97142f6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 7 deletions

View File

@ -639,7 +639,7 @@ func executeNpmScanForModule(modulePath string, config *ScanOptions, scan *white
return err
}
if err := reinstallNodeModulesIfLsFails(modulePath, config, utils); err != nil {
if err := reinstallNodeModulesIfLsFails(config, utils); err != nil {
return err
}
@ -681,7 +681,7 @@ func getNpmProjectName(modulePath string, utils whitesourceUtils) (string, error
// This hack/work-around that should be removed once scanning it consistently performed using the Unified Agent.
// A possible reason for encountering "npm ls" errors in the first place is that a different node version
// is used for whitesourceExecuteScan due to a different docker image being used compared to the build stage.
func reinstallNodeModulesIfLsFails(modulePath string, config *ScanOptions, utils whitesourceUtils) error {
func reinstallNodeModulesIfLsFails(config *ScanOptions, utils whitesourceUtils) error {
// No need to have output from "npm ls" in the log
utils.Stdout(ioutil.Discard)
defer utils.Stdout(log.Writer())
@ -706,7 +706,8 @@ func reinstallNodeModulesIfLsFails(modulePath string, config *ScanOptions, utils
return fmt.Errorf("failed to remove package-lock.json: %w", err)
}
}
return utils.InstallAllNPMDependencies(config, []string{modulePath})
// Passing only "package.json", because we are already inside the module's directory.
return utils.InstallAllNPMDependencies(config, []string{"package.json"})
}
// executeYarnScan generates a configuration file whitesource.config.json with appropriate values from config,

View File

@ -131,6 +131,11 @@ type downloadedFile struct {
filePath string
}
type npmInstall struct {
currentDir string
packageJSON []string
}
type whitesourceUtilsMock struct {
*mock.FilesMock
*mock.ExecMockRunner
@ -139,7 +144,7 @@ type whitesourceUtilsMock struct {
usedBuildDescriptorFile string
usedOptions versioning.Options
downloadedFiles []downloadedFile
npmInstalledModules []string
npmInstalledModules []npmInstall
}
func (w *whitesourceUtilsMock) DownloadFile(url, filename string, _ http.Header, _ []*http.Cookie) error {
@ -169,8 +174,11 @@ func (w *whitesourceUtilsMock) FindPackageJSONFiles(_ *ScanOptions) ([]string, e
return matches, nil
}
func (w *whitesourceUtilsMock) InstallAllNPMDependencies(_ *ScanOptions, _ []string) error {
w.npmInstalledModules = append(w.npmInstalledModules, w.CurrentDir)
func (w *whitesourceUtilsMock) InstallAllNPMDependencies(_ *ScanOptions, packageJSONs []string) error {
w.npmInstalledModules = append(w.npmInstalledModules, npmInstall{
currentDir: w.CurrentDir,
packageJSON: packageJSONs,
})
return nil
}
@ -427,7 +435,11 @@ func TestExecuteScanNPM(t *testing.T) {
err := executeScan(&config, scan, utilsMock)
// assert
assert.NoError(t, err)
assert.Equal(t, []string{"app", ""}, utilsMock.npmInstalledModules)
expectedNpmInstalls := []npmInstall{
{currentDir: "app", packageJSON: []string{"package.json"}},
{currentDir: "", packageJSON: []string{"package.json"}},
}
assert.Equal(t, expectedNpmInstalls, utilsMock.npmInstalledModules)
assert.True(t, utilsMock.HasRemovedFile("package-lock.json"))
})
}