func withFakeHome(t *testing.T, callback func()) { oldHome := os.Getenv("HOME") oldHomePath := os.Getenv("HOMEPATH") oldHomeDrive := os.Getenv("HOMEDRIVE") defer func() { os.Setenv("HOMEDRIVE", oldHomeDrive) os.Setenv("HOMEPATH", oldHomePath) os.Setenv("HOME", oldHome) }() defer func() { NewConfigurationDiskRepository().Delete() }() fileutils.TempDir("test-config", func(dir string, err error) { os.Setenv("HOME", dir) volumeName := filepath.VolumeName(dir) if volumeName != "" { relativePath := strings.Replace(dir, volumeName, "", 1) os.Setenv("HOMEPATH", relativePath) os.Setenv("HOMEDRIVE", volumeName) } callback() }) }
func withFakeHome(callback func(dirPath string)) { fileutils.TempDir("test-config", func(dir string, err error) { if err != nil { Fail("Couldn't create tmp file") } callback(filepath.Join(dir, ".cf", "config.json")) }) }
func TestZipWithEmptyDir(t *testing.T) { fileutils.TempFile("zip_test", func(zipFile *os.File, err error) { fileutils.TempDir("zip_test", func(emptyDir string, err error) { zipper := ApplicationZipper{} err = zipper.Zip(emptyDir, zipFile) assert.Error(t, err) assert.Equal(t, err.Error(), "Directory is empty") }) }) }
func (repo CloudControllerApplicationBitsRepository) sourceDir(appDir string, cb func(sourceDir string, err error)) { // If appDir is a zip, first extract it to a temporary directory if repo.zipper.IsZipFile(appDir) { fileutils.TempDir("unzipped-app", func(tmpDir string, err error) { err = repo.extractZip(appDir, tmpDir) cb(tmpDir, err) }) } else { cb(appDir, nil) } }
func (repo CloudControllerApplicationBitsRepository) UploadApp(appGuid string, appDir string, cb func(path string, zipSize, fileCount uint64)) (apiResponse net.ApiResponse) { fileutils.TempDir("apps", func(uploadDir string, err error) { if err != nil { apiResponse = net.NewApiResponseWithMessage(err.Error()) return } var presentResourcesJson []byte repo.sourceDir(appDir, func(sourceDir string, sourceErr error) { if sourceErr != nil { err = sourceErr return } presentResourcesJson, err = repo.copyUploadableFiles(sourceDir, uploadDir) }) if err != nil { apiResponse = net.NewApiResponseWithMessage("%s", err) return } fileutils.TempFile("uploads", func(zipFile *os.File, err error) { if err != nil { apiResponse = net.NewApiResponseWithMessage("%s", err.Error()) return } err = repo.zipper.Zip(uploadDir, zipFile) if err != nil { apiResponse = net.NewApiResponseWithError("Error zipping application", err) return } stat, err := zipFile.Stat() if err != nil { apiResponse = net.NewApiResponseWithError("Error zipping application", err) return } cb(appDir, uint64(stat.Size()), cf.CountFiles(uploadDir)) apiResponse = repo.uploadBits(appGuid, zipFile, presentResourcesJson) if apiResponse.IsNotSuccessful() { return } }) }) return }
func (repo CloudControllerApplicationBitsRepository) sourceDir(appDir string, cb func(sourceDir string, err error)) { // If appDir is a zip, first extract it to a temporary directory if !repo.fileIsZip(appDir) { cb(appDir, nil) return } fileutils.TempDir("unzipped-app", func(tmpDir string, err error) { if err != nil { cb("", err) return } err = repo.extractZip(appDir, tmpDir) cb(tmpDir, err) }) }
func (repo CloudControllerApplicationBitsRepository) UploadApp(app cf.Application, appDir string) (apiResponse net.ApiResponse) { fileutils.TempDir("apps", func(uploadDir string, err error) { if err != nil { apiResponse = net.NewApiResponseWithMessage(err.Error()) return } var resourcesJson []byte repo.sourceDir(appDir, func(sourceDir string, sourceErr error) { if sourceErr != nil { err = sourceErr return } resourcesJson, err = repo.copyUploadableFiles(sourceDir, uploadDir) }) if err != nil { apiResponse = net.NewApiResponseWithMessage(err.Error()) return } fileutils.TempFile("uploads", func(zipFile *os.File, err error) { if err != nil { apiResponse = net.NewApiResponseWithMessage(err.Error()) return } err = repo.zipper.Zip(uploadDir, zipFile) if err != nil { apiResponse = net.NewApiResponseWithError("Error zipping application", err) return } apiResponse = repo.uploadBits(app, zipFile, resourcesJson) if apiResponse.IsNotSuccessful() { return } }) }) return }
Expect(err).NotTo(HaveOccurred()) zipper := ApplicationZipper{} err = zipper.Zip(filepath.Join(dir, "../fixtures/application.jar"), zipFile) Expect(err).NotTo(HaveOccurred()) Expect(fileToString(zipFile)).To(Equal("This is an application jar file\n")) }) }) It("TestZipWithInvalidFile", func() { fileutils.TempFile("zip_test", func(zipFile *os.File, err error) { zipper := ApplicationZipper{} err = zipper.Zip("/a/bogus/directory", zipFile) Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("open /a/bogus/directory")) }) }) It("TestZipWithEmptyDir", func() { fileutils.TempFile("zip_test", func(zipFile *os.File, err error) { fileutils.TempDir("zip_test", func(emptyDir string, err error) { zipper := ApplicationZipper{} err = zipper.Zip(emptyDir, zipFile) Expect(err).To(HaveOccurred()) Expect(err.Error()).To(Equal("Directory is empty")) }) }) }) })