Skip to content

Commit

Permalink
Add file filter to ignore hidden files (prefixed with a .)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiante committed Aug 14, 2024
1 parent 6a02b72 commit 404ede7
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 4 deletions.
30 changes: 26 additions & 4 deletions cmd/squeeze/benchmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log/slog"
"os"
"path/filepath"
"strings"
"time"

squeezegoclient "github.com/dexpro-solutions-gmbh/squeeze-go-client"
Expand Down Expand Up @@ -55,7 +56,9 @@ func newBenchmarkCmd() *cobra.Command {
if err != nil {
return err
}
if d.IsDir() {

keep := filterFile(path, d)
if !keep {
return nil
}

Expand All @@ -66,7 +69,7 @@ func newBenchmarkCmd() *cobra.Command {
defer func(file *os.File) {
err := file.Close()
if err != nil {
slog.Error("Closing file failed", "err", err)
slog.Error("Closing path failed", "err", err)
}
}(file)

Expand All @@ -77,11 +80,11 @@ func newBenchmarkCmd() *cobra.Command {
// Ignore the upload error and continue with next file - this allows the benchmark
// to still be run. That is most often what we want since Squeeze may decide
// to reject uploads of invalid files / file types.
slog.Error("Failed to upload document", "file", fileName, "err", resErr)
slog.Error("Failed to upload document", "path", fileName, "err", resErr)
return nil
}

slog.Info("File uploaded", "file", fileName)
slog.Info("File uploaded", "path", fileName)

documentCount += 1

Expand Down Expand Up @@ -135,6 +138,25 @@ func newBenchmarkCmd() *cobra.Command {
return cmd
}

// filterFile is used to filter files which should not be uploaded for a benchmark.
//
// This function respects common file types which would not be processed by Squeeze
// and should generally not be uploaded.
//
// Returns true if the file should be uploaded, false otherwise.
func filterFile(_ string, d fs.DirEntry) bool {
if d.IsDir() {
return false
}

// Ignore all hidden files (at least on Unix like systems where . prefix is used)
if strings.HasPrefix(d.Name(), ".") {
return false
}

return true
}

func getStepCount(client *squeezegoclient.QueueApi, stepName string) (int, error) {
step, err := client.GetQueueStep(stepName)
if err != nil {
Expand Down
80 changes: 80 additions & 0 deletions cmd/squeeze/benchmark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package squeeze

import (
"io/fs"
"testing"

"github.com/stretchr/testify/require"
)

type mockDirEntry struct {
name string
dir bool
}

func (m mockDirEntry) Name() string {
return m.name
}

func (m mockDirEntry) IsDir() bool {
return m.dir
}

func (m mockDirEntry) Type() fs.FileMode {
panic("not implemented")
}

func (m mockDirEntry) Info() (fs.FileInfo, error) {
panic("not implemented")
}

func Test_filterFile(t *testing.T) {
type test struct {
name string
path string
entry mockDirEntry
expected bool
}

tests := []test{
{
name: "valid pdf",
path: "invoice.pdf",
entry: mockDirEntry{
name: "invoice.pdf",
},
expected: true,
},
{
name: "valid xml",
path: "invoice.xml",
entry: mockDirEntry{
name: "invoice.xml",
},
expected: true,
},
{
name: "hidden path",
path: ".env",
entry: mockDirEntry{
name: ".env",
},
expected: false,
},
{
name: "directory",
path: "my-dir",
entry: mockDirEntry{
name: "my-dir",
dir: true,
},
expected: false,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
require.Equal(t, test.expected, filterFile(test.path, test.entry))
})
}
}

0 comments on commit 404ede7

Please sign in to comment.