Skip to content
This repository has been archived by the owner on Dec 29, 2024. It is now read-only.

Commit

Permalink
refactor: renamed all getters in code
Browse files Browse the repository at this point in the history
  • Loading branch information
Wittano committed Dec 17, 2023
1 parent 56b7674 commit 9b55226
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 25 deletions.
12 changes: 6 additions & 6 deletions path/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"path/filepath"
)

func GetPathsFromPattern(src string) ([]string, error) {
reg, err := GetPathRegex(src)
func PathsFromPattern(src string) ([]string, error) {
reg, err := Regex(src)
if err != nil {
return nil, err
}
Expand All @@ -33,15 +33,15 @@ func GetPathsFromPattern(src string) ([]string, error) {
return paths, nil
}

func GetPathFromPatternRecursive(path string) ([]string, error) {
func PathsFromPatternRecursive(path string) ([]string, error) {
dir, pattern := filepath.Split(path)
if !isFilePathIsRegex(pattern) {
dir = path
}

files, err := os.ReadDir(dir)
if err != nil {
return GetPathsFromPattern(dir)
return PathsFromPattern(dir)
}

var (
Expand All @@ -53,9 +53,9 @@ func GetPathFromPatternRecursive(path string) ([]string, error) {
var path []string

if f.IsDir() {
path, err = GetPathFromPatternRecursive(dir + f.Name())
path, err = PathsFromPatternRecursive(dir + f.Name())
} else {
path, err = GetPathsFromPattern(filepath.Join(dir, f.Name()))
path, err = PathsFromPattern(filepath.Join(dir, f.Name()))
}

if err != nil {
Expand Down
24 changes: 12 additions & 12 deletions path/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,40 @@ import (
"testing"
)

func TestGetPathsFromRegex(t *testing.T) {
func TestPathsFromRegex(t *testing.T) {
exp := test.CreateTempFile(t)

paths, err := GetPathsFromPattern(exp)
paths, err := PathsFromPattern(exp)
if err == nil && len(paths) != 1 && paths[0] == exp {
t.Fatalf("Failed got paths. Expected 1, acually %d", len(paths))
}
}

func TestGetPathsFromRegexButRegexStartFromStar(t *testing.T) {
func TestPathsFromRegexButRegexStartFromStar(t *testing.T) {
p := test.CreateTempFile(t)
exp := strings.Replace(p, "test", "*est", 1)

paths, err := GetPathsFromPattern(exp)
paths, err := PathsFromPattern(exp)
if err == nil && len(paths) != 1 && paths[0] == exp {
t.Fatalf("Failed got paths. Expected 1, acually %d", len(paths))
}
}

func TestGetPathsFromRegexButFunctionReturnNil(t *testing.T) {
func TestPathsFromRegexButFunctionReturnNil(t *testing.T) {
p := test.CreateTempFile(t)
exp := strings.Replace(p, "test", "tset", 1)

paths, err := GetPathsFromPattern(exp)
paths, err := PathsFromPattern(exp)
if err != nil || paths != nil || len(paths) != 0 {
t.Fatalf("Failed got paths. Expected 1, acually %d", len(paths))
}
}

func TestGetPathsFromRegexRecursive(t *testing.T) {
func TestPathsFromRegexRecursive(t *testing.T) {
_, expFilename := test.CreateNestedTempDirWithFiles(t, "test.mp4")
dir := filepath.Dir(expFilename)

paths, err := GetPathFromPatternRecursive(dir + "*.mp4*")
paths, err := PathsFromPatternRecursive(dir + "*.mp4*")
if err != nil || len(paths) != 1 {
t.Fatalf("Failed got paths. Expected 1, acually %d. Error: %s", len(paths), err)
}
Expand All @@ -51,11 +51,11 @@ func TestGetPathsFromRegexRecursive(t *testing.T) {
}
}

func TestGetPathsFromRegexRecursiveButFunctionReturnNil(t *testing.T) {
func TestPathsFromRegexRecursiveButFunctionReturnNil(t *testing.T) {
expDir, _ := test.CreateNestedTempDirWithFiles(t, "test")
dir := strings.Replace(expDir, "test", "tset", 1)

paths, err := GetPathFromPatternRecursive(dir)
paths, err := PathsFromPatternRecursive(dir)
if err != nil {
t.Fatalf(err.Error())
}
Expand All @@ -65,7 +65,7 @@ func TestGetPathsFromRegexRecursiveButFunctionReturnNil(t *testing.T) {
}
}

func BenchmarkGetPathsFromRegex(b *testing.B) {
func BenchmarkPathsFromRegex(b *testing.B) {
dir := b.TempDir()
pattern := "test"

Expand All @@ -79,7 +79,7 @@ func BenchmarkGetPathsFromRegex(b *testing.B) {
exp := file.Name()

for i := 0; i < b.N; i++ {
GetPathsFromPattern(exp)
PathsFromPattern(exp)
}

b.ReportAllocs()
Expand Down
4 changes: 2 additions & 2 deletions path/regex.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"regexp"
)

func GetPathRegex(src string) (*regexp.Regexp, error) {
func Regex(src string) (*regexp.Regexp, error) {
pattern := filepath.Base(src)

reg, err := regexp.Compile("\\*")
reg, err := regexp.Compile(`\*`)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions path/regex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type argument struct {
out string
}

func TestGetPathRegex(t *testing.T) {
func TestPathRegex(t *testing.T) {
paths := []argument{
{"/path/to/example*", "example.txt"},
{"/path/to/example*", "example1234123412.txt"},
Expand All @@ -22,7 +22,7 @@ func TestGetPathRegex(t *testing.T) {

for _, p := range paths {
t.Run(p.in, func(t *testing.T) {
res, err := GetPathRegex(p.in)
res, err := Regex(p.in)
if err != nil {
t.Fatalf("Failed to extract regex from path. %s", err)
}
Expand Down
4 changes: 2 additions & 2 deletions setting/config_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ func (d Directory) RealPaths() (paths []string, err error) {

for _, exp := range d.Src {
if d.Recursive {
paths, err = path.GetPathFromPatternRecursive(exp)
paths, err = path.PathsFromPatternRecursive(exp)
} else {
paths, err = path.GetPathsFromPattern(exp)
paths, err = path.PathsFromPattern(exp)
}

if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion setting/config_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestFailedLoadingConfig(t *testing.T) {
}
}

func TestGetTrashDir(t *testing.T) {
func TestTrashDir(t *testing.T) {
tempFile := test.CreateTempFile(t)

d := Directory{
Expand Down

0 comments on commit 9b55226

Please sign in to comment.