Skip to content

Commit 16c7308

Browse files
authored
Added function to list files by extension (#4)
1 parent 399eb5b commit 16c7308

File tree

5 files changed

+34
-2
lines changed

5 files changed

+34
-2
lines changed

xcollection/xcollection_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
func TestStringBoolMapKeys(t *testing.T) {
13-
13+
t.Parallel()
1414
keys := MapKeys(map[string]bool{
1515
"qwe": true,
1616
"123": true,
@@ -20,6 +20,7 @@ func TestStringBoolMapKeys(t *testing.T) {
2020
}
2121

2222
func TestRandomSelection(t *testing.T) {
23+
t.Parallel()
2324
rand.Seed(time.Now().Unix())
2425
element := Random([]string{"1", "2", "3", "4", "5", "6"})
2526
log.Printf("Random element: %s", element)

xenv/xenv_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
)
88

99
func TestFetchingCurrentUser(t *testing.T) {
10-
10+
t.Parallel()
1111
user := Env("USER", "")
1212

1313
assert.NotEmpty(t, user)

xfiles/xfiles.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package xfiles
22

33
import (
44
"bufio"
5+
"io/fs"
56
"os"
7+
"path/filepath"
68
"strings"
79

810
"github.com/unidev-platform/golang-core/xcollection"
@@ -29,3 +31,21 @@ func Distinct(path string) ([]string, error) {
2931
return xcollection.MapKeys(linesMap), scanner.Err()
3032

3133
}
34+
35+
// Find - extract files matching extension
36+
func Find(dir string, extension string) ([]string, error) {
37+
var files []string
38+
err := filepath.WalkDir(dir, func(s string, d fs.DirEntry, e error) error {
39+
if e != nil {
40+
return e
41+
}
42+
if filepath.Ext(d.Name()) == extension {
43+
files = append(files, s)
44+
}
45+
return nil
46+
})
47+
if err != nil {
48+
return nil, err
49+
}
50+
return files, nil
51+
}

xfiles/xfiles_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,19 @@ import (
77
)
88

99
func TestFileLinesExtraction(t *testing.T) {
10+
t.Parallel()
1011
lines, err := Distinct("distinct_file_lines_test.txt")
1112
if err != nil {
1213
panic(err)
1314
}
1415
assert.Equal(t, 6, len(lines))
1516
}
17+
18+
func TestListFiles(t *testing.T) {
19+
t.Parallel()
20+
files, err := Find(".", ".txt")
21+
if err != nil {
22+
panic(err)
23+
}
24+
assert.Equal(t, 1, len(files))
25+
}

xstring/xstrings_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
)
88

99
func TestStringExtractions(t *testing.T) {
10+
t.Parallel()
1011
items := Between(" 1qwe2 666 1xxx2 000", "1", "2")
1112

1213
assert.Equal(t, 2, len(items))

0 commit comments

Comments
 (0)