Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions pkg/skaffold/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,18 @@ func AbsolutePaths(workspace string, paths []string) []string {

func IsFile(path string) bool {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Exported functions should have a documentation comment according to Go best practices. This provides clarity on the function's behavior, such as whether it follows symlinks and how it handles errors.

Suggested change
func IsFile(path string) bool {
// IsFile returns true if the path exists and is a regular file. Symlinks are followed.
func IsFile(path string) bool {
References
  1. All exported names should have a doc comment. This is a standard practice in Go to improve code maintainability and discoverability. (link)

info, err := os.Stat(path)
// err could be permission-related
return (err == nil || !os.IsNotExist(err)) && info.Mode().IsRegular()
if err != nil {
return false
}
return info.Mode().IsRegular()
}

func IsDir(path string) bool {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Exported functions should have a documentation comment according to Go best practices. This clarifies that the function follows symlinks and returns false on any error.

Suggested change
func IsDir(path string) bool {
// IsDir returns true if the path exists and is a directory. Symlinks are followed.
func IsDir(path string) bool {
References
  1. All exported names should have a doc comment. This is a standard practice in Go to improve code maintainability and discoverability. (link)

info, err := os.Stat(path)
// err could be permission-related
return (err == nil || !os.IsNotExist(err)) && info.IsDir()
if err != nil {
return false
}
return info.IsDir()
}

// IsEmptyDir returns true for empty directories otherwise false
Expand Down
8 changes: 8 additions & 0 deletions pkg/skaffold/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,14 @@ func TestIsFileIsDir(t *testing.T) {
testutil.CheckDeepEqual(t, false, IsDir(filepath.Join(tmpDir.Root(), "nonexistent")))
}

func TestIsFileIsDirStatError(t *testing.T) {
tmpDir := testutil.NewTempDir(t).Touch("file")
pathWithFileAsDir := tmpDir.Path("file/child")

testutil.CheckDeepEqual(t, false, IsFile(pathWithFileAsDir))
testutil.CheckDeepEqual(t, false, IsDir(pathWithFileAsDir))
}

func TestIsURL(t *testing.T) {
testutil.CheckDeepEqual(t, false, IsURL("foo"))
testutil.CheckDeepEqual(t, false, IsURL("http:bar"))
Expand Down