2023-06-08 22:32:59 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
# Copyright (c) 2016 pkgconf authors (see AUTHORS).
|
|
|
|
#
|
|
|
|
# Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
# purpose with or without fee is hereby granted, provided that the above
|
|
|
|
# copyright notice and this permission notice appear in all copies.
|
|
|
|
#
|
|
|
|
# This software is provided 'as is' and without any warranty, express or
|
|
|
|
# implied. In no event shall the authors be liable for any damages arising
|
|
|
|
# from the use of this software.
|
|
|
|
|
|
|
|
"""Simple runner for test cases."""
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
2023-06-08 23:10:16 +00:00
|
|
|
import asyncio
|
2023-06-08 22:32:59 +00:00
|
|
|
import dataclasses
|
2023-06-08 23:10:16 +00:00
|
|
|
import os
|
2023-06-08 22:32:59 +00:00
|
|
|
import sys
|
|
|
|
import typing
|
|
|
|
|
|
|
|
try:
|
|
|
|
import tomllib
|
|
|
|
except ImportError:
|
|
|
|
import tomli as tomllib
|
|
|
|
|
|
|
|
if typing.TYPE_CHECKING:
|
|
|
|
|
|
|
|
class TestDefinition(typing.TypedDict, total=False):
|
|
|
|
exitcode: int
|
2023-06-08 23:10:16 +00:00
|
|
|
stdout: str
|
|
|
|
stderr: str
|
|
|
|
args: typing.List[str]
|
|
|
|
env: typing.Dict[str, str]
|
2023-06-08 22:32:59 +00:00
|
|
|
|
|
|
|
class Arguments(typing.Protocol):
|
2023-06-08 23:10:16 +00:00
|
|
|
suites: typing.List[str]
|
2023-06-08 22:32:59 +00:00
|
|
|
pkgconf: str
|
|
|
|
verbose: bool
|
|
|
|
|
|
|
|
|
2023-06-08 23:10:16 +00:00
|
|
|
TEST_ROOT = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
|
|
|
|
|
2023-06-08 22:32:59 +00:00
|
|
|
@dataclasses.dataclass
|
|
|
|
class Result:
|
|
|
|
|
|
|
|
name: str
|
2023-06-08 23:10:16 +00:00
|
|
|
success: bool = False
|
2023-06-08 22:32:59 +00:00
|
|
|
reason: typing.Optional[str] = None
|
|
|
|
|
|
|
|
|
2023-06-08 23:10:16 +00:00
|
|
|
def interpolate(input: str) -> str:
|
|
|
|
return input.format(
|
|
|
|
test_root=TEST_ROOT,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-06-08 22:32:59 +00:00
|
|
|
async def run_test(pkgconf: str, name: str, test: TestDefinition, verbose: bool) -> Result:
|
2023-06-08 23:10:16 +00:00
|
|
|
|
|
|
|
env: typing.Dict[str, str] = {}
|
|
|
|
if (renv := test.get('env', None)) is not None:
|
|
|
|
env = {k: interpolate(v) for k, v in renv.items()}
|
2023-06-08 22:32:59 +00:00
|
|
|
|
|
|
|
proc = await asyncio.create_subprocess_exec(
|
2023-06-08 23:10:16 +00:00
|
|
|
pkgconf, *test.get('args', []),
|
|
|
|
stdout=asyncio.subprocess.PIPE if 'stdout' in test is not None else asyncio.subprocess.DEVNULL,
|
|
|
|
stderr=asyncio.subprocess.PIPE if 'stderr' in test is not None else asyncio.subprocess.DEVNULL,
|
|
|
|
env=env)
|
2023-06-08 22:32:59 +00:00
|
|
|
|
2023-06-08 23:10:16 +00:00
|
|
|
rout, rerr = await proc.communicate()
|
|
|
|
out = rout.decode() if rout is not None else ''
|
|
|
|
err = rerr.decode() if rerr is not None else ''
|
2023-06-08 22:32:59 +00:00
|
|
|
|
|
|
|
result = Result(name)
|
|
|
|
|
2023-06-08 23:10:16 +00:00
|
|
|
if (ret := test.get('exitcode', 0)) and proc.returncode != ret:
|
2023-06-08 22:32:59 +00:00
|
|
|
result.reason = f"Return code was {proc.returncode}, but expected {ret}"
|
2023-06-08 23:10:16 +00:00
|
|
|
elif (exp := test.get('stdout')) is not None and out != exp:
|
|
|
|
result.reason = f"Stdout was {out}, but expected {exp}"
|
|
|
|
elif (exp := test.get('stderr')) is not None and err != exp:
|
|
|
|
result.reason = f"Stdout was {err}, but expected {err}"
|
|
|
|
else:
|
|
|
|
result.success = True
|
2023-06-08 22:32:59 +00:00
|
|
|
|
|
|
|
if verbose:
|
|
|
|
if result.success:
|
|
|
|
print(f"{name}: passed")
|
|
|
|
else:
|
|
|
|
print(f"{name}: failed\n reason: {result.reason}")
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
async def run(args: Arguments) -> None:
|
2023-06-08 23:10:16 +00:00
|
|
|
tests: typing.List[typing.Coroutine[typing.Any, typing.Any, Result]] = []
|
|
|
|
for suite in args.suites:
|
|
|
|
with open(suite, 'rb') as f:
|
|
|
|
suite: typing.Dict[str, TestDefinition] = tomllib.load(f)
|
2023-06-08 22:32:59 +00:00
|
|
|
|
2023-06-08 23:10:16 +00:00
|
|
|
tests.extend(
|
|
|
|
[run_test(args.pkgconf, n, s, args.verbose) for n, s in suite.items()])
|
|
|
|
|
|
|
|
results = await asyncio.gather(*tests)
|
2023-06-08 22:32:59 +00:00
|
|
|
|
|
|
|
return all(r.success for r in results)
|
|
|
|
|
|
|
|
|
|
|
|
def main() -> None:
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument('pkgconf', help="Path to built pkgconf executable")
|
2023-06-08 23:10:16 +00:00
|
|
|
parser.add_argument('suites', nargs="+", help="One or more toml test suite definition")
|
2023-06-08 22:32:59 +00:00
|
|
|
parser.add_argument('-v', '--verbose', action='store_true',
|
|
|
|
help="Print more information while running")
|
|
|
|
args: Arguments = parser.parse_args()
|
|
|
|
|
|
|
|
loop = asyncio.new_event_loop()
|
|
|
|
success = loop.run_until_complete(run(args))
|
|
|
|
|
|
|
|
sys.exit(int(not success))
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|