Skip to content

Commit

Permalink
testing: support multiple test case names
Browse files Browse the repository at this point in the history
Summary:
Previously, `TestTmp` takes a `str | None` as `testcase` name. This diff
extends it to support multiple test case names like `(str, str)`. `None` and
`False` are discarded so one might write conditional test case name like:

  hasfeature(x) and 'testcase-name'

Reviewed By: sggutier

Differential Revision: D64705417

fbshipit-source-id: 1b91654aa03418c186bf5c7021ed28c6f405e781
  • Loading branch information
quark-zju authored and facebook-github-bot committed Oct 21, 2024
1 parent abd11ad commit 90ebb9e
Showing 1 changed file with 35 additions and 9 deletions.
44 changes: 35 additions & 9 deletions eden/scm/sapling/testing/t/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
import textwrap
import unittest
from dataclasses import dataclass
from itertools import chain
from pathlib import Path
from typing import Callable, Optional
from typing import Callable, Iterable, Optional, Union

from .. import sh
from ..sh.bufio import BufIO
Expand Down Expand Up @@ -244,7 +245,7 @@ def __init__(
self,
updateglobalstate: bool = True,
tmpprefix: str = "",
testcase: Optional[str] = None,
testcase: Union[Iterable[Union[str, None, bool]], str, None, bool] = None,
):
"""create a TestTmp environment (tmpdir, and a shinterp Env)
Intended to be used in 'with' context.
Expand All @@ -257,7 +258,8 @@ def __init__(
self._fallbackmatch = None
self._setup(tmpprefix)
self._lastout = ""
self._testcase = testcase
# feature names that can be tested by `hasfeature` for this single test case
self._testcase_names = list(_expand_testcase_names(testcase))

def atexit(self, func):
# register a function to be called during tearing down
Expand Down Expand Up @@ -401,12 +403,12 @@ def __enter__(self):
os.chdir(str(self.path))
shext.updateosenv(self.shenv.getexportedenv())

if self._testcase is not None:
if self._testcase in hghave.checks:
for name in self._testcase_names:
if name in hghave.checks:
raise RuntimeError(
f"test case {self._testcase} conflicts with an existing feature"
f"test case {name} conflicts with an existing feature"
)
hghave.checks[self._testcase] = (True, f"test case {self._testcase}")
hghave.checks[name] = (True, f"test case {name}")

return self

Expand All @@ -421,8 +423,8 @@ def __exit__(self, et, ev, tb):
os.chdir(self._origcwd)
shext.updateosenv(self._origenv)

if self._testcase is not None:
del hghave.checks[self._testcase]
for name in self._testcase_names:
del hghave.checks[name]

self._teardown()

Expand Down Expand Up @@ -574,3 +576,27 @@ def _applysubstitutions(self, out: str) -> str:
else:
out = re.sub(frompat, topat, out)
return out


def _expand_testcase_names(
value: Union[Iterable[str], str, bool, None],
) -> Iterable[str]:
"""Flatten and filter out None test case names.
>>> list(_expand_testcase_names(None))
[]
>>> list(_expand_testcase_names('a'))
['a']
>>> list(_expand_testcase_names(('a', None, False, 'b')))
['a', 'b']
>>> list(_expand_testcase_names(('a', ('b', 'c'))))
['a', 'b', 'c']
"""
if value is False or value is None:
return
elif isinstance(value, str):
yield value
elif value is True:
raise RuntimeError("_expand_testcase_names: invalid value: True")
else:
yield from chain(*map(_expand_testcase_names, value))

0 comments on commit 90ebb9e

Please sign in to comment.