-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathtest
More file actions
executable file
·101 lines (85 loc) · 3.03 KB
/
Copy pathtest
File metadata and controls
executable file
·101 lines (85 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env -S uv run -q --frozen --isolated --python 3.12 --group scripts python3
#
# GT4Py - GridTools Framework
#
# Copyright (c) 2014-2024, ETH Zurich
# All rights reserved.
#
# Please, refer to the LICENSE file in the root directory.
# SPDX-License-Identifier: BSD-3-Clause
#
"""
Unified scripts tests entry point.
Provides two sub-commands:
- ``python``: run pytest in ``scripts/tests/python``.
- ``sh``: run bats in ``scripts/tests/sh``.
Usage:
./scripts/test --help
./scripts/test python -- -k common
./scripts/test sh
"""
from __future__ import annotations
import pathlib
import subprocess
import sys
from typing import Annotated
import rich
import typer
if __name__ == "__main__":
try:
# First try to import the 'python' folder inside './scripts'
# and check that it is indeed the expected one.
import python
python_scripts_dir = str(pathlib.Path(__file__).resolve().absolute().parent / "python")
if not (len(python.__path__) == 1 and python.__path__[0] == python_scripts_dir):
print(
"ERROR: The 'scripts/python' package path does not match the expected path.\n"
"Please check the structure of the repository.\n",
file=sys.stderr,
)
sys.exit(1)
# Then add the 'python' folder to sys.path to mimic the `sys.path` of the
# command modules run directly as scripts.
sys.path.insert(0, python_scripts_dir)
from helpers import common
except ImportError as e:
print(
f"ERROR: '{e.name}' package cannot be imported.\n"
"Make sure 'uv' is installed in your system and run directly this script "
"as an executable file, to let 'uv' create a temporary venv with all the "
"required dependencies.\n",
file=sys.stderr,
)
sys.exit(127)
app = typer.Typer(
name="test",
help="Scripts tests toolbox.",
no_args_is_help=True,
)
@app.command("python")
def python_tests(
pytest_args: Annotated[
list[str] | None,
typer.Argument(help="Pytest arguments to forward. Pass them after '--'."),
] = None,
):
cmd = ["pytest", *(pytest_args or [])]
base_dir = common.SCRIPTS_DIR / "tests" / "python"
rich.print(f"Running: {base_dir} $ {' '.join(cmd)}\n")
result = subprocess.run(cmd, cwd=base_dir)
raise SystemExit(result.returncode)
@app.command("sh")
def sh_tests():
cmd = ["bats", str(common.SCRIPTS_DIR / "tests" / "sh")]
try:
result = subprocess.run(cmd, cwd=common.SCRIPTS_DIR.parent)
except FileNotFoundError:
print(
"ERROR: 'bats' executable cannot be found. Install bats-core to run shell tests.",
file=sys.stderr,
)
raise SystemExit(127) from None
raise SystemExit(result.returncode)
app()
else:
raise ImportError("This module is not meant to be imported. Please run it as a script.")