Skip to content

Commit

Permalink
Refactor file path validation
Browse files Browse the repository at this point in the history
Small refactor to turn file/dir path validation more explicit.

Signed-off-by: Luis Rascao <luis.rascao@gmail.com>
  • Loading branch information
lrascao committed Nov 21, 2023
1 parent a339bdf commit 4fe6978
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
6 changes: 3 additions & 3 deletions pkg/runfileconfig/run_file_config_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func (a *RunFileConfig) resolvePathToAbsAndValidate(baseDir string, paths ...*st
return err
}
*path = absPath
if err = utils.ValidateFilePath(*path); err != nil {
if err = utils.ValidatePath(*path); err != nil {
return err
}
}
Expand Down Expand Up @@ -264,7 +264,7 @@ func (a *RunFileConfig) resolveResourcesFilePath(app *App) error {
return nil
}
localResourcesDir := filepath.Join(app.AppDirPath, standalone.DefaultDaprDirName, standalone.DefaultResourcesDirName)
if err := utils.ValidateFilePath(localResourcesDir); err == nil {
if err := utils.ValidatePath(localResourcesDir); err == nil {
app.ResourcesPaths = []string{localResourcesDir}
} else if len(a.Common.ResourcesPaths) > 0 {
app.ResourcesPaths = append(app.ResourcesPaths, a.Common.ResourcesPaths...)
Expand All @@ -285,7 +285,7 @@ func (a *RunFileConfig) resolveConfigFilePath(app *App) error {
return nil
}
localConfigFile := filepath.Join(app.AppDirPath, standalone.DefaultDaprDirName, standalone.DefaultConfigFileName)
if err := utils.ValidateFilePath(localConfigFile); err == nil {
if err := utils.ValidatePath(localConfigFile); err == nil {
app.ConfigFile = localConfigFile
} else if len(strings.TrimSpace(a.Common.ConfigFile)) > 0 {
app.ConfigFile = a.Common.ConfigFile
Expand Down
17 changes: 10 additions & 7 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,13 +351,16 @@ func GetVersionAndImageVariant(imageTag string) (string, string) {
return imageTag, ""
}

// Returns true if the given file path is valid.
func ValidateFilePath(filePath string) error {
if filePath != "" {
if _, err := os.Stat(filePath); err != nil {
return fmt.Errorf("error in getting the file info for %s: %w", filePath, err)
}
// Returns no error if the given file/directory path is valid.
func ValidatePath(path string) error {
if path == "" {
return nil
}

if _, err := os.Stat(path); err != nil {
return fmt.Errorf("error in getting the file info for %s: %w", path, err)
}

return nil
}

Expand Down Expand Up @@ -402,7 +405,7 @@ func ReadFile(filePath string) ([]byte, error) {
// FindFileInDir finds and returns the path of the given file name in the given directory.
func FindFileInDir(dirPath, fileName string) (string, error) {
filePath := filepath.Join(dirPath, fileName)
if err := ValidateFilePath(filePath); err != nil {
if err := ValidatePath(filePath); err != nil {
return "", fmt.Errorf("error in validating the file path %q: %w", filePath, err)
}
return filePath, nil
Expand Down
13 changes: 10 additions & 3 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,12 @@ func TestGetVersionAndImageVariant(t *testing.T) {
}
}

func TestValidateFilePaths(t *testing.T) {
func TestValidatePaths(t *testing.T) {
dirName := createTempDir(t, "test_validate_paths")
defer cleanupTempDir(t, dirName)
validFile := createTempFile(t, dirName, "valid_test_file.yaml")
t.Cleanup(func() {
cleanupTempDir(t, dirName)
})
testcases := []struct {
name string
input string
Expand All @@ -194,6 +196,11 @@ func TestValidateFilePaths(t *testing.T) {
input: validFile,
expectedErr: false,
},
{
name: "valid directory path",
input: dirName,
expectedErr: false,
},
{
name: "invalid file path",
input: "invalid_file_path",
Expand All @@ -205,7 +212,7 @@ func TestValidateFilePaths(t *testing.T) {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
actual := ValidateFilePath(tc.input)
actual := ValidatePath(tc.input)
assert.Equal(t, tc.expectedErr, actual != nil)
})
}
Expand Down

0 comments on commit 4fe6978

Please sign in to comment.