Skip to content

Commit 1085d24

Browse files
committed
Added dry run logic to compile CLI command and added pytests
1 parent d14d20f commit 1085d24

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

quantcrypt/internal/cli/commands/compile.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ def command_compile(
4040
algos = const.SupportedAlgos.filter(algorithms)
4141

4242
variants = [const.PQAVariant.REF]
43-
if with_opt:
43+
if with_opt: # pragma: no cover
4444
arch = platform.machine().lower()
4545
if arch in const.AMDArches:
4646
variants.append(const.PQAVariant.OPT_AMD)
4747
elif arch in const.ARMArches:
4848
variants.append(const.PQAVariant.OPT_ARM)
49-
else: # pragma: no branch
49+
else:
5050
console.raise_error("This machine does not support optimized variants.")
5151

5252
variants_fmt = 'only the [italic tan]clean[/]'
@@ -60,6 +60,11 @@ def command_compile(
6060
if not non_interactive:
6161
console.ask_continue(exit_on_false=True)
6262

63+
if dry_run:
64+
console.styled_print("QuantCrypt would have compiled the following algorithms:")
65+
console.pretty_print(', '.join(s.armor_name() for s in algos))
66+
return
67+
6368
console.styled_print("\nInitializing compilation[grey46]...[/]\n")
6469
process = Compiler.run(variants, algos, in_subprocess=True)
6570

tests/test_cli/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ def closure(
8585
decrypt=cmd.dec_app,
8686
sign=cmd.sign_app,
8787
verify=cmd.verify_app,
88+
compile=cmd.compile_app,
8889
)[command]
8990
result = runner.invoke(_app, options, input=user_input)
9091
if debug:

tests/test_cli/test_compile.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#
2+
# MIT License
3+
#
4+
# Copyright (c) 2024, Mattias Aabmets
5+
#
6+
# The contents of this file are subject to the terms and conditions defined in the License.
7+
# You may not use, modify, or distribute this file except in compliance with the License.
8+
#
9+
# SPDX-License-Identifier: MIT
10+
#
11+
12+
from textwrap import dedent
13+
from quantcrypt.internal import constants as const
14+
from quantcrypt.internal.cli.commands import compile
15+
from .conftest import CLIMessages
16+
17+
18+
class MockedProcess:
19+
def __init__(self, returncode: int):
20+
self._returncode = returncode
21+
22+
@property
23+
def stdout(self) -> str:
24+
return dedent(f"""
25+
Some garbage output from CFFI...
26+
{const.SubprocTag}Compiling clean variant of MLKEM512...
27+
More garbage output from CFFI...
28+
""")
29+
30+
@property
31+
def returncode(self) -> int:
32+
return int(self._returncode)
33+
34+
def wait(self) -> None:
35+
return
36+
37+
38+
class MockedCompiler:
39+
_returncode = 1
40+
41+
@classmethod
42+
def run(cls, *_, **kwargs) -> MockedProcess:
43+
assert "in_subprocess" in kwargs and kwargs["in_subprocess"] is True
44+
cls._returncode = not cls._returncode
45+
return MockedProcess(cls._returncode)
46+
47+
48+
def test_compile(cli_runner, alt_tmp_path, monkeypatch) -> None:
49+
monkeypatch.setattr(compile, "Compiler", MockedCompiler)
50+
51+
cli_runner("compile", ['mlkem512'], "n\n", CLIMessages.CANCELLED)
52+
cli_runner("compile", ['mlkem512'], "y\n", CLIMessages.SUCCESS)
53+
cli_runner("compile", ['-D', 'mlkem512'], "y\n", CLIMessages.DRYRUN)
54+
cli_runner("compile", ['-N', 'mlkem512'], "", CLIMessages.ERROR)
55+
cli_runner("compile", ['-N', 'mlkem512'], "", CLIMessages.SUCCESS)

0 commit comments

Comments
 (0)