Skip to content

Commit

Permalink
test(rule): raise test coverage in a package
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanilves committed Oct 27, 2024
1 parent c5d2505 commit 3df81ae
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions fixtures/config/rule/link-to-sample.hcl
1 change: 1 addition & 0 deletions fixtures/config/rule/sample.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# dummy sample
43 changes: 43 additions & 0 deletions pkg/config/rule/rule_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package rule

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
)

const (
fixturePath = "../../../fixtures/config/rule"
)

func TestPathPrefixed(t *testing.T) {
assert := assert.New(t)

assert.False(Rule{Prefix: "./prefix"}.pathPrefixed("/etc/passwd"))
assert.True(Rule{Prefix: ""}.pathPrefixed("/etc/passwd"))
assert.True(Rule{Prefix: "./prefix"}.pathPrefixed("./prefix/file"))
}

func TestNormalizeNameEx(t *testing.T) {
assert := assert.New(t)

r := Rule{NameEx: "*.inc.php"}

assert.Equal(r.normalizeNameEx(), "^.*\\.inc\\.php$")
}

func TestNameMatched(t *testing.T) {
assert := assert.New(t)

files, err := os.ReadDir(fixturePath)
if err != nil {
panic(err)
}

for _, file := range files {
assert.True(Rule{}.nameMatched(file))
assert.True(Rule{NameEx: ".*"}.nameMatched(file))
assert.False(Rule{NameEx: "some-nonsense"}.nameMatched(file))
}
}

0 comments on commit 3df81ae

Please sign in to comment.