Skip to content

Commit

Permalink
Merge pull request #13 from sundowndev/test/analysis
Browse files Browse the repository at this point in the history
Add missing tests for analysis pkg
  • Loading branch information
sundowndev committed Jan 3, 2023
2 parents 98d6fad + 7292424 commit 49cb7ca
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 3 deletions.
69 changes: 69 additions & 0 deletions lib/analysis/analysis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//go:build !windows

package analysis

import (
"bytes"
"github.com/stretchr/testify/assert"
"github.com/sundowndev/covermyass/v2/mocks"
"os"
"testing"
)

func TestAnalysis_Write(t *testing.T) {
fakeCheck := &mocks.Check{}

testcases := []struct {
name string
filename string
results []Result
}{
{
name: "test valid analysis",
filename: "testdata/valid_analysis.txt",
results: []Result{
{
Path: "/var/log/dummy.log",
Size: int64(32),
Mode: os.ModePerm,
ReadOnly: false,
Check: fakeCheck,
},
{
Path: "/var/log/dummy2.log",
Size: int64(8),
Mode: 600,
ReadOnly: true,
Check: fakeCheck,
},
},
},
{
name: "test empty analysis",
filename: "testdata/empty_analysis.txt",
results: []Result{},
},
}

for _, tt := range testcases {
t.Run(tt.name, func(t *testing.T) {
testdata, err := os.ReadFile(tt.filename)
if err != nil {
t.Fatal(err)
}

a := NewAnalysis()
for _, r := range tt.results {
a.AddResult(r)
}
assert.Len(t, a.Results(), len(tt.results))

buffer := &bytes.Buffer{}
a.Write(buffer)

assert.Equal(t, string(testdata), buffer.String())
})
}

fakeCheck.AssertExpectations(t)
}
9 changes: 6 additions & 3 deletions lib/analysis/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ import (

type Analyzer struct {
filter filter.Filter
finder find.Finder
}

func NewAnalyzer(filterEngine filter.Filter) *Analyzer {
return &Analyzer{filterEngine}
return &Analyzer{
filterEngine,
find.New(os.DirFS(""), filterEngine),
}
}

func (a *Analyzer) Analyze() (*Analysis, error) {
Expand All @@ -31,8 +35,7 @@ func (a *Analyzer) Analyze() (*Analysis, error) {
for _, c := range check.GetAllChecks() {
wg.Add(1)
go func(c check.Check) {
finder := find.New(os.DirFS(""), a.filter)
results, err := finder.Run(context.TODO(), c.Paths())
results, err := a.finder.Run(context.TODO(), c.Paths())
if err != nil {
logrus.Error(err)
return
Expand Down
2 changes: 2 additions & 0 deletions lib/analysis/testdata/empty_analysis.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Summary
Found 0 files (0 read-write, 0 read-only) in 0s
6 changes: 6 additions & 0 deletions lib/analysis/testdata/valid_analysis.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Found the following files
/var/log/dummy.log (32 B, -rwxrwxrwx)
/var/log/dummy2.log (8 B, ---x-wx---)

Summary
Found 2 files (1 read-write, 1 read-only) in 0s
55 changes: 55 additions & 0 deletions mocks/Check.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 49cb7ca

Please sign in to comment.