From 6773abceb5314507c4a8ecd776052b42a9bd7b85 Mon Sep 17 00:00:00 2001 From: Bar Belity Date: Wed, 17 Apr 2019 18:11:48 +0300 Subject: [PATCH] Use client's temp util when creating temp dirs --- executers/dependenciesutils.go | 12 ++++-------- executers/dependenciesutils_test.go | 24 ++++++++++++------------ executers/populateandpublish.go | 14 ++++++++------ 3 files changed, 24 insertions(+), 26 deletions(-) diff --git a/executers/dependenciesutils.go b/executers/dependenciesutils.go index b86737d..356ab7b 100644 --- a/executers/dependenciesutils.go +++ b/executers/dependenciesutils.go @@ -141,20 +141,16 @@ func performHeadRequest(auth auth.ArtifactoryDetails, client *httpclient.HttpCli } // Creating dependency with the mod file in the temp directory -func createDependencyInTemp(zipPath string) (tempDir string, err error) { - tempDir, err = fileutils.GetTempDirPath() - if err != nil { - return "", err - } +func createDependencyInTemp(zipPath, tempDir string) (err error) { multiReader, err := multifilereader.NewMultiFileReaderAt([]string{zipPath}) if err != nil { - return "", errorutils.CheckError(err) + return errorutils.CheckError(err) } err = fileutils.Unzip(multiReader, multiReader.Size(), tempDir) if err != nil { - return "", errorutils.CheckError(err) + return errorutils.CheckError(err) } - return tempDir, nil + return nil } // Returns the actual path to the dependency. diff --git a/executers/dependenciesutils_test.go b/executers/dependenciesutils_test.go index 8f3d847..85e448e 100644 --- a/executers/dependenciesutils_test.go +++ b/executers/dependenciesutils_test.go @@ -67,11 +67,11 @@ func TestEncodeDecodePath(t *testing.T) { } func TestCreateDependency(t *testing.T) { - err := fileutils.CreateTempDirPath() + tempDirPath, err := fileutils.CreateTempDir() if err != nil { t.Error(err) } - defer fileutils.RemoveTempDir() + defer fileutils.RemoveTempDir(tempDirPath) baseDir, err := getBaseDir() if err != nil { t.Error(err) @@ -83,11 +83,11 @@ func TestCreateDependency(t *testing.T) { modContent: []byte(modContent), zipPath: filepath.Join(cachePath, "v1.2.3.zip"), } - tempDir, err := createDependencyInTemp(dep.GetZipPath()) + err = createDependencyInTemp(dep.GetZipPath(), tempDirPath) if err != nil { t.Error(err) } - path := filepath.Join(tempDir, "github.com", "test@v1.2.3", "test.go") + path := filepath.Join(tempDirPath, "github.com", "test@v1.2.3", "test.go") exists, err := fileutils.IsFileExists(path, false) if err != nil { @@ -97,18 +97,18 @@ func TestCreateDependency(t *testing.T) { if !exists { t.Error(fmt.Sprintf("Missing %s", path)) } - err = os.RemoveAll(filepath.Join(tempDir, "github.com")) + err = os.RemoveAll(filepath.Join(tempDirPath, "github.com")) if err != nil { t.Error(err) } } func TestGetModPath(t *testing.T) { - err := fileutils.CreateTempDirPath() + tempDirPath, err := fileutils.CreateTempDir() if err != nil { t.Error(err) } - defer fileutils.RemoveTempDir() + defer fileutils.RemoveTempDir(tempDirPath) baseDir, err := getBaseDir() if err != nil { t.Error(err) @@ -120,18 +120,18 @@ func TestGetModPath(t *testing.T) { modContent: []byte(modContent), zipPath: filepath.Join(cachePath, "v1.2.3.zip"), } - pwd := PackageWithDeps{Dependency: &dep} - tempDir, err := createDependencyInTemp(dep.GetZipPath()) + pwd := PackageWithDeps{Dependency: &dep, depsTempDir: tempDirPath} + err = createDependencyInTemp(dep.GetZipPath(), tempDirPath) if err != nil { t.Error(err) } - modPath := pwd.getModPathInTemp(tempDir) - path := filepath.Join(tempDir, "github.com", "test@v1.2.3", "go.mod") + modPath := pwd.getModPathInTemp(tempDirPath) + path := filepath.Join(tempDirPath, "github.com", "test@v1.2.3", "go.mod") if path != modPath { t.Error(fmt.Sprintf("Expected %s, got %s", path, modPath)) } - err = os.RemoveAll(filepath.Join(tempDir, "github.com")) + err = os.RemoveAll(filepath.Join(tempDirPath, "github.com")) if err != nil { t.Error(err) } diff --git a/executers/populateandpublish.go b/executers/populateandpublish.go index f523e8a..431c518 100644 --- a/executers/populateandpublish.go +++ b/executers/populateandpublish.go @@ -28,16 +28,17 @@ type PackageWithDeps struct { cachePath string GoModEditMessage string originalModContent []byte + depsTempDir string } // Populates and publish the dependencies. func RecursivePublish(targetRepo, goModEditMessage string, serviceManager *artifactory.ArtifactoryServicesManager) error { - err := fileutils.CreateTempDirPath() + tempDirPath, err := fileutils.CreateTempDir() if err != nil { return err } - defer fileutils.RemoveTempDir() - pwd := &PackageWithDeps{GoModEditMessage: goModEditMessage} + defer fileutils.RemoveTempDir(tempDirPath) + pwd := &PackageWithDeps{GoModEditMessage: goModEditMessage, depsTempDir: tempDirPath} err = pwd.Init() if err != nil { return err @@ -232,11 +233,11 @@ func (pwd *PackageWithDeps) getModPathAndUnzipDependency(path string) (string, e return "", err } // Unzips the zip file into temp - tempDir, err := createDependencyInTemp(pwd.Dependency.GetZipPath()) + err = createDependencyInTemp(pwd.Dependency.GetZipPath(), pwd.depsTempDir) if err != nil { return "", err } - path = pwd.getModPathInTemp(tempDir) + path = pwd.getModPathInTemp(pwd.depsTempDir) return path, err } @@ -362,7 +363,8 @@ func (pwd *PackageWithDeps) setTransitiveDependencies(targetRepo string, graphDe depsWithTrans := &PackageWithDeps{Dependency: dep, regExp: pwd.regExp, cachePath: pwd.cachePath, - GoModEditMessage: pwd.GoModEditMessage} + GoModEditMessage: pwd.GoModEditMessage, + depsTempDir: pwd.depsTempDir} dependencies = append(dependencies, *depsWithTrans) dependenciesMap[name+":"+version] = downloadedFromArtifactory }