diff --git a/fixtures/config/rule/link-to-sample.hcl b/fixtures/config/rule/link-to-sample.hcl new file mode 120000 index 0000000..8da0162 --- /dev/null +++ b/fixtures/config/rule/link-to-sample.hcl @@ -0,0 +1 @@ +sample.hcl \ No newline at end of file diff --git a/fixtures/config/rule/sample.hcl b/fixtures/config/rule/sample.hcl new file mode 100644 index 0000000..8ceb704 --- /dev/null +++ b/fixtures/config/rule/sample.hcl @@ -0,0 +1 @@ +# dummy sample diff --git a/pkg/config/rule/rule_test.go b/pkg/config/rule/rule_test.go new file mode 100644 index 0000000..a8de20c --- /dev/null +++ b/pkg/config/rule/rule_test.go @@ -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)) + } +}