-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8a2e5a6
commit 5849855
Showing
22 changed files
with
447 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package utils | ||
|
||
import "os" | ||
|
||
// MockFileSystem is a mock implementation of FileSystem for testing | ||
type MockFileSystem struct { | ||
FileExists bool | ||
Err error | ||
} | ||
|
||
func (m MockFileSystem) Stat(name string) (os.FileInfo, error) { | ||
if m.Err != nil { | ||
return nil, m.Err | ||
} | ||
if m.FileExists { | ||
return nil, nil | ||
} | ||
return nil, os.ErrNotExist | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package fileline | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/macadmins/osquery-extension/pkg/utils" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestProcessFile(t *testing.T) { | ||
t.Run("processFile with wildcard", func(t *testing.T) { | ||
// Create temporary files for testing | ||
tmpFile1, err := os.CreateTemp("", "testfile1-*.txt") | ||
assert.NoError(t, err) | ||
defer os.Remove(tmpFile1.Name()) | ||
|
||
tmpFile2, err := os.CreateTemp("", "testfile2-*.txt") | ||
assert.NoError(t, err) | ||
defer os.Remove(tmpFile2.Name()) | ||
|
||
_, err = tmpFile1.WriteString("line1\nline2\n") | ||
assert.NoError(t, err) | ||
_, err = tmpFile2.WriteString("line3\nline4\n") | ||
assert.NoError(t, err) | ||
|
||
tmpFile1.Close() | ||
tmpFile2.Close() | ||
|
||
path := filepath.Join(filepath.Dir(tmpFile1.Name()), "testfile%-*.txt") | ||
fs := utils.MockFileSystem{FileExists: true, Err: nil} | ||
lines, err := processFile(path, true, fs) | ||
assert.NoError(t, err) | ||
assert.Len(t, lines, 4) | ||
}) | ||
|
||
t.Run("processFile without wildcard", func(t *testing.T) { | ||
// Create a temporary file for testing | ||
tmpFile, err := os.CreateTemp("", "testfile-*.txt") | ||
assert.NoError(t, err) | ||
defer os.Remove(tmpFile.Name()) | ||
|
||
_, err = tmpFile.WriteString("line1\nline2\n") | ||
assert.NoError(t, err) | ||
tmpFile.Close() | ||
|
||
fs := utils.MockFileSystem{FileExists: true, Err: nil} | ||
|
||
lines, err := processFile(tmpFile.Name(), false, fs) | ||
assert.NoError(t, err) | ||
assert.Len(t, lines, 2) | ||
}) | ||
} | ||
|
||
func TestReadLines(t *testing.T) { | ||
t.Run("readLines file exists", func(t *testing.T) { | ||
// Create a temporary file for testing | ||
tmpFile, err := os.CreateTemp("", "testfile-*.txt") | ||
assert.NoError(t, err) | ||
defer os.Remove(tmpFile.Name()) | ||
|
||
_, err = tmpFile.WriteString("line1\nline2\n") | ||
assert.NoError(t, err) | ||
tmpFile.Close() | ||
fs := utils.MockFileSystem{FileExists: true, Err: nil} | ||
lines, err := readLines(tmpFile.Name(), fs) | ||
assert.NoError(t, err) | ||
assert.Len(t, lines, 2) | ||
}) | ||
|
||
t.Run("readLines file does not exist", func(t *testing.T) { | ||
fs := utils.MockFileSystem{FileExists: false, Err: nil} | ||
lines, err := readLines("nonexistentfile.txt", fs) | ||
assert.Error(t, err) | ||
assert.Nil(t, lines) | ||
assert.Equal(t, "file does not exist", err.Error()) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.