Skip to content

Commit b48a0fd

Browse files
committed
Added tests
1 parent 2963ae4 commit b48a0fd

File tree

2 files changed

+68
-20
lines changed

2 files changed

+68
-20
lines changed

exasol/toolbox/nox/_artifacts.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ def get_suffix() -> str:
137137
_combine_coverage(session, dir, f"coverage{suffix}*/.coverage")
138138
_copy_artifacts(
139139
dir,
140+
dir.parent,
140141
f"lint{suffix}/.lint.txt",
141142
f"lint{suffix}/.lint.json",
142143
f"security{suffix}/.security.json",
@@ -154,16 +155,16 @@ def _combine_coverage(session: Session, dir: Path, pattern: str):
154155
pattern: glob pattern, e.g. "*.coverage"
155156
"""
156157
if args := [f for f in dir.glob(pattern) if f.exists()]:
157-
session.run("coverage", "combine", "--keep", *args)
158+
session.run("coverage", "combine", "--keep", *sorted(args))
158159
else:
159160
print(f"Could not find any file {dir}/{pattern}", file=sys.stderr)
160161

161162

162-
def _copy_artifacts(dir: Path, *files: str):
163+
def _copy_artifacts(source: Path, dest: Path, *files: str):
163164
for file in files:
164-
path = dir / file
165+
path = source / file
165166
if path.exists():
166167
print(f"Copying file {path}", file=sys.stderr)
167-
shutil.copy(path, ".")
168+
shutil.copy(path, dest)
168169
else:
169170
print(f"File not found {path}", file=sys.stderr)

test/unit/artifacts_test.py

Lines changed: 63 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
import pytest
2+
import contextlib
3+
from dataclasses import dataclass
4+
from pathlib import Path
25
import re
36
from inspect import cleandoc
4-
from unittest.mock import Mock, patch
7+
from unittest.mock import Mock, patch, call
58
from exasol.toolbox.nox._artifacts import copy_artifacts
69

710

8-
9-
@pytest.fixture
10-
def python_version():
11-
return "9.9"
12-
13-
14-
@pytest.fixture
15-
def project_config(python_version):
11+
@contextlib.contextmanager
12+
def mock_session(path: Path, python_version: str, *files: str):
1613
with patch("exasol.toolbox.nox._artifacts.PROJECT_CONFIG") as config:
1714
config.python_versions = [python_version]
18-
yield config
15+
for rel in files:
16+
file = path / rel
17+
file.parent.mkdir(parents=True, exist_ok=True)
18+
file.write_text(rel)
19+
yield Mock(posargs=[str(path)])
1920

2021

21-
def test_no_coverage(project_config, tmp_path, capsys):
22-
session = Mock(posargs=[str(tmp_path)])
23-
copy_artifacts(session)
22+
def test_missing_files(tmp_path, capsys):
23+
with mock_session(tmp_path, "9.9") as session:
24+
copy_artifacts(session)
2425
captured = capsys.readouterr()
25-
re.match(
26+
assert re.match(
2627
cleandoc(
2728
f"""
2829
Could not find any file .*/coverage-python9.9\\*/.coverage
@@ -33,5 +34,51 @@ def test_no_coverage(project_config, tmp_path, capsys):
3334
),
3435
captured.err,
3536
)
36-
with capsys.disabled():
37-
print(captured.err)
37+
#with capsys.disabled():
38+
# print(captured.err)
39+
40+
41+
@dataclass
42+
class endswith:
43+
"""
44+
Assert that the str representation of the argument ends with the
45+
specfied suffix.
46+
"""
47+
suffix: str
48+
49+
def __eq__(self, actual):
50+
return str(actual).endswith(self.suffix)
51+
52+
53+
def test_all_files(tmp_path, capsys):
54+
with mock_session(
55+
tmp_path / "artifacts",
56+
"9.9",
57+
"coverage-python9.9-fast/.coverage",
58+
"coverage-python9.9-slow/.coverage",
59+
"lint-python9.9/.lint.txt",
60+
"lint-python9.9/.lint.json",
61+
"security-python9.9/.security.json"
62+
) as session:
63+
copy_artifacts(session)
64+
65+
captured = capsys.readouterr()
66+
assert session.run.call_args == call (
67+
"coverage",
68+
"combine",
69+
"--keep",
70+
endswith("coverage-python9.9-fast/.coverage"),
71+
endswith("coverage-python9.9-slow/.coverage"),
72+
)
73+
assert re.match(
74+
cleandoc(
75+
f"""
76+
Copying file .*/lint-python9.9/.lint.txt
77+
Copying file .*/lint-python9.9/.lint.json
78+
Copying file .*/security-python9.9/.security.json
79+
"""
80+
),
81+
captured.err,
82+
)
83+
for f in [".lint.txt", ".lint.json", ".security.json"]:
84+
assert (tmp_path / f).exists()

0 commit comments

Comments
 (0)