Skip to content

Commit

Permalink
testing: add a parser for testcases that can read Python code
Browse files Browse the repository at this point in the history
Summary: Add the support of parsing a more complicated "#testcases" definition that can be Python code. This allows the flexibility to customize the result list of desired test cases.

Reviewed By: quark-zju

Differential Revision: D64726786

fbshipit-source-id: bf92a9fca413bc74044d01f3bf2155df7ce9a5d7
  • Loading branch information
lXXXw authored and facebook-github-bot committed Oct 23, 2024
1 parent b2857d1 commit 5a3a987
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions eden/scm/sapling/testing/t/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@

import textwrap
from dataclasses import dataclass
from typing import Callable, List, Optional, Tuple

from itertools import product
from typing import Callable, List, Optional, Tuple, Union


def transform(
Expand Down Expand Up @@ -410,7 +412,7 @@ def appendline(line):
appendline(f'raise __import__("unittest").SkipTest({repr(msg)})\n')
elif info.line.startswith("#testcases "):
assert registertestcase is not None
newcases = info.line.split()[1:]
newcases = parse_testcases(info.line)
for testcase in newcases:
registertestcase(testcase)
testcases.extend(newcases)
Expand Down Expand Up @@ -470,3 +472,37 @@ def appendline(line):
i = nexti

return "".join(newlines)


def parse_testcases(testcases: str) -> List[Union[str, Tuple[str]]]:
r"""Helper function to parse "#testcases " directive.
Support of the previous syntax where a list of simple test cases are provided:
>>> parse_testcases("#testcases CASE1 CASE2")
['CASE1', 'CASE2']
Support of parsing the string as Python code when seeing "(":
>>> parse_testcases("#testcases('FEATURE_1', 'FEATURE_2')")
['FEATURE_1', 'FEATURE_2']
With Python code, we can combine different cases to get a mixed testcase consisting of
independent features:
>>> parse_testcases("#testcases(*product(['FEATURE_1', None], ['FEATURE_2', None]))")
[('FEATURE_1', 'FEATURE_2'), ('FEATURE_1', None), (None, 'FEATURE_2'), (None, None)]
"""
prefix = "#testcases "

if testcases.startswith(prefix):
return testcases[len(prefix) :].split()

# treat it as a python expression to evaluate
# ignore the leading "#"
python_str = testcases[1:]

res = eval(python_str, {"product": product, "testcases": lambda *args: args})

return list(res)

0 comments on commit 5a3a987

Please sign in to comment.