From cbc55427f20a61906ab44b08e3a55706ad62534b Mon Sep 17 00:00:00 2001 From: "Thing-han, Lim" <15379156+potsrevennil@users.noreply.github.com> Date: Wed, 22 May 2024 11:06:59 +0800 Subject: [PATCH 1/2] add -u --uart for specific tty Signed-off-by: Thing-han, Lim <15379156+potsrevennil@users.noreply.github.com> --- scripts/tests | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/scripts/tests b/scripts/tests index c383e6b..a8a4506 100755 --- a/scripts/tests +++ b/scripts/tests @@ -36,13 +36,15 @@ def serial_ports(): return ports -def usbdev(): - port = next(iter(serial_ports()), None) +def default_serial_port(): + return next(iter(serial_ports()), None) - if port is None: + +def usbdev(uart): + if uart is None: raise DeviceError("No available usb device") - dev = serial.Serial(port, 38400) + dev = serial.Serial(uart, 38400) return dev @@ -159,6 +161,7 @@ def base_test( expect_proc, actual_proc, verbose, + uart, ): """ test_type: test, speed, stack, nistkat @@ -206,7 +209,7 @@ def base_test( logging.info(f"Check {file} passed") if platform_cfg is not None: - dev = usbdev() + dev = usbdev(uart) for scheme in ["mlkem512", "mlkem768", "mlkem1024"]: file = f"elf/{scheme}-{test_type}.elf" @@ -246,6 +249,14 @@ _shared_options = [ type=bool, help="Show verbose output or not", ), + click.option( + "-u", + "--uart", + type=click.Path(), + show_default=True, + default=default_serial_port(), + help="TTY serial device for UART, default to the 1st serial device connected to your board or an empty string", + ), click.argument( "platform", nargs=1, @@ -269,9 +280,9 @@ def add_options(options): type=click.Path(), help="The binary hex file that you wanted to test.", ) -def run(platform, bin, verbose): +def run(platform, bin, verbose, uart): config_logger(True) - dev = usbdev() + dev = usbdev(uart) try: result = asyncio.run( @@ -289,7 +300,7 @@ def run(platform, bin, verbose): ) @add_options(_shared_options) @click.option("-i", "--iterations", default=1, type=int, help="Number of tests") -def func(platform, iterations, verbose): +def func(platform, iterations, verbose, uart): try: base_test( TEST_TYPE.TEST, @@ -298,6 +309,7 @@ def func(platform, iterations, verbose): lambda _: iterations * 3, lambda output: count(output, "OK"), verbose, + uart, ) except asyncio.CancelledError: pass @@ -306,7 +318,7 @@ def func(platform, iterations, verbose): @click.command(short_help="Run speed tests", context_settings={"show_default": True}) @add_options(_shared_options) @click.option("-i", "--iterations", default=1, type=int, help="Number of tests") -def speed(platform, iterations, verbose): +def speed(platform, iterations, verbose, uart): try: base_test( TEST_TYPE.SPEED, @@ -315,6 +327,7 @@ def speed(platform, iterations, verbose): lambda _: 1, lambda output: count(output, "OK"), verbose, + uart, ) except asyncio.CancelledError: pass @@ -322,7 +335,7 @@ def speed(platform, iterations, verbose): @click.command(short_help="Run stack tests", context_settings={"show_default": True}) @add_options(_shared_options) -def stack(platform, verbose): +def stack(platform, verbose, uart): try: base_test( TEST_TYPE.STACK, @@ -331,6 +344,7 @@ def stack(platform, verbose): lambda _: 1, lambda output: count(output, "OK"), verbose, + uart, ) except asyncio.CancelledError: pass @@ -338,7 +352,7 @@ def stack(platform, verbose): @click.command(short_help="Run nistkat tests", context_settings={"show_default": True}) @add_options(_shared_options) -def nistkat(platform, verbose): +def nistkat(platform, verbose, uart): def scheme_hash(scheme): result = subprocess.run( [ @@ -364,6 +378,7 @@ def nistkat(platform, verbose): scheme_hash, lambda output: str(output, encoding="utf-8").strip().lower(), verbose, + uart, ) except asyncio.CancelledError: pass From bb8859cd88782eb1ec9869bad729d1a35fe6f6af Mon Sep 17 00:00:00 2001 From: "Thing-han, Lim" <15379156+potsrevennil@users.noreply.github.com> Date: Wed, 22 May 2024 11:32:55 +0800 Subject: [PATCH 2/2] add black as python formatter Signed-off-by: Thing-han, Lim <15379156+potsrevennil@users.noreply.github.com> --- flake.nix | 1 + scripts/ci/lint | 9 ++++++++- scripts/format | 3 +++ scripts/tests | 1 - 4 files changed, 12 insertions(+), 2 deletions(-) diff --git a/flake.nix b/flake.nix index 136c18d..cadb2cb 100644 --- a/flake.nix +++ b/flake.nix @@ -38,6 +38,7 @@ yq; inherit (pkgs.python311Packages) + black pyserial# 3.5 click; }; diff --git a/scripts/ci/lint b/scripts/ci/lint index ff43932..81c012d 100755 --- a/scripts/ci/lint +++ b/scripts/ci/lint @@ -18,7 +18,7 @@ checkerr() if [[ ${#1} != 0 ]]; then echo "$1" | while read -r file line; do - echo "::error file={$file},line={${line:-1}},title={Format error}::$file require to be formatted" + echo "::error file=$file,line=${line:-1},title=Format error::$file require to be formatted" done SUCCESS=false fi @@ -35,6 +35,13 @@ echo "::group::Linting shell scripts with shfmt" checkerr "$(shfmt -s -l -i 2 -ci -fn $(shfmt -f $(git grep -l '' :/)))" echo "::endgroup::" +echo "::group::Linting python scripts with black" +if ! diff=$(black --check --diff -q --include scripts/tests "$ROOT"); then + echo "::error title=Format error::$diff" + SUCCESS=false +fi +echo "::endgroup::" + echo "::group::Linting c files with astyle" checkerr "$(astyle $(git ls-files ":/*.c" ":/*.h") --options="$ROOT/.astylerc" --dry-run --formatted | awk '{print $2}')" echo "::endgroup::" diff --git a/scripts/format b/scripts/format index 1d9478c..73e455e 100755 --- a/scripts/format +++ b/scripts/format @@ -24,6 +24,9 @@ nixpkgs-fmt "$ROOT" info "Formatting shell scripts" shfmt -s -w -l -i 2 -ci -fn $(shfmt -f $(git grep -l '' :/)) +info "Formatting python scripts" +black --include scripts/tests "$ROOT" + info "Formatting c files" astyle $(git ls-files ":/*.c" ":/*.h") --options="$ROOT/.astylerc" --formatted | awk '{print $2}' diff --git a/scripts/tests b/scripts/tests index a8a4506..f3b5a3a 100755 --- a/scripts/tests +++ b/scripts/tests @@ -120,7 +120,6 @@ class PLATFORM(Enum): class RecursiveNamespace(SimpleNamespace): - @staticmethod def map_entry(entry): if isinstance(entry, dict):