-
Notifications
You must be signed in to change notification settings - Fork 932
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move code.cloudfoundry.org/gofileutils to here
- used to be here many years ago - is now archived so maintaining it here makes more sense - removed the ginkgo v1 out of the couple of tests
- Loading branch information
Showing
30 changed files
with
703 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package fileutils | ||
|
||
import ( | ||
"os" | ||
) | ||
|
||
func IsDirEmpty(dir string) (isEmpty bool, err error) { | ||
dirFile, err := os.Open(dir) | ||
if err != nil { | ||
return | ||
} | ||
|
||
_, readErr := dirFile.Readdirnames(1) | ||
if readErr != nil { | ||
isEmpty = true | ||
} else { | ||
isEmpty = false | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package fileutils | ||
|
||
import ( | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
) | ||
|
||
func Open(path string) (file *os.File, err error) { | ||
err = os.MkdirAll(filepath.Dir(path), os.ModeDir|os.ModePerm) | ||
if err != nil { | ||
return | ||
} | ||
|
||
return os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) | ||
} | ||
|
||
func Create(path string) (file *os.File, err error) { | ||
err = os.MkdirAll(filepath.Dir(path), os.ModeDir|os.ModePerm) | ||
if err != nil { | ||
return | ||
} | ||
|
||
return os.Create(path) | ||
} | ||
|
||
func CopyPathToPath(fromPath, toPath string) (err error) { | ||
srcFileInfo, err := os.Stat(fromPath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if srcFileInfo.IsDir() { | ||
err = os.MkdirAll(toPath, srcFileInfo.Mode()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
files, err := ioutil.ReadDir(fromPath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, file := range files { | ||
err = CopyPathToPath(path.Join(fromPath, file.Name()), path.Join(toPath, file.Name())) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} else { | ||
var dst *os.File | ||
dst, err = Create(toPath) | ||
if err != nil { | ||
return err | ||
} | ||
defer dst.Close() | ||
|
||
dst.Chmod(srcFileInfo.Mode()) | ||
|
||
src, err := os.Open(fromPath) | ||
if err != nil { | ||
return err | ||
} | ||
defer src.Close() | ||
|
||
_, err = io.Copy(dst, src) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
package fileutils_test | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
|
||
"code.cloudfoundry.org/cli/cf/fileutils/fileutils" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("Fileutils File", func() { | ||
var fixturePath = filepath.Clean("../fixtures/fileutils/supervirus.zsh") | ||
var fixtureBytes []byte | ||
|
||
BeforeEach(func() { | ||
var err error | ||
fixtureBytes, err = ioutil.ReadFile(fixturePath) | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) | ||
|
||
Describe("Open", func() { | ||
It("opens an existing file", func() { | ||
fd, err := fileutils.Open(fixturePath) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
fileBytes, err := ioutil.ReadAll(fd) | ||
Expect(err).NotTo(HaveOccurred()) | ||
fd.Close() | ||
|
||
Expect(fileBytes).To(Equal(fixtureBytes)) | ||
}) | ||
|
||
It("creates a non-existing file and all intermediary directories", func() { | ||
fd, err := ioutil.TempFile("", "open_test") | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
_, err = fd.WriteString("Never Gonna Give You Up") | ||
Expect(err).NotTo(HaveOccurred()) | ||
fd.Close() | ||
|
||
fileBytes, err := ioutil.ReadFile(fd.Name()) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(string(fileBytes)).To(Equal("Never Gonna Give You Up")) | ||
}) | ||
}) | ||
|
||
Describe("Create", func() { | ||
It("truncates an existing file", func() { | ||
tmpFile, err := ioutil.TempFile("", "create_test") | ||
Expect(err).NotTo(HaveOccurred()) | ||
_, err = tmpFile.WriteString("Never Gonna Give You Up") | ||
Expect(err).NotTo(HaveOccurred()) | ||
filePath := tmpFile.Name() | ||
tmpFile.Close() | ||
|
||
fd, err := fileutils.Create(filePath) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
fileBytes, err := ioutil.ReadAll(fd) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(len(fileBytes)).To(Equal(0)) | ||
fd.Close() | ||
}) | ||
|
||
It("creates a non-existing file and all intermediary directories", func() { | ||
fd, err := ioutil.TempFile("", "create_test") | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
_, err = fd.WriteString("Never Gonna Let You Down") | ||
Expect(err).NotTo(HaveOccurred()) | ||
fd.Close() | ||
|
||
fileBytes, err := ioutil.ReadFile(fd.Name()) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(string(fileBytes)).To(Equal("Never Gonna Let You Down")) | ||
}) | ||
}) | ||
|
||
Describe("CopyPathToPath", func() { | ||
Describe("when the source is a file", func() { | ||
var destPath string | ||
|
||
BeforeEach(func() { | ||
fd, err := ioutil.TempFile("", "copy_test") | ||
Expect(err).NotTo(HaveOccurred()) | ||
fd.Close() | ||
destPath = fd.Name() | ||
}) | ||
|
||
AfterEach(func() { | ||
os.RemoveAll(destPath) | ||
}) | ||
|
||
It("copies the file contents", func() { | ||
err := fileutils.CopyPathToPath(fixturePath, destPath) | ||
Expect(err).NotTo(HaveOccurred()) | ||
fileBytes, err := ioutil.ReadFile(destPath) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
fixtureBytes, err := ioutil.ReadFile(fixturePath) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(fileBytes).To(Equal(fixtureBytes)) | ||
}) | ||
|
||
It("preserves the file mode", func() { | ||
err := fileutils.CopyPathToPath(fixturePath, destPath) | ||
Expect(err).NotTo(HaveOccurred()) | ||
fileInfo, err := os.Stat(destPath) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
expectedFileInfo, err := os.Stat(fixturePath) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
Expect(fileInfo.Mode()).To(Equal(expectedFileInfo.Mode())) | ||
}) | ||
}) | ||
|
||
Describe("when the source is a directory", func() { | ||
var dirPath, destPath string | ||
|
||
BeforeEach(func() { | ||
dirPath = filepath.Join(filepath.Dir(fixturePath), "some-dir") | ||
destPath = filepath.Join(filepath.Dir(fixturePath), "some-other-dir") | ||
}) | ||
|
||
AfterEach(func() { | ||
os.RemoveAll(destPath) | ||
}) | ||
|
||
It("creates a directory at the destination path", func() { | ||
err := fileutils.CopyPathToPath(dirPath, destPath) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
fileInfo, err := os.Stat(destPath) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(fileInfo.IsDir()).To(BeTrue()) | ||
}) | ||
|
||
It("copies all of the files from the src directory", func() { | ||
err := fileutils.CopyPathToPath(dirPath, destPath) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
fileInfo, err := os.Stat(path.Join(destPath, "some-file")) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(fileInfo.IsDir()).To(BeFalse()) | ||
}) | ||
|
||
It("preserves the directory's mode", func() { | ||
err := fileutils.CopyPathToPath(dirPath, destPath) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
fileInfo, err := os.Stat(destPath) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
expectedFileInfo, err := os.Stat(dirPath) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
Expect(fileInfo.Mode()).To(Equal(expectedFileInfo.Mode())) | ||
}) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.