Skip to content

Commit

Permalink
check assertion arguments with inspect.Signature.bind()
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbray committed Jan 14, 2022
1 parent 7deed56 commit 8f8c8c4
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
9 changes: 5 additions & 4 deletions planemo/workflow_lint.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import inspect
import json
import os
import re
Expand Down Expand Up @@ -219,14 +220,14 @@ def _check_test_assertions(lint_context, assertion_definitions):
for module in asserts.assertion_modules:
for function_name in dir(module):
if function_name.split('assert_')[-1] in assertion_definitions:
f = module.__dict__[function_name]
signature = inspect.signature(module.__dict__[function_name])
try:
# try running the function with the attributes supplied and check for TypeError
f('', **assertion_definitions[function_name.split('assert_')[-1]])
# try mapping the function with the attributes supplied and check for TypeError
signature.bind('', **assertion_definitions[function_name.split('assert_')[-1]])
except AssertionError:
pass
except TypeError as e:
lint_context.error(f'Invalid assertion in tests: {str(e)}')
lint_context.error(f'Invalid assertion in tests: {function_name} {str(e)}')
assertions_valid = False
return assertions_valid

Expand Down
3 changes: 2 additions & 1 deletion tests/data/wf15-test-assertions-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
output:
asserts:
has_text:
non_existent_attribute: 0
non_existent_attribute: 0
text: 'text'
2 changes: 1 addition & 1 deletion tests/test_cmd_workflow_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def test_assertion_linting(self):
workflow_path = '/'.join((TEST_DATA_DIR, 'wf15-test-assertions.yml'))
lint_cmd = ["workflow_lint", workflow_path]
result = self._runner.invoke(self._cli.planemo, lint_cmd)
assert "ERROR: Invalid assertion in tests: assert_has_text() got an unexpected keyword argument 'non_existent_attribute'" in result.output
assert "Invalid assertion in tests: assert_has_text got an unexpected keyword argument 'non_existent_attribute'" in result.output


def _wf_repo(rel_path):
Expand Down

0 comments on commit 8f8c8c4

Please sign in to comment.