mirror of
https://github.com/trufflesecurity/trufflehog.git
synced 2024-11-10 07:04:24 +00:00
[chore] Add filesystem integration test (#2358)
This commit is contained in:
parent
8e90c4e669
commit
24d0680f5c
1 changed files with 82 additions and 0 deletions
82
pkg/engine/filesystem_integration_test.go
Normal file
82
pkg/engine/filesystem_integration_test.go
Normal file
|
@ -0,0 +1,82 @@
|
|||
//go:build integration
|
||||
// +build integration
|
||||
|
||||
package engine
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/trufflesecurity/trufflehog/v3/pkg/context"
|
||||
"github.com/trufflesecurity/trufflehog/v3/pkg/sources"
|
||||
)
|
||||
|
||||
// createFilesystemTree is a helper function to create a temporary directory
|
||||
// that contains all of the provided files and contents on the operating
|
||||
// system's filesystem. Sub-directories will be created as needed. On success,
|
||||
// the root directory is returned and the caller is responsible for removing it
|
||||
// from the filesystem.
|
||||
func createFilesystemTree(files map[string]string) (string, error) {
|
||||
parentDir, err := os.MkdirTemp("", "trufflehog-integration-test")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
for path, contents := range files {
|
||||
fullPath := filepath.Join(parentDir, path)
|
||||
if err := os.MkdirAll(filepath.Dir(fullPath), 0755); err != nil {
|
||||
_ = os.RemoveAll(parentDir)
|
||||
return "", err
|
||||
}
|
||||
if err := os.WriteFile(fullPath, []byte(contents), 0644); err != nil {
|
||||
_ = os.RemoveAll(parentDir)
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
return parentDir, nil
|
||||
}
|
||||
|
||||
func TestFilesystem(t *testing.T) {
|
||||
// Setup test directory.
|
||||
rootDir, err := createFilesystemTree(map[string]string{
|
||||
"/foo": "bar",
|
||||
"/bar": "baz",
|
||||
"/dir/a": "a",
|
||||
"/dir/b": "b",
|
||||
"/dir/c": "c",
|
||||
"/.ignore/file": "this should be ignored",
|
||||
"/.ignore/sub/dir": "this should also be ignored",
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
defer os.RemoveAll(rootDir)
|
||||
|
||||
configDir, err := createFilesystemTree(map[string]string{
|
||||
"/exclude": ".ignore",
|
||||
// TODO: Test include configuration.
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
defer os.RemoveAll(configDir)
|
||||
|
||||
// Run the scan.
|
||||
ctx := context.Background()
|
||||
e, err := Start(ctx,
|
||||
WithDetectors(DefaultDetectors()...),
|
||||
WithVerify(false),
|
||||
)
|
||||
assert.NoError(t, err)
|
||||
err = e.ScanFileSystem(ctx, sources.FilesystemConfig{
|
||||
Paths: []string{rootDir},
|
||||
ExcludePathsFile: filepath.Join(configDir, "exclude"),
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
|
||||
err = e.Finish(ctx)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Check the output provided by metrics.
|
||||
metrics := e.GetMetrics()
|
||||
assert.Equal(t, uint64(5), metrics.ChunksScanned)
|
||||
assert.Equal(t, uint64(9), metrics.BytesScanned)
|
||||
}
|
Loading…
Reference in a new issue