Skip to content

Commit

Permalink
fn: add ensuredirexists to fn package
Browse files Browse the repository at this point in the history
  • Loading branch information
Abdulkbk committed Feb 5, 2025
1 parent 6bf895a commit 4a68445
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
25 changes: 25 additions & 0 deletions fn/io.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fn

import (
"fmt"
"os"
)

Expand Down Expand Up @@ -43,3 +44,27 @@ func WriteFileRemove(name string, data []byte, perm os.FileMode) error {

return err
}

// EnsureDirExists creates a directory if it doesn't exist and also handles
// symlink-related errors with user-friendly messages. It creates all necessary
// parent directories with the specified permissions.
func EnsureDirExists(dir string, perm os.FileMode) error {
err := os.MkdirAll(dir, perm)
if err != nil {
// Show a nicer error message if it's because a symlink
// is linked to a directory that does not exist
// (probably because it's not mounted).
if e, ok := err.(*os.PathError); ok && os.IsExist(err) {
link, lerr := os.Readlink(e.Path)
if lerr == nil {
return fmt.Errorf("is symlink %s -> %s "+
"mounted?", e.Path, link)
}
}

return fmt.Errorf("failed to create directory '%s': %v",
dir, err)
}

return nil
}
77 changes: 77 additions & 0 deletions fn/io_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fn

import (
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -94,3 +95,79 @@ func ensureFileContents(t *testing.T, filename string, data string) {

require.Equal(t, data, string(contents))
}

// TestEnsureDirExists verifies the behavior of EnsureDirExists function in
// various scenarios:
// - Creating a new directory when it doesn't exist
// - Handling an already existing directory
// - Dealing with symlinks pointing to non-existent directories
// - Handling invalid paths
// The test uses a temporary directory and runs multiple test cases to ensure
// proper directory creation, permission settings, and error handling.
func TestEnsureDirExists(t *testing.T) {
t.Parallel()

tempDir := t.TempDir()

tests := []struct {
name string
setup func() string
wantError bool
}{
{
name: "create directory",
setup: func() string {
return filepath.Join(tempDir, "testdir")
},
wantError: false,
},
{
name: "existing directory",
setup: func() string {
dir := filepath.Join(tempDir, "testdir")
err := os.Mkdir(dir, 0700)
require.NoError(t, err)
return dir
},
wantError: false,
},
{
name: "symlink to non-existent directory",
setup: func() string {
dir := filepath.Join(tempDir, "testdir")
symlink := filepath.Join(tempDir, "symlink")
err := os.Symlink(dir, symlink)
require.NoError(t, err)
return symlink
},
wantError: true,
},
{
name: "invalid path",
setup: func() string {
return string([]byte{0})
},
wantError: true,
},
}

for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
dir := tc.setup()
defer os.RemoveAll(dir)

err := EnsureDirExists(dir, 0700)
if tc.wantError {
require.Error(t, err)
return
}

require.NoError(t, err)

info, err := os.Stat(dir)
require.NoError(t, err)
require.True(t, info.IsDir())
})
}
}

0 comments on commit 4a68445

Please sign in to comment.