Add: proper error handling for files

This commit is contained in:
Yoann La Cancellera
2023-10-10 12:05:19 +02:00
parent b0477bc37f
commit 12ea8033a9
2 changed files with 44 additions and 1 deletions

View File

@@ -2,6 +2,7 @@ package main
import (
"bufio"
"os"
"os/exec"
"runtime"
"strings"
@@ -25,6 +26,10 @@ func init() {
}
}
var (
errDirectoriesUnsupported = errors.New("directories are not supported")
)
// timelineFromPaths takes every path, search them using a list of regexes
// and organize them in a timeline that will be ready to aggregate or read
func timelineFromPaths(paths []string, regexes types.RegexMap) (types.Timeline, error) {
@@ -34,6 +39,14 @@ func timelineFromPaths(paths []string, regexes types.RegexMap) (types.Timeline,
compiledRegex := prepareGrepArgument(regexes)
for _, path := range paths {
osinfo, err := os.Stat(path)
if err != nil {
return nil, err
}
if osinfo.IsDir() {
return nil, errDirectoriesUnsupported
}
stdout := make(chan string)
go func() {
@@ -193,7 +206,6 @@ func iterateOnGrepResults(path string, regexes types.RegexMap, grepStdout <-chan
}
ctx, displayer = regex.Handle(ctx, line)
li := types.NewLogInfo(date, displayer, line, regex, key, ctx, filetype)
lt = lt.Add(li)
}

View File

@@ -0,0 +1,31 @@
package main
import (
"errors"
"os"
"testing"
)
func TestTimelineFromPaths(t *testing.T) {
tests := []struct {
path string
expectedErr error
}{
{
path: "tests/logs/",
expectedErr: errDirectoriesUnsupported,
},
{
path: "tests/logs/non_existing",
expectedErr: os.ErrNotExist,
},
}
for _, test := range tests {
_, err := timelineFromPaths([]string{test.path}, nil)
if !errors.Is(err, test.expectedErr) {
t.Fatalf("with path %s, expected error %v, got %v", test.path, test.expectedErr, err)
}
}
}