From 86c13937009d27c8949410b549ce0e8bfd30b691 Mon Sep 17 00:00:00 2001 From: Graham Gilbert Date: Wed, 13 May 2026 22:17:36 -0700 Subject: [PATCH] Improve fileline table tests --- tables/fileline/file_line.go | 10 +++- tables/fileline/file_line_test.go | 78 +++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 2 deletions(-) diff --git a/tables/fileline/file_line.go b/tables/fileline/file_line.go index 2bc65c5..1bf75f4 100644 --- a/tables/fileline/file_line.go +++ b/tables/fileline/file_line.go @@ -74,12 +74,18 @@ func processFile(path string, wildcard bool, fs utils.FileSystem) ([]FileLine, e return nil, err } for _, file := range files { - lines, _ := readLines(file, fs) + lines, err := readLines(file, fs) + if err != nil { + return nil, err + } output = append(output, lines...) } } else { - lines, _ := readLines(path, fs) + lines, err := readLines(path, fs) + if err != nil { + return nil, err + } output = append(output, lines...) } diff --git a/tables/fileline/file_line_test.go b/tables/fileline/file_line_test.go index 76ad2d3..aee290b 100644 --- a/tables/fileline/file_line_test.go +++ b/tables/fileline/file_line_test.go @@ -1,14 +1,23 @@ package fileline import ( + "context" "os" "path/filepath" "testing" "github.com/macadmins/osquery-extension/pkg/utils" + "github.com/osquery/osquery-go/plugin/table" "github.com/stretchr/testify/assert" ) +func TestFileLineColumns(t *testing.T) { + assert.Equal(t, []table.ColumnDefinition{ + table.TextColumn("line"), + table.TextColumn("path"), + }, FileLineColumns()) +} + func TestProcessFile(t *testing.T) { t.Run("processFile with wildcard", func(t *testing.T) { // Create temporary files for testing @@ -57,6 +66,29 @@ func TestProcessFile(t *testing.T) { assert.NoError(t, err) assert.Len(t, lines, 2) }) + + t.Run("invalid wildcard pattern", func(t *testing.T) { + lines, err := processFile("[", true, utils.MockFileSystem{FileExists: true}) + assert.Error(t, err) + assert.Nil(t, lines) + }) + + t.Run("read error is returned without wildcard", func(t *testing.T) { + lines, err := processFile(filepath.Join(t.TempDir(), "missing.txt"), false, utils.MockFileSystem{FileExists: true}) + assert.Error(t, err) + assert.Nil(t, lines) + }) + + t.Run("read error is returned with wildcard", func(t *testing.T) { + tmpFile := filepath.Join(t.TempDir(), "testfile-one.txt") + err := os.Mkdir(tmpFile, os.ModePerm) + assert.NoError(t, err) + + path := filepath.Join(filepath.Dir(tmpFile), "testfile%.txt") + lines, err := processFile(path, true, utils.MockFileSystem{FileExists: false}) + assert.Error(t, err) + assert.Nil(t, lines) + }) } func TestReadLines(t *testing.T) { @@ -84,4 +116,50 @@ func TestReadLines(t *testing.T) { assert.Nil(t, lines) assert.Equal(t, "file does not exist", err.Error()) }) + + t.Run("readLines open error", func(t *testing.T) { + fs := utils.MockFileSystem{FileExists: true, Err: nil} + lines, err := readLines(filepath.Join(t.TempDir(), "missing.txt"), fs) + assert.Error(t, err) + assert.Nil(t, lines) + }) +} + +func TestFileLineGenerate(t *testing.T) { + tmpFile, err := os.CreateTemp("", "testfile-*.txt") + assert.NoError(t, err) + defer func() { + assert.NoError(t, os.Remove(tmpFile.Name())) + }() + + _, err = tmpFile.WriteString("line1\nline2\n") + assert.NoError(t, err) + assert.NoError(t, tmpFile.Close()) + + results, err := FileLineGenerate(context.Background(), table.QueryContext{ + Constraints: map[string]table.ConstraintList{ + "path": {Constraints: []table.Constraint{{ + Operator: table.OperatorEquals, + Expression: tmpFile.Name(), + }}}, + }, + }) + assert.NoError(t, err) + assert.Equal(t, []map[string]string{ + {"line": "line1", "path": tmpFile.Name()}, + {"line": "line2", "path": tmpFile.Name()}, + }, results) +} + +func TestFileLineGenerateReturnsPathErrors(t *testing.T) { + results, err := FileLineGenerate(context.Background(), table.QueryContext{ + Constraints: map[string]table.ConstraintList{ + "path": {Constraints: []table.Constraint{{ + Operator: table.OperatorEquals, + Expression: filepath.Join(t.TempDir(), "missing.txt"), + }}}, + }, + }) + assert.Error(t, err) + assert.Empty(t, results) }