|
| 1 | +import contextlib |
| 2 | +import re |
| 3 | +from dataclasses import dataclass |
| 4 | +from inspect import cleandoc |
| 5 | +from pathlib import Path |
| 6 | +from unittest.mock import ( |
| 7 | + Mock, |
| 8 | + call, |
| 9 | + patch, |
| 10 | +) |
| 11 | + |
| 12 | +import pytest |
| 13 | + |
| 14 | +from exasol.toolbox.nox._artifacts import copy_artifacts |
| 15 | + |
| 16 | + |
| 17 | +@contextlib.contextmanager |
| 18 | +def mock_session(path: Path, python_version: str, *files: str): |
| 19 | + with patch("exasol.toolbox.nox._artifacts.PROJECT_CONFIG") as config: |
| 20 | + config.python_versions = [python_version] |
| 21 | + for rel in files: |
| 22 | + file = path / rel |
| 23 | + file.parent.mkdir(parents=True, exist_ok=True) |
| 24 | + file.write_text(rel) |
| 25 | + yield Mock(posargs=[str(path)]) |
| 26 | + |
| 27 | + |
| 28 | +def test_missing_files(tmp_path, capsys): |
| 29 | + with mock_session(tmp_path, "9.9") as session: |
| 30 | + copy_artifacts(session) |
| 31 | + captured = capsys.readouterr() |
| 32 | + assert re.match( |
| 33 | + cleandoc( |
| 34 | + f""" |
| 35 | + Could not find any file .*/coverage-python9.9\\*/.coverage |
| 36 | + File not found .*/lint-python9.9/.lint.txt |
| 37 | + File not found .*/lint-python9.9/.lint.json |
| 38 | + File not found .*/security-python9.9/.security.json |
| 39 | + """ |
| 40 | + ), |
| 41 | + captured.err, |
| 42 | + ) |
| 43 | + |
| 44 | + |
| 45 | +@dataclass |
| 46 | +class endswith: |
| 47 | + """ |
| 48 | + Assert that the str representation of the argument ends with the |
| 49 | + specified suffix. |
| 50 | + """ |
| 51 | + |
| 52 | + suffix: str |
| 53 | + |
| 54 | + def __eq__(self, actual): |
| 55 | + return str(actual).endswith(self.suffix) |
| 56 | + |
| 57 | + |
| 58 | +def test_all_files(tmp_path, capsys): |
| 59 | + with mock_session( |
| 60 | + tmp_path / "artifacts", |
| 61 | + "9.9", |
| 62 | + "coverage-python9.9-fast/.coverage", |
| 63 | + "coverage-python9.9-slow/.coverage", |
| 64 | + "lint-python9.9/.lint.txt", |
| 65 | + "lint-python9.9/.lint.json", |
| 66 | + "security-python9.9/.security.json", |
| 67 | + ) as session: |
| 68 | + copy_artifacts(session) |
| 69 | + |
| 70 | + captured = capsys.readouterr() |
| 71 | + assert session.run.call_args == call( |
| 72 | + "coverage", |
| 73 | + "combine", |
| 74 | + "--keep", |
| 75 | + endswith("coverage-python9.9-fast/.coverage"), |
| 76 | + endswith("coverage-python9.9-slow/.coverage"), |
| 77 | + ) |
| 78 | + assert re.match( |
| 79 | + cleandoc( |
| 80 | + f""" |
| 81 | + Copying file .*/lint-python9.9/.lint.txt |
| 82 | + Copying file .*/lint-python9.9/.lint.json |
| 83 | + Copying file .*/security-python9.9/.security.json |
| 84 | + """ |
| 85 | + ), |
| 86 | + captured.err, |
| 87 | + ) |
| 88 | + for f in [".lint.txt", ".lint.json", ".security.json"]: |
| 89 | + assert (tmp_path / f).exists() |
0 commit comments