Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve testing for rules #1404

Merged
merged 4 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{"source":"Bearer","version":"dev","findings":[{"cwe_ids":["319"],"id":"expected_rule","title":"","description":"","documentation_url":"","line_number":3,"full_filename":"e2e/rules/testdata/data/expected_rule/main.rb","filename":"main.rb","source":{"start":3,"end":3,"column":{"start":3,"end":7}},"sink":{"start":3,"end":3,"column":{"start":3,"end":7},"content":"sink"},"parent_line_number":3,"snippet":"sink","fingerprint":"c50ecec7e1fcfba6cce5fcfab129556c_0","old_fingerprint":"6630ae26e5210b1e43bb4c02426e6be7_0","code_extract":" sink","severity":"low"},{"cwe_ids":["319"],"id":"expected_rule","title":"","description":"","documentation_url":"","line_number":8,"full_filename":"e2e/rules/testdata/data/expected_rule/main.rb","filename":"main.rb","source":{"start":8,"end":8,"column":{"start":3,"end":7}},"sink":{"start":8,"end":8,"column":{"start":3,"end":7},"content":"sink"},"parent_line_number":8,"snippet":"sink","fingerprint":"c50ecec7e1fcfba6cce5fcfab129556c_1","old_fingerprint":"6630ae26e5210b1e43bb4c02426e6be7_1","code_extract":" sink","severity":"low"}],"expected_findings":[{"rule_id":"expected_rule","location":{"start":3,"end":3,"column":{"start":3,"end":7}}},{"rule_id":"expected_rule","location":{"start":8,"end":8,"column":{"start":3,"end":7}}}]}

--
Analyzing codebase

16 changes: 16 additions & 0 deletions e2e/rules/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@ func buildRulesTestCase(testName, path, ruleID string) testhelper.TestCase {
return testhelper.NewTestCase(testName, arguments, options)
}

func buildRulesTestCaseJsonV2(testName, path, ruleID string) testhelper.TestCase {
arguments := []string{
"scan",
path,
"--only-rule=" + ruleID,
"--format=jsonv2",
"--disable-default-rules",
"--exit-code=0",
"--external-rule-dir=" + filepath.Join("e2e", "rules", "testdata", "rules"),
}

options := testhelper.TestCaseOptions{}

return testhelper.NewTestCase(testName, arguments, options)
}

func runRulesTest(folderPath string, ruleID string, t *testing.T) {
snapshotDirectory := ".snapshots"

Expand Down
15 changes: 15 additions & 0 deletions e2e/rules/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,18 @@ func TestRubyRailsDefaultEncryptionStructure(t *testing.T) {
func TestRubyRailsDefaultEncryptionSchema(t *testing.T) {
runRulesTest("ruby_rails_default_encryption_schema_rb", "ruby_rails_default_encryption", t)
}

func TestExpectedRule(t *testing.T) {
testDataDir := "testdata/data/expected_rule"

testCases := []testhelper.TestCase{}
testCases = append(testCases,
buildRulesTestCaseJsonV2(
testDataDir,
filepath.Join("e2e", "rules", testDataDir),
"expected_rule",
),
)

testhelper.RunTestsWithSnapshotSubdirectory(t, testCases, ".snapshots")
}
14 changes: 14 additions & 0 deletions e2e/rules/testdata/data/expected_rule/main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def m
# bearer:expected expected_rule
sink
end

def n
# bearer:expected expected_rule
sink
end

def foo
bar
end

9 changes: 9 additions & 0 deletions e2e/rules/testdata/rules/expected_rule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
patterns:
- sink
languages:
- ruby
severity: low
metadata:
cwe_id:
- 319
id: expected_rule
218 changes: 0 additions & 218 deletions internal/languages/__template__/analyzer/analyzer.go

This file was deleted.

20 changes: 0 additions & 20 deletions internal/languages/__template__/detectors/detectors_test.go

This file was deleted.

Loading
Loading