Skip to content
Merged
6 changes: 5 additions & 1 deletion cli/cmd/download_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ func (c *DownloadPackageCmd) DownloadBuild(p portal.Portal, build portal.Build,
return fmt.Errorf("failed to find artifact in package: %w", err)
}

fullFilename := strings.ReplaceAll(build.Version, "/", "-") + "-" + filename
shortHash := build.Hash
if len(shortHash) > 10 {
shortHash = shortHash[:10]
}
fullFilename := strings.ReplaceAll(build.Version, "/", "-") + "-" + shortHash + "-" + filename
out, err := c.FileWriter.OpenAppend(fullFilename)
if err != nil {
out, err = c.FileWriter.Create(fullFilename)
Expand Down
40 changes: 36 additions & 4 deletions cli/cmd/download_package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var _ = Describe("DownloadPackages", func() {
c cmd.DownloadPackageCmd
filename string
version string
hash string
build portal.Build
mockPortal *portal.MockPortal
mockFileWriter *util.MockFileIO
Expand All @@ -30,6 +31,7 @@ var _ = Describe("DownloadPackages", func() {
BeforeEach(func() {
filename = "installer.tar.gz"
version = "codesphere-1.42.0"
hash = "abc1234567"
mockPortal = portal.NewMockPortal(GinkgoT())
mockFileWriter = util.NewMockFileIO(GinkgoT())
})
Expand All @@ -44,6 +46,7 @@ var _ = Describe("DownloadPackages", func() {
}
build = portal.Build{
Version: version,
Hash: hash,
Artifacts: []portal.Artifact{
{Filename: filename},
{Filename: "otherFilename.tar.gz"},
Expand Down Expand Up @@ -169,35 +172,64 @@ var _ = Describe("DownloadPackages", func() {
It("Downloads the correct artifact to the correct output file", func() {
expectedBuildToDownload := portal.Build{
Version: version,
Hash: hash,
Artifacts: []portal.Artifact{
{Filename: filename},
},
}

fakeFile := os.NewFile(uintptr(0), filename)
mockFileWriter.EXPECT().OpenAppend(version+"-"+filename).Return(fakeFile, nil)
mockFileWriter.EXPECT().Open(version+"-"+filename).Return(fakeFile, nil)
mockFileWriter.EXPECT().OpenAppend(version+"-"+hash+"-"+filename).Return(fakeFile, nil)
mockFileWriter.EXPECT().Open(version+"-"+hash+"-"+filename).Return(fakeFile, nil)
mockPortal.EXPECT().DownloadBuildArtifact(portal.CodesphereProduct, expectedBuildToDownload, mock.Anything, 0, false).Return(nil)
mockPortal.EXPECT().VerifyBuildArtifactDownload(mock.Anything, expectedBuildToDownload).Return(nil)
err := c.DownloadBuild(mockPortal, build, filename)
Expect(err).NotTo(HaveOccurred())
})

It("Truncates long hash to 10 characters in filename", func() {
longHash := "abc1234567890defghij"
buildWithLongHash := portal.Build{
Version: version,
Hash: longHash,
Artifacts: []portal.Artifact{
{Filename: filename},
{Filename: "otherFilename.tar.gz"},
},
}
expectedBuildToDownload := portal.Build{
Version: version,
Hash: longHash,
Artifacts: []portal.Artifact{
{Filename: filename},
},
}

fakeFile := os.NewFile(uintptr(0), filename)
mockFileWriter.EXPECT().OpenAppend(version+"-abc1234567-"+filename).Return(fakeFile, nil)
mockFileWriter.EXPECT().Open(version+"-abc1234567-"+filename).Return(fakeFile, nil)
mockPortal.EXPECT().DownloadBuildArtifact(portal.CodesphereProduct, expectedBuildToDownload, mock.Anything, 0, false).Return(nil)
mockPortal.EXPECT().VerifyBuildArtifactDownload(mock.Anything, expectedBuildToDownload).Return(nil)
err := c.DownloadBuild(mockPortal, buildWithLongHash, filename)
Expect(err).NotTo(HaveOccurred())
})

Context("Version contains a slash", func() {
BeforeEach(func() {
version = "other/version/v1.42.0"
})
It("Downloads the correct artifact to the correct output file", func() {
expectedBuildToDownload := portal.Build{
Version: version,
Hash: hash,
Artifacts: []portal.Artifact{
{Filename: filename},
},
}

fakeFile := os.NewFile(uintptr(0), filename)
mockFileWriter.EXPECT().OpenAppend("other-version-v1.42.0-"+filename).Return(fakeFile, nil)
mockFileWriter.EXPECT().Open("other-version-v1.42.0-"+filename).Return(fakeFile, nil)
mockFileWriter.EXPECT().OpenAppend("other-version-v1.42.0-"+hash+"-"+filename).Return(fakeFile, nil)
mockFileWriter.EXPECT().Open("other-version-v1.42.0-"+hash+"-"+filename).Return(fakeFile, nil)
mockPortal.EXPECT().DownloadBuildArtifact(portal.CodesphereProduct, expectedBuildToDownload, mock.Anything, 0, false).Return(nil)
mockPortal.EXPECT().VerifyBuildArtifactDownload(mock.Anything, expectedBuildToDownload).Return(nil)
err := c.DownloadBuild(mockPortal, build, filename)
Expand Down
1 change: 1 addition & 0 deletions internal/installer/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func (p *Package) Extract(force bool) error {
if err != nil {
return fmt.Errorf("failed to figure out if package %s is already extracted in %s: %w", p.Filename, workDir, err)
}

if alreadyExtracted && !force {
log.Println("Skipping extraction, package already unpacked. Use force option to overwrite.")
return nil
Expand Down