1"""Provides common functionality to the test scripts."""
2
3import os
4import sys
5from pathlib import Path
6
7def set_source(source):
8    """Checks whether the source file exists and returns its path."""
9    if not Path(source).is_file():
10        die(source)
11    return Path(source)
12
13def set_executable(executable):
14    """Checks whether a Flang executable has been set and returns its path."""
15    flang_fc1 = Path(executable)
16    if not flang_fc1.is_file():
17        die(flang_fc1)
18    return str(flang_fc1)
19
20def set_temp(tmp):
21    """Sets a temporary directory or creates one if it doesn't exist."""
22    os.makedirs(Path(tmp), exist_ok=True)
23    return Path(tmp)
24
25def die(file=None):
26    """Used in other functions."""
27    if file is None:
28        print(f"{sys.argv[0]}: FAIL")
29    else:
30        print(f"{sys.argv[0]}: File not found: {file}")
31    sys.exit(1)
32
33def check_args(args):
34    """Verifies that 2 arguments have been passed."""
35    if len(args) < 3:
36        print(f"Usage: {args[0]} <fortran-source> <flang-command>")
37        sys.exit(1)
38
39def check_args_long(args):
40    """Verifies that 3 arguments have been passed."""
41    if len(args) < 4:
42        print(f"Usage: {args[0]} <fortran-source> <temp-test-dir> <flang-command>")
43        sys.exit(1)
44
45