Skip to content

Commit

Permalink
Add flag to ignore specific procedural languages (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
jgpruitt authored Jul 25, 2024
1 parent 9dde38b commit 777fb15
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ __pycache__
.pytest_cache
.tox
dist
.idea
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ options:
Analyze PLpgSQL code (default: True)
--explain EXPLAIN Describe an error/warning code
--ignore IGNORE Ignore error or warning code
--ignore-lang LANG Ignore unknown procedural language
--sql-accepting SQL_FN
Specify one or more sql-accepting functions
```
Expand Down
9 changes: 9 additions & 0 deletions src/pgspot/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ def run():
type=str,
help="Ignore error or warning code",
)
parser.add_argument(
"--ignore-lang",
dest="ignore_lang",
action="append",
default=[],
type=str,
help="Ignore one or more unknown procedural languages",
)
parser.add_argument(
"--sql-accepting",
dest="sql_fn",
Expand All @@ -70,6 +78,7 @@ def run():
)

args = parser.parse_args()
args.ignore_lang = [lang.lower() for lang in args.ignore_lang]

counter = Counter(args)
state = State(counter)
Expand Down
2 changes: 2 additions & 0 deletions src/pgspot/visitors.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ def visit_CreateFunctionStmt(self, ancestors, node):
visit_plpgsql(state, node)
case "c" | "internal":
pass
case language if language in self.state.args.ignore_lang:
pass
case _:
self.state.unknown(f"Unknown function language: {language}")

Expand Down
51 changes: 51 additions & 0 deletions tests/ignore_lang_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from util import run


sql = """
CREATE FUNCTION python_max(a integer, b integer) RETURNS integer AS $$
return max(a, b)
$$ LANGUAGE plpython3u SET search_path TO 'pg_catalog', 'pg_temp'
;
CREATE FUNCTION tcl_max(integer, integer) RETURNS integer AS $$
if {$1 > $2} {return $1}
return $2
$$ LANGUAGE pltcl STRICT SET search_path TO 'pg_catalog', 'pg_temp'
;
"""


def test_unknown_lang_plpython3u():
output = run(sql)
assert "Unknown function language: plpython3u" in output


def test_unknown_lang_pltcl():
output = run(sql)
assert "Unknown function language: pltcl" in output


def test_ignore_lang_plpython3u():
output = run(sql, list("--ignore-lang=plpython3u"))
assert "Unknown function language: plpython3u" not in output


def test_ignore_lang_pltcl():
output = run(sql, list("--ignore-lang=pltcl"))
assert "Unknown function language: pltcl" not in output


def test_ignore_lang_plpython3u_pltcl():
output = run(sql, ["--ignore-lang=plpython3u", "--ignore-lang=pltcl"])
assert (
"Unknown function language: pltcl" not in output
and "Unknown function language: plpython3u" not in output
)


def test_ignore_lang_upper():
output = run(sql, ["--ignore-lang=PLPYTHON3U", "--ignore-lang=PLTCL"])
assert (
"Unknown function language: pltcl" not in output
and "Unknown function language: plpython3u" not in output
)

0 comments on commit 777fb15

Please sign in to comment.