Skip to content

Commit

Permalink
Revise error msg for create new document with empty path or filename
Browse files Browse the repository at this point in the history
  • Loading branch information
wedaly committed Dec 23, 2023
1 parent 496e88a commit 9959b5b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
9 changes: 7 additions & 2 deletions file/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@ import (
// after the check, or the user might not have permission to create the file.
func ValidateCreate(path string) error {
dir, filename := filepath.Split(path)
dir = filepath.Clean(dir)

// If the filename is empty, return an error.
if filename == "" {
return fmt.Errorf("File name cannot be empty")
if dir == "" {
return fmt.Errorf("File path is empty")
} else {
return fmt.Errorf("File path must end with a file name")
}
}

dir = filepath.Clean(dir)

// If the directory doesn't exist, return an error.
if f, err := os.Stat(dir); err != nil {
if os.IsNotExist(err) {
Expand Down
9 changes: 7 additions & 2 deletions file/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ func TestValidateCreateSuccess(t *testing.T) {
require.NoError(t, err)
}

func TestValidateCreateEmptyFilename(t *testing.T) {
func TestValidateCreateEmptyPath(t *testing.T) {
err := ValidateCreate("")
require.EqualError(t, err, "File name cannot be empty")
require.EqualError(t, err, "File path is empty")
}

func TestValidateCreatePathEndingWithDirectory(t *testing.T) {
err := ValidateCreate("foobar/")
require.EqualError(t, err, "File path must end with a file name")
}

func TestValidateCreateDirectoryDoesNotExist(t *testing.T) {
Expand Down

0 comments on commit 9959b5b

Please sign in to comment.