mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-24 03:11:24 -07:00
Extend validate-modules to check the next to last line (#34819)
* Add validation for the next to last line of a module * Fix last error code * Reduce to a single conditional * Fix conditionals * Move the final warnings statement to main() in mysql_replication
This commit is contained in:
parent
a65f702155
commit
b10d5f34ea
3 changed files with 27 additions and 1 deletions
|
@ -518,6 +518,30 @@ class ModuleValidator(Validator):
|
|||
bodies.extend(if_bodies)
|
||||
|
||||
for child in bodies:
|
||||
|
||||
# validate that the next to last line is 'if __name__ == "__main__"'
|
||||
if child.lineno == (self.length - 1):
|
||||
|
||||
mainchecked = False
|
||||
try:
|
||||
if isinstance(child, ast.If) and \
|
||||
child.test.left.id == '__name__' and \
|
||||
len(child.test.ops) == 1 and \
|
||||
isinstance(child.test.ops[0], ast.Eq) and \
|
||||
child.test.comparators[0].s == '__main__':
|
||||
mainchecked = True
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if not mainchecked:
|
||||
self.reporter.error(
|
||||
path=self.object_path,
|
||||
code=109,
|
||||
msg='Next to last line should be: if __name__ == "__main__":',
|
||||
line=child.lineno
|
||||
)
|
||||
|
||||
# validate that the final line is a call to main()
|
||||
if isinstance(child, ast.Expr):
|
||||
if isinstance(child.value, ast.Call):
|
||||
if (isinstance(child.value.func, ast.Name) and
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue