tests: stop using deprecated unittest.makeSuite
done with:
import libcst as cst
import sys
class RewriteMakeSuite(cst.CSTTransformer):
def leave_Call(self, original_node: cst.Call, updated_node: cst.Call) -> cst.BaseExpression:
func = updated_node.func
if isinstance(func, cst.Attribute) and isinstance(func.value, cst.Name):
if func.value.value == "unittest" and func.attr.value == "makeSuite":
first_arg = updated_node.args[0]
# check second argument
if len(updated_node.args) > 1:
second = updated_node.args[1].value
if isinstance(second, cst.SimpleString):
val = second.evaluated_value
if val != "test":
print(f"ERROR: unexpected second argument {second.code!r} in {original_node.code!r}", file=sys.stderr)
return updated_node
else:
print(f"ERROR: non-string second argument in {original_node.code!r}", file=sys.stderr)
return updated_node
# keep only first argument and remove comma
new_args = [first_arg.with_changes(comma=None)]
new_func = cst.Attribute(
value=cst.Attribute(
value=cst.Name("unittest"),
attr=cst.Name("defaultTestLoader"),
),
attr=cst.Name("loadTestsFromTestCase"),
)
return updated_node.with_changes(func=new_func, args=new_args)
return updated_node
if __name__ == "__main__":
for path in sys.argv[1:]:
src = open(path).read()
mod = cst.parse_module(src)
new_mod = mod.visit(RewriteMakeSuite())
with open(path, "w") as f:
f.write(new_mod.code)