Repository: DataDog/guarddog (python)

Issue:
GuardDog should not crash when running only metadata rules
```
$ guarddog pypi scan requests --rules some-metadata-rule
Some rules failed to run while scanning requests:

* rules-all: failed to run rule:
An error occurred when running Semgrep.

command: semgrep --exclude='helm' --exclude='.idea' --exclude='venv' --exclude='test' --exclude='tests' --exclude='.env' --exclude='dist' --exclude='build' --exclude='semgrep' --exclude='migrations' --exclude='.github' --exclude='.semgrep_logs' --no-git-ignore --json --quiet /var/folders/_j/rxmxz87j51q5mzmk79qs0qs00000gp/T/tmp8kfzeyvi/requests
status code: 7
output: {"errors": [{"code": 7, "level": "error", "message": "No config given. Run with `--config auto` or see https://semgrep.dev/docs/running-rules/ for instructions on running with a specific config\n", "type": "SemgrepError"}], "paths": {"_comment": "<add --verbose for a list of skipped paths>", "scanned": []}, "results": [], "version": "0.112.1"}
```

Pull request description:
Don't run Semgrep when no sourcecode rule should be run (fixes #161)
Fixes #161


Patch:
diff --git a/guarddog/analyzer/analyzer.py b/guarddog/analyzer/analyzer.py
index cb2d41d..b0e0ada 100644
--- a/guarddog/analyzer/analyzer.py
+++ b/guarddog/analyzer/analyzer.py
@@ -3,7 +3,7 @@ import logging
 import os
 import subprocess
 from pathlib import Path
-from typing import Optional, Iterable
+from typing import Optional, Iterable, List
 
 from guarddog.analyzer.metadata import get_metadata_detectors
 from guarddog.ecosystems import ECOSYSTEM
@@ -159,15 +159,19 @@ class Analyzer:
         errors = {}
         issues = 0
 
-        rules_path: Iterable[str]
+        rules_path: List[str]
         if rules is None:
             log.debug(f"No rules specified using full rules directory {self.sourcecode_rules_path}")
-            rules_path = {self.sourcecode_rules_path}
+            rules_path = [self.sourcecode_rules_path]
         else:
-            rules_path = map(
+            rules_path = list(map(
                 lambda rule_name: os.path.join(self.sourcecode_rules_path, f"{rule_name}.yml"),
                 rules
-            )
+            ))
+
+        if len(rules_path) == 0:
+            log.debug("No source code rules to run")
+            return {"results": {}, "errors": {}, "issues": 0}
 
         try:
             log.debug(f"Running source code rules against {path}")


Test patch:
diff --git a/tests/core/test_sourcecode_analyzer.py b/tests/core/test_sourcecode_analyzer.py
new file mode 100644
index 0000000..8ba1ac0
--- /dev/null
+++ b/tests/core/test_sourcecode_analyzer.py
@@ -0,0 +1,12 @@
+from guarddog import ecosystems
+from guarddog.analyzer.analyzer import Analyzer
+
+
+def test_source_code_analyzer_ran_with_no_rules():
+    """
+    Regression test for https://github.com/DataDog/guarddog/issues/161
+    """
+    analyzer = Analyzer(ecosystem=ecosystems.ECOSYSTEM.PYPI)
+
+    result = analyzer.analyze_sourcecode("/tmp", set())
+    assert len(result['errors']) == 0