Skip to content

Commit

Permalink
Move code.cloudfoundry.org/gofileutils to here
Browse files Browse the repository at this point in the history
- 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
moleske committed Sep 15, 2023
1 parent 80e4086 commit 9e7aaec
Show file tree
Hide file tree
Showing 30 changed files with 703 additions and 16 deletions.
2 changes: 1 addition & 1 deletion actor/pluginaction/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (

"code.cloudfoundry.org/cli/actor/actionerror"
"code.cloudfoundry.org/cli/api/plugin"
"code.cloudfoundry.org/cli/cf/fileutils/fileutils"
"code.cloudfoundry.org/cli/util/configv3"
"code.cloudfoundry.org/cli/util/generic"
"code.cloudfoundry.org/gofileutils/fileutils"
)

//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . PluginMetadata
Expand Down
2 changes: 1 addition & 1 deletion cf/actors/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"code.cloudfoundry.org/cli/cf/api/applicationbits"
"code.cloudfoundry.org/cli/cf/api/resources"
"code.cloudfoundry.org/cli/cf/appfiles"
"code.cloudfoundry.org/cli/cf/fileutils/fileutils"
. "code.cloudfoundry.org/cli/cf/i18n"
"code.cloudfoundry.org/cli/cf/models"
"code.cloudfoundry.org/gofileutils/fileutils"
)

const windowsPathPrefix = `\\?\`
Expand Down
2 changes: 1 addition & 1 deletion cf/api/applicationbits/application_bits.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package applicationbits

import (
"bytes"
"code.cloudfoundry.org/cli/cf/fileutils/fileutils"
"encoding/json"
"fmt"
"io"
Expand All @@ -14,7 +15,6 @@ import (
"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
. "code.cloudfoundry.org/cli/cf/i18n"
"code.cloudfoundry.org/cli/cf/net"
"code.cloudfoundry.org/gofileutils/fileutils"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion cf/api/buildpack_bits.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ import (
"code.cloudfoundry.org/cli/cf/appfiles"
"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
"code.cloudfoundry.org/cli/cf/errors"
"code.cloudfoundry.org/cli/cf/fileutils/fileutils"
. "code.cloudfoundry.org/cli/cf/i18n"
"code.cloudfoundry.org/cli/cf/models"
"code.cloudfoundry.org/cli/cf/net"
"code.cloudfoundry.org/cli/util"
"code.cloudfoundry.org/gofileutils/fileutils"
)

//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . BuildpackBitsRepository
Expand Down
2 changes: 1 addition & 1 deletion cf/appfiles/app_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"path/filepath"
"runtime"

"code.cloudfoundry.org/cli/cf/fileutils/fileutils"
"code.cloudfoundry.org/cli/cf/models"
"code.cloudfoundry.org/gofileutils/fileutils"
securejoin "github.com/cyphar/filepath-securejoin"
)

Expand Down
2 changes: 1 addition & 1 deletion cf/appfiles/app_files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"code.cloudfoundry.org/cli/cf/appfiles"
uuid "github.com/nu7hatch/gouuid"

"code.cloudfoundry.org/cli/cf/fileutils/fileutils"
"code.cloudfoundry.org/cli/cf/models"
"code.cloudfoundry.org/gofileutils/fileutils"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down
2 changes: 1 addition & 1 deletion cf/appfiles/zipper.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"runtime"

"code.cloudfoundry.org/cli/cf/errors"
"code.cloudfoundry.org/gofileutils/fileutils"
"code.cloudfoundry.org/cli/cf/fileutils/fileutils"
securejoin "github.com/cyphar/filepath-securejoin"
)

Expand Down
2 changes: 1 addition & 1 deletion cf/appfiles/zipper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"strings"

. "code.cloudfoundry.org/cli/cf/appfiles"
"code.cloudfoundry.org/gofileutils/fileutils"
"code.cloudfoundry.org/cli/cf/fileutils/fileutils"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down
2 changes: 1 addition & 1 deletion cf/commands/curl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
"code.cloudfoundry.org/cli/cf/api/apifakes"
"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
"code.cloudfoundry.org/cli/cf/errors"
"code.cloudfoundry.org/cli/cf/fileutils/fileutils"
"code.cloudfoundry.org/cli/cf/requirements"
"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
testcmd "code.cloudfoundry.org/cli/cf/util/testhelpers/commands"
testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration"
testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal"
"code.cloudfoundry.org/gofileutils/fileutils"

"code.cloudfoundry.org/cli/cf/commandregistry"
"code.cloudfoundry.org/cli/cf/trace"
Expand Down
2 changes: 1 addition & 1 deletion cf/commands/plugin/install_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import (
"code.cloudfoundry.org/cli/cf/commandregistry"
"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
"code.cloudfoundry.org/cli/cf/configuration/pluginconfig"
"code.cloudfoundry.org/cli/cf/fileutils/fileutils"
"code.cloudfoundry.org/cli/cf/flags"
. "code.cloudfoundry.org/cli/cf/i18n"
"code.cloudfoundry.org/cli/cf/requirements"
"code.cloudfoundry.org/cli/cf/terminal"
"code.cloudfoundry.org/cli/cf/util/downloader"
"code.cloudfoundry.org/cli/plugin"
"code.cloudfoundry.org/cli/util"
"code.cloudfoundry.org/gofileutils/fileutils"

pluginRPCService "code.cloudfoundry.org/cli/plugin/rpc"
)
Expand Down
2 changes: 1 addition & 1 deletion cf/commands/plugin/uninstall_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"code.cloudfoundry.org/cli/cf/configuration"
"code.cloudfoundry.org/cli/cf/configuration/confighelpers"
"code.cloudfoundry.org/cli/cf/configuration/pluginconfig"
"code.cloudfoundry.org/cli/cf/fileutils/fileutils"
"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
"code.cloudfoundry.org/gofileutils/fileutils"

testcmd "code.cloudfoundry.org/cli/cf/util/testhelpers/commands"
testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal"
Expand Down
20 changes: 20 additions & 0 deletions cf/fileutils/fileutils/dir_utils.go
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
}
74 changes: 74 additions & 0 deletions cf/fileutils/fileutils/file_utils.go
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
}
165 changes: 165 additions & 0 deletions cf/fileutils/fileutils/file_utils_test.go
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()))
})
})
})
})
Loading

0 comments on commit 9e7aaec

Please sign in to comment.