From 404ede7ab4800069c2695142aaca18364b15937e Mon Sep 17 00:00:00 2001 From: fabiante Date: Wed, 14 Aug 2024 11:15:42 +0200 Subject: [PATCH] Add file filter to ignore hidden files (prefixed with a .) --- cmd/squeeze/benchmark.go | 30 +++++++++++-- cmd/squeeze/benchmark_test.go | 80 +++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 4 deletions(-) create mode 100644 cmd/squeeze/benchmark_test.go diff --git a/cmd/squeeze/benchmark.go b/cmd/squeeze/benchmark.go index 7f627a6..441efc7 100644 --- a/cmd/squeeze/benchmark.go +++ b/cmd/squeeze/benchmark.go @@ -7,6 +7,7 @@ import ( "log/slog" "os" "path/filepath" + "strings" "time" squeezegoclient "github.com/dexpro-solutions-gmbh/squeeze-go-client" @@ -55,7 +56,9 @@ func newBenchmarkCmd() *cobra.Command { if err != nil { return err } - if d.IsDir() { + + keep := filterFile(path, d) + if !keep { return nil } @@ -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) @@ -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 @@ -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 { diff --git a/cmd/squeeze/benchmark_test.go b/cmd/squeeze/benchmark_test.go new file mode 100644 index 0000000..cc1999c --- /dev/null +++ b/cmd/squeeze/benchmark_test.go @@ -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)) + }) + } +}