Skip to content

Commit 02d6d9e

Browse files
authored
Add flag to tests scripts to specfic tty device (#40)
* add -u --uart for specific tty Signed-off-by: Thing-han, Lim <15379156+potsrevennil@users.noreply.github.com> * add black as python formatter Signed-off-by: Thing-han, Lim <15379156+potsrevennil@users.noreply.github.com> --------- Signed-off-by: Thing-han, Lim <15379156+potsrevennil@users.noreply.github.com>
1 parent 530c346 commit 02d6d9e

File tree

4 files changed

+38
-13
lines changed

4 files changed

+38
-13
lines changed

flake.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
yq;
3939

4040
inherit (pkgs.python311Packages)
41+
black
4142
pyserial# 3.5
4243
click;
4344
};

scripts/ci/lint

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ checkerr()
1818

1919
if [[ ${#1} != 0 ]]; then
2020
echo "$1" | while read -r file line; do
21-
echo "::error file={$file},line={${line:-1}},title={Format error}::$file require to be formatted"
21+
echo "::error file=$file,line=${line:-1},title=Format error::$file require to be formatted"
2222
done
2323
SUCCESS=false
2424
fi
@@ -35,6 +35,13 @@ echo "::group::Linting shell scripts with shfmt"
3535
checkerr "$(shfmt -s -l -i 2 -ci -fn $(shfmt -f $(git grep -l '' :/)))"
3636
echo "::endgroup::"
3737

38+
echo "::group::Linting python scripts with black"
39+
if ! diff=$(black --check --diff -q --include scripts/tests "$ROOT"); then
40+
echo "::error title=Format error::$diff"
41+
SUCCESS=false
42+
fi
43+
echo "::endgroup::"
44+
3845
echo "::group::Linting c files with astyle"
3946
checkerr "$(astyle $(git ls-files ":/*.c" ":/*.h") --options="$ROOT/.astylerc" --dry-run --formatted | awk '{print $2}')"
4047
echo "::endgroup::"

scripts/format

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ nixpkgs-fmt "$ROOT"
2424
info "Formatting shell scripts"
2525
shfmt -s -w -l -i 2 -ci -fn $(shfmt -f $(git grep -l '' :/))
2626

27+
info "Formatting python scripts"
28+
black --include scripts/tests "$ROOT"
29+
2730
info "Formatting c files"
2831
astyle $(git ls-files ":/*.c" ":/*.h") --options="$ROOT/.astylerc" --formatted | awk '{print $2}'
2932

scripts/tests

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,15 @@ def serial_ports():
3636
return ports
3737

3838

39-
def usbdev():
40-
port = next(iter(serial_ports()), None)
39+
def default_serial_port():
40+
return next(iter(serial_ports()), None)
4141

42-
if port is None:
42+
43+
def usbdev(uart):
44+
if uart is None:
4345
raise DeviceError("No available usb device")
4446

45-
dev = serial.Serial(port, 38400)
47+
dev = serial.Serial(uart, 38400)
4648
return dev
4749

4850

@@ -118,7 +120,6 @@ class PLATFORM(Enum):
118120

119121

120122
class RecursiveNamespace(SimpleNamespace):
121-
122123
@staticmethod
123124
def map_entry(entry):
124125
if isinstance(entry, dict):
@@ -159,6 +160,7 @@ def base_test(
159160
expect_proc,
160161
actual_proc,
161162
verbose,
163+
uart,
162164
):
163165
"""
164166
test_type: test, speed, stack, nistkat
@@ -206,7 +208,7 @@ def base_test(
206208
logging.info(f"Check {file} passed")
207209

208210
if platform_cfg is not None:
209-
dev = usbdev()
211+
dev = usbdev(uart)
210212

211213
for scheme in ["mlkem512", "mlkem768", "mlkem1024"]:
212214
file = f"elf/{scheme}-{test_type}.elf"
@@ -246,6 +248,14 @@ _shared_options = [
246248
type=bool,
247249
help="Show verbose output or not",
248250
),
251+
click.option(
252+
"-u",
253+
"--uart",
254+
type=click.Path(),
255+
show_default=True,
256+
default=default_serial_port(),
257+
help="TTY serial device for UART, default to the 1st serial device connected to your board or an empty string",
258+
),
249259
click.argument(
250260
"platform",
251261
nargs=1,
@@ -269,9 +279,9 @@ def add_options(options):
269279
type=click.Path(),
270280
help="The binary hex file that you wanted to test.",
271281
)
272-
def run(platform, bin, verbose):
282+
def run(platform, bin, verbose, uart):
273283
config_logger(True)
274-
dev = usbdev()
284+
dev = usbdev(uart)
275285

276286
try:
277287
result = asyncio.run(
@@ -289,7 +299,7 @@ def run(platform, bin, verbose):
289299
)
290300
@add_options(_shared_options)
291301
@click.option("-i", "--iterations", default=1, type=int, help="Number of tests")
292-
def func(platform, iterations, verbose):
302+
def func(platform, iterations, verbose, uart):
293303
try:
294304
base_test(
295305
TEST_TYPE.TEST,
@@ -298,6 +308,7 @@ def func(platform, iterations, verbose):
298308
lambda _: iterations * 3,
299309
lambda output: count(output, "OK"),
300310
verbose,
311+
uart,
301312
)
302313
except asyncio.CancelledError:
303314
pass
@@ -306,7 +317,7 @@ def func(platform, iterations, verbose):
306317
@click.command(short_help="Run speed tests", context_settings={"show_default": True})
307318
@add_options(_shared_options)
308319
@click.option("-i", "--iterations", default=1, type=int, help="Number of tests")
309-
def speed(platform, iterations, verbose):
320+
def speed(platform, iterations, verbose, uart):
310321
try:
311322
base_test(
312323
TEST_TYPE.SPEED,
@@ -315,14 +326,15 @@ def speed(platform, iterations, verbose):
315326
lambda _: 1,
316327
lambda output: count(output, "OK"),
317328
verbose,
329+
uart,
318330
)
319331
except asyncio.CancelledError:
320332
pass
321333

322334

323335
@click.command(short_help="Run stack tests", context_settings={"show_default": True})
324336
@add_options(_shared_options)
325-
def stack(platform, verbose):
337+
def stack(platform, verbose, uart):
326338
try:
327339
base_test(
328340
TEST_TYPE.STACK,
@@ -331,14 +343,15 @@ def stack(platform, verbose):
331343
lambda _: 1,
332344
lambda output: count(output, "OK"),
333345
verbose,
346+
uart,
334347
)
335348
except asyncio.CancelledError:
336349
pass
337350

338351

339352
@click.command(short_help="Run nistkat tests", context_settings={"show_default": True})
340353
@add_options(_shared_options)
341-
def nistkat(platform, verbose):
354+
def nistkat(platform, verbose, uart):
342355
def scheme_hash(scheme):
343356
result = subprocess.run(
344357
[
@@ -364,6 +377,7 @@ def nistkat(platform, verbose):
364377
scheme_hash,
365378
lambda output: str(output, encoding="utf-8").strip().lower(),
366379
verbose,
380+
uart,
367381
)
368382
except asyncio.CancelledError:
369383
pass

0 commit comments

Comments
 (0)