test: make ast check pylint conforming

A few adjustments to the AST check:
  * Avoid local imports. Move the AST import to the head of the file.
    We will get warnings if it is unused, so it should not get stale
    there once we drop the AST hack.
  * Avoid `TODO:` since pylint will parse it (parsing comments, yay!)
  * Use `isinstance()` for type-checks.
This commit is contained in:
David Rheinsberg 2020-04-20 08:20:48 +02:00 committed by Tom Gundersen
parent eb4d90cf94
commit e73f88d398

View file

@ -1,3 +1,4 @@
import ast
import json
import unittest
from pathlib import Path
@ -13,18 +14,17 @@ class TestStageInfo(unittest.TestCase):
@staticmethod
def get_stage_info(stage: Path) -> dict:
'''Return the STAGE_* variables from the given stage.'''
# TODO: This works for now, but stages should probably have some
# NOTE: This works for now, but stages should probably have some
# standard way of dumping this info so we (and other higher-level
# tools) don't have to parse the code and walk through the AST
# to find these values.
import ast
stage_info = {}
with open(stage) as fobj:
stage_ast = ast.parse(fobj.read(), filename=stage)
# STAGE_* assignments are at the toplevel, no need to walk()
for node in stage_ast.body:
if type(node) is ast.Assign and type(node.value) == ast.Str:
if isinstance(node, ast.Assign) and isinstance(node.value, ast.Str):
for target in node.targets:
if target.id.startswith("STAGE_"):
stage_info[target.id] = node.value.s
@ -55,7 +55,7 @@ class TestStageInfo(unittest.TestCase):
stage_opts = self.parse_stage_opts(stage_info["STAGE_OPTS"])
self.assertIsNotNone(stage_opts)
# TODO: we probably want an actual JSON Schema validator but
# NOTE: We probably want an actual JSON Schema validator but
# a nicely basic sanity test for our current STAGE_OPTS is:
# 1. If it's not empty, there should be a "properties" object,
# 2. If "required" exists, each item should be a property name