Skip to content

Commit dbf085f

Browse files
committed
Ensure listings are generated to a build folder.
1 parent 1e4a073 commit dbf085f

39 files changed

+288
-258
lines changed

.gitignore

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,5 @@ __pycache__/
55
*.profile
66
.cache
77

8-
*.bin
9-
*.elf
10-
*.exe
11-
*.hex
12-
*.oj
13-
*report.html
14-
158
dist
16-
test/listings
9+
build/

examples/.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
*.bin
3+
*.elf
4+
*.exe
5+
*.hex
6+
*.oj
7+
*report.html

test/helper_util.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,39 @@
1+
import logging
12
import os
23
import re
3-
import sys
4-
import subprocess
5-
import socket
6-
import time
74
import shutil
8-
import logging
5+
import socket
96
import string
7+
import subprocess
8+
import sys
9+
import time
1010
from contextlib import suppress
1111
from functools import lru_cache
12+
from pathlib import Path
1213

1314
# Store testdir for safe switch back to directory:
1415
testdir = os.path.dirname(os.path.abspath(__file__))
16+
test_path = Path(testdir)
1517
logger = logging.getLogger("util")
1618

1719

18-
def make_filename(s):
20+
def make_filename(s) -> Path:
1921
"""Remove all invalid characters from a string for a valid filename.
2022
2123
And create a directory if none present.
2224
"""
23-
folders = ["listings"]
25+
folders = ["..", "build", "listings"]
2426
parts = list(map(only_valid_chars, s.split(".")))
2527
assert parts
2628
folders.extend(parts[:-1])
2729
basename = parts[-1]
2830
assert basename.startswith("test_")
2931
basename = basename[5:]
3032
assert basename
31-
output_dir = relpath(*folders)
32-
if not os.path.exists(output_dir):
33-
os.makedirs(output_dir)
34-
return os.path.join(output_dir, basename)
33+
output_dir = Path(relpath(*folders))
34+
if not output_dir.exists():
35+
output_dir.mkdir(parents=True)
36+
return output_dir / basename
3537

3638

3739
def only_valid_chars(s):
@@ -43,10 +45,10 @@ def relpath(*args):
4345
return os.path.normpath(os.path.join(testdir, *args))
4446

4547

46-
def source_files(folder, extension):
47-
for filename in os.listdir(folder):
48-
if filename.endswith(extension):
49-
yield os.path.join(folder, filename)
48+
def source_files(folder: Path, extension):
49+
for filename in folder.iterdir():
50+
if str(filename).endswith(extension):
51+
yield folder / filename
5052

5153

5254
qemu_app = "qemu-system-arm"

test/lang/__init__.py

Whitespace-only changes.

test/lang/c/test_c_test_suite.py

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121
"""
2222

2323
import unittest
24-
import glob
2524
import fnmatch
2625
import argparse
2726
import io
2827
import os
2928
import logging
3029
import subprocess
30+
from pathlib import Path
3131

3232
from ppci.common import CompilerError, logformat
3333
from ppci import api
@@ -36,18 +36,18 @@
3636
from ppci.format.elf import write_elf
3737

3838

39-
this_dir = os.path.dirname(os.path.abspath(__file__))
39+
this_dir = Path(__file__).resolve().parent
40+
root_folder = this_dir.parent.parent.parent
41+
build_folder = root_folder / "build" / "c_test_suite"
4042
logger = logging.getLogger("c-test-suite")
4143

4244

4345
def c_test_suite_populate(cls):
4446
"""Enrich a unittest.TestCase with a function for each test snippet."""
4547
if "C_TEST_SUITE_DIR" in os.environ:
46-
c_test_suite_directory = os.path.normpath(
47-
os.environ["C_TEST_SUITE_DIR"]
48-
)
48+
suite_folder = Path(os.environ["C_TEST_SUITE_DIR"]).resolve()
4949

50-
for filename in get_test_snippets(c_test_suite_directory):
50+
for filename in get_test_snippets(suite_folder):
5151
create_test_function(cls, filename)
5252
else:
5353

@@ -60,26 +60,21 @@ def test_stub(self):
6060
return cls
6161

6262

63-
def get_test_snippets(c_test_suite_directory, name_filter="*"):
64-
snippet_folder = os.path.join(
65-
c_test_suite_directory, "tests", "single-exec"
66-
)
63+
def get_test_snippets(suite_folder: Path, name_filter="*"):
64+
snippet_folder = suite_folder / "tests" / "single-exec"
6765

6866
# Check if we have a folder:
69-
if not os.path.isdir(snippet_folder):
67+
if not snippet_folder.is_dir():
7068
raise ValueError(f"{snippet_folder} is not a directory")
7169

72-
for filename in sorted(glob.iglob(os.path.join(snippet_folder, "*.c"))):
73-
base_name = os.path.splitext(os.path.split(filename)[1])[0]
74-
75-
if fnmatch.fnmatch(base_name, name_filter):
70+
for filename in sorted(snippet_folder.glob("*.c")):
71+
if fnmatch.fnmatch(filename.stem, name_filter):
7672
yield filename
7773

7874

79-
def create_test_function(cls, filename):
75+
def create_test_function(cls, filename: Path):
8076
"""Create a test function for a single snippet"""
81-
snippet_filename = os.path.split(filename)[1]
82-
test_name = os.path.splitext(snippet_filename)[0]
77+
test_name = filename.stem
8378
test_name = test_name.replace(".", "_").replace("-", "_")
8479
test_function_name = "test_" + test_name
8580

@@ -91,24 +86,25 @@ def test_function(self):
9186
setattr(cls, test_function_name, test_function)
9287

9388

94-
def perform_test(filename):
89+
def perform_test(filename: Path):
9590
"""Try to compile the given snippet."""
9691
logger.info("Step 1: Compile %s!", filename)
9792
march = "x86_64"
9893

99-
html_report = os.path.splitext(filename)[0] + "_report.html"
94+
build_folder.mkdir(parents=True, exist_ok=True)
95+
96+
html_report = build_folder / (filename.stem + "_report.html")
10097

10198
coptions = COptions()
102-
root_folder = os.path.join(this_dir, "..", "..", "..")
103-
libc_folder = os.path.join(root_folder, "librt", "libc")
104-
libc_include = os.path.join(libc_folder, "include")
105-
coptions.add_include_path(libc_include)
99+
libc_folder = root_folder / "librt" / "libc"
100+
libc_include = libc_folder / "include"
101+
coptions.add_include_path(str(libc_include))
106102

107103
# TODO: this should be injected elsewhere?
108104
coptions.add_define("__LP64__", "1")
109105
# coptions.enable('freestanding')
110106

111-
with html_reporter(html_report) as reporter, open(filename) as f:
107+
with html_reporter(html_report) as reporter, filename.open() as f:
112108
try:
113109
obj1 = api.cc(f, march, coptions=coptions, reporter=reporter)
114110
except CompilerError as ex:
@@ -118,15 +114,19 @@ def perform_test(filename):
118114

119115
obj0 = api.asm(io.StringIO(STARTERCODE), march)
120116
obj2 = api.c3c([io.StringIO(BSP_C3_SRC)], [], march)
121-
with open(os.path.join(libc_include, "lib.c")) as f:
117+
with (libc_folder / "lib.c").open() as f:
122118
obj3 = api.cc(f, march, coptions=coptions)
123119

124-
obj = api.link([obj0, obj1, obj2, obj3], layout=io.StringIO(ARCH_MMAP))
120+
# with (libc_folder / "src" / "string" / "string.c").open() as f:
121+
# obj4 = api.cc(f, march, coptions=coptions)
122+
123+
objs = [obj0, obj1, obj2, obj3]
124+
obj = api.link(objs, layout=io.StringIO(ARCH_MMAP))
125125

126126
logger.info("Step 2: Run it!")
127127

128-
exe_filename = os.path.splitext(filename)[0] + "_executable.elf"
129-
with open(exe_filename, "wb") as f:
128+
exe_filename = build_folder / (filename.stem + "_executable.elf")
129+
with exe_filename.open("wb") as f:
130130
write_elf(obj, f, type="executable")
131131
api.chmod_x(exe_filename)
132132

@@ -136,7 +136,8 @@ def perform_test(filename):
136136
assert exit_code == 0
137137
captured_stdout = test_prog.stdout.read().decode("ascii")
138138

139-
with open(filename + ".expected") as f:
139+
expected_filename = filename.parent / (filename.stem + ".c.expected")
140+
with expected_filename.open() as f:
140141
expected_stdout = f.read()
141142

142143
# Compare stdout:
@@ -213,18 +214,18 @@ def main():
213214
logging.basicConfig(level=loglevel, format=logformat)
214215

215216
if args.folder is not None:
216-
suite_folder = args.folder
217+
suite_folder = Path(args.folder)
217218
elif "C_TEST_SUITE_DIR" in os.environ:
218-
suite_folder = os.environ["C_TEST_SUITE_DIR"]
219+
suite_folder = Path(os.environ["C_TEST_SUITE_DIR"])
219220
else:
220221
parser.print_help()
221-
print("ERROR: Specify where the c test suite is located!")
222+
logger.error("ERROR: Specify where the c test suite is located!")
222223
return 1
223224

224225
for filename in get_test_snippets(suite_folder, name_filter=args.filter):
225226
perform_test(filename)
226227

227-
print("OK.")
228+
logger.info("OK.")
228229

229230

230231
if __name__ == "__main__":

test/lang/fortran/test_fortran_test_suite.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,20 @@
99
1010
"""
1111

12+
import os
1213
import unittest
13-
import glob
14-
import os.path
14+
from pathlib import Path
1515

1616
from ppci.api import fortrancompile, get_arch
1717

1818

19-
def create_test_function(cls, filename):
19+
def create_test_function(cls, filename: Path):
2020
"""Create a test function for a single snippet"""
21-
_, snippet_filename = os.path.split(filename)
22-
test_function_name = "test_" + snippet_filename.replace(".", "_")
21+
test_function_name = "test_" + filename.stem.replace(".", "_")
2322

2423
def test_function(self):
2524
march = get_arch("arm")
26-
with open(filename) as f:
25+
with filename.open() as f:
2726
fortrancompile([f.read()], march)
2827
# TODO: check output for correct values:
2928

@@ -35,8 +34,8 @@ def test_function(self):
3534

3635
def populate(cls):
3736
if "FCVS_DIR" in os.environ:
38-
directory = os.path.normpath(os.environ["FCVS_DIR"])
39-
for filename in sorted(glob.iglob(os.path.join(directory, "*.FOR"))):
37+
path = Path(os.environ["FCVS_DIR"]).resolve()
38+
for filename in sorted(path.glob("*.FOR")):
4039
create_test_function(cls, filename)
4140
return cls
4241

test/lang/test_python.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from ppci.lang.python import load_py, python_to_ir
66
from ppci.utils.reporting import html_reporter
77

8+
from ..helper_util import make_filename
89

910
src1 = """
1011
def a(x: int, y: int) -> int:
@@ -71,7 +72,8 @@ def test_load_py(self):
7172
d = {}
7273
exec(src1, d)
7374
a = d["a"]
74-
with html_reporter("p2p_report.html") as reporter:
75+
html_filename = make_filename(self.id()).with_suffix(".html")
76+
with html_reporter(html_filename) as reporter:
7577
m2 = load_py(io.StringIO(src1), reporter=reporter)
7678

7779
for x in range(20):
@@ -88,7 +90,8 @@ def myprint(x: int) -> None:
8890
imports = {
8991
"myprint": myprint,
9092
}
91-
with html_reporter("p2p_callback_report.html") as reporter:
93+
html_filename = make_filename(self.id()).with_suffix(".html")
94+
with html_reporter(html_filename) as reporter:
9295
m2 = load_py(io.StringIO(src2), imports=imports, reporter=reporter)
9396
# Segfaults:
9497
m2.a(2)

0 commit comments

Comments
 (0)