Skip to content

Commit

Permalink
more reliably delete temp files
Browse files Browse the repository at this point in the history
  • Loading branch information
emilwareus committed Oct 2, 2023
1 parent 5bdcb2e commit 06f7fb8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
4 changes: 2 additions & 2 deletions internal/resolution/pm/nuget/cmd_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ func (cmdf *CmdFactory) MakeInstallCmd(command string, file string) (*exec.Cmd,
fileLockName := "packages.lock.json"
if packageConfig.MatchString(file) {
file, err = cmdf.convertPackagesConfigToCsproj(file, command)
cmdf.tempoCsproj = file
if err != nil {
return nil, err
}
cmdf.tempoCsproj = file
fileLockName = ".packages.config.nuget.debricked.lock"
}

Expand Down Expand Up @@ -152,7 +152,7 @@ func (cmdf *CmdFactory) createCsprojContentWithTemplate(targetFrameworksStr stri
"Packages": packages,
})
if err != nil {
return "", err
return tpl.String(), err
}

return tpl.String(), nil
Expand Down
11 changes: 7 additions & 4 deletions internal/resolution/pm/nuget/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func (j *Job) Run() {

j.SendStatus("installing dependencies")
output, err := j.runInstallCmd()
defer j.cleanupTempoCsproj()
if err != nil {
j.Errors().Critical(fmt.Errorf("%s\n%s", output, err))

Expand All @@ -63,15 +64,17 @@ func (j *Job) runInstallCmd() ([]byte, error) {
return installCmdOutput, j.GetExitError(err)
}

return installCmdOutput, nil
}

func (j *Job) cleanupTempoCsproj() {
// Cleanup of the temporary .csproj file (packages.config)
tempFile := j.cmdFactory.GetTempoCsproj()
if tempFile != "" {
// remove the packages.config.csproj file
err = osRemoveAll(tempFile)
err := osRemoveAll(tempFile)
if err != nil {
return installCmdOutput, j.GetExitError(err)
j.Errors().Critical(fmt.Errorf("failed to remove temporary .csproj file: %s", err))
}
}

return installCmdOutput, nil
}
6 changes: 3 additions & 3 deletions internal/resolution/pm/nuget/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestRunInstallPackagesConfigRemoveAllErr(t *testing.T) {

oldOsRemoveAll := osRemoveAll
cmdErr := errors.New("os-remove-all-error")
cmdErrGt := errors.New("\n\nos-remove-all-error")
cmdErrGt := errors.New("failed to remove temporary .csproj file: os-remove-all-error")
osRemoveAll = func(path string) error {
return cmdErr
}
Expand All @@ -61,8 +61,8 @@ func TestRunInstallPackagesConfigRemoveAllErr(t *testing.T) {

go jobTestdata.WaitStatus(j)
j.Run()

assert.Equal(t, j.Errors().GetAll()[0], cmdErrGt)
errors := j.Errors().GetAll()
assert.Equal(t, errors[0], cmdErrGt)

}

Expand Down

0 comments on commit 06f7fb8

Please sign in to comment.