initial commit
This commit is contained in:
commit
885e7d6e02
22 changed files with 3001 additions and 0 deletions
155
internal/parser/scanner_test.go
Normal file
155
internal/parser/scanner_test.go
Normal file
|
|
@ -0,0 +1,155 @@
|
|||
package parser
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestScanDirectory_FindsAccessAndErrorLogs(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
|
||||
// Create test log files
|
||||
files := []string{
|
||||
"access.log",
|
||||
"access.log.1",
|
||||
"error.log",
|
||||
"error.log.1",
|
||||
"other.txt", // should be ignored
|
||||
}
|
||||
for _, f := range files {
|
||||
if err := os.WriteFile(filepath.Join(dir, f), []byte(""), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
accessFiles, errorFiles, err := ScanDirectory(dir, false)
|
||||
if err != nil {
|
||||
t.Fatalf("ScanDirectory error: %v", err)
|
||||
}
|
||||
if len(accessFiles) != 2 {
|
||||
t.Errorf("expected 2 access files, got %d: %v", len(accessFiles), accessFiles)
|
||||
}
|
||||
if len(errorFiles) != 2 {
|
||||
t.Errorf("expected 2 error files, got %d: %v", len(errorFiles), errorFiles)
|
||||
}
|
||||
}
|
||||
|
||||
func TestScanDirectory_GzipIncluded(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
files := []string{
|
||||
"access.log",
|
||||
"access.log.1.gz",
|
||||
"error.log",
|
||||
}
|
||||
for _, f := range files {
|
||||
if err := os.WriteFile(filepath.Join(dir, f), []byte(""), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
accessFiles, _, err := ScanDirectory(dir, true)
|
||||
if err != nil {
|
||||
t.Fatalf("ScanDirectory error: %v", err)
|
||||
}
|
||||
if len(accessFiles) != 2 {
|
||||
t.Errorf("expected 2 access files (including gz), got %d", len(accessFiles))
|
||||
}
|
||||
}
|
||||
|
||||
func TestScanDirectory_GzipExcluded(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
files := []string{
|
||||
"access.log",
|
||||
"access.log.1.gz",
|
||||
}
|
||||
for _, f := range files {
|
||||
if err := os.WriteFile(filepath.Join(dir, f), []byte(""), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
accessFiles, _, err := ScanDirectory(dir, false)
|
||||
if err != nil {
|
||||
t.Fatalf("ScanDirectory error: %v", err)
|
||||
}
|
||||
if len(accessFiles) != 1 {
|
||||
t.Errorf("expected 1 access file (gz excluded), got %d", len(accessFiles))
|
||||
}
|
||||
}
|
||||
|
||||
func TestScanDirectory_NonexistentDir(t *testing.T) {
|
||||
_, _, err := ScanDirectory("/nonexistent/path/xyz", true)
|
||||
if err == nil {
|
||||
t.Error("expected error for nonexistent directory, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpenLogFile_PlainText(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
content := "test line\n"
|
||||
path := filepath.Join(dir, "test.log")
|
||||
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
rc, err := openLogFile(path)
|
||||
if err != nil {
|
||||
t.Fatalf("openLogFile error: %v", err)
|
||||
}
|
||||
defer rc.Close()
|
||||
|
||||
scanner := bufio.NewScanner(rc)
|
||||
if !scanner.Scan() {
|
||||
t.Fatal("expected to read a line")
|
||||
}
|
||||
if scanner.Text() != "test line" {
|
||||
t.Errorf("got %q, want %q", scanner.Text(), "test line")
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpenLogFile_Gzip(t *testing.T) {
|
||||
// Use the pre-made fixture
|
||||
rc, err := openLogFile("testdata/access.log.1.gz")
|
||||
if err != nil {
|
||||
t.Fatalf("openLogFile gz error: %v", err)
|
||||
}
|
||||
defer rc.Close()
|
||||
|
||||
scanner := bufio.NewScanner(rc)
|
||||
if !scanner.Scan() {
|
||||
t.Fatal("expected to read a line from gz file")
|
||||
}
|
||||
line := scanner.Text()
|
||||
if line == "" {
|
||||
t.Error("expected non-empty line from gz file")
|
||||
}
|
||||
}
|
||||
|
||||
func TestScanDirectory_SortOrder(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
files := []string{
|
||||
"access.log.2.gz",
|
||||
"access.log",
|
||||
"access.log.1",
|
||||
"access.log.1.gz",
|
||||
}
|
||||
for _, f := range files {
|
||||
if err := os.WriteFile(filepath.Join(dir, f), []byte(""), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
accessFiles, _, err := ScanDirectory(dir, true)
|
||||
if err != nil {
|
||||
t.Fatalf("ScanDirectory error: %v", err)
|
||||
}
|
||||
if len(accessFiles) != 4 {
|
||||
t.Fatalf("expected 4 files, got %d", len(accessFiles))
|
||||
}
|
||||
// access.log should come first (most recent)
|
||||
if filepath.Base(accessFiles[0]) != "access.log" {
|
||||
t.Errorf("expected access.log first, got %q", filepath.Base(accessFiles[0]))
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue