|
| 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