2121"""
2222
2323import unittest
24- import glob
2524import fnmatch
2625import argparse
2726import io
2827import os
2928import logging
3029import subprocess
30+ from pathlib import Path
3131
3232from ppci .common import CompilerError , logformat
3333from ppci import api
3636from 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"
4042logger = logging .getLogger ("c-test-suite" )
4143
4244
4345def 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
230231if __name__ == "__main__" :
0 commit comments