From 4f5ffa832e004f4d5f7423bdf26da574efa90f3e Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Mon, 13 Sep 2021 12:26:39 +0100 Subject: [PATCH] Add integration testing (#47) Tests subprocess-tee when used from inside molecule. --- .dockerignore | 5 +++++ .yamllint | 3 --- Dockerfile | 10 +++++++++- molecule/default/converge.yml | 8 ++++++++ molecule/default/molecule.yml | 11 +++++++++++ setup.cfg | 13 +++++++------ src/subprocess_tee/__init__.py | 7 +++++-- src/subprocess_tee/test/test_func.py | 15 +++++++++++++++ tox.ini | 8 ++++++-- 9 files changed, 66 insertions(+), 14 deletions(-) create mode 100644 .dockerignore create mode 100644 molecule/default/converge.yml create mode 100644 molecule/default/molecule.yml create mode 100644 src/subprocess_tee/test/test_func.py diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..4fe7547 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,5 @@ +.tox +**/*.pyc +.github +build +dist diff --git a/.yamllint b/.yamllint index 415452f..8827676 100644 --- a/.yamllint +++ b/.yamllint @@ -2,9 +2,6 @@ # Based on ansible-lint config extends: default -ignore: | - *{{cookiecutter** - rules: braces: max-spaces-inside: 1 diff --git a/Dockerfile b/Dockerfile index 11be040..288aeef 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,14 +2,22 @@ FROM alpine:latest # Alpine is used on purpose because it does not come with bash, and we # want to test that subprocess-tee works even on systems without bash shell. ENV BUILD_DEPS="\ +ansible-base \ +gcc \ git \ +libffi-dev \ +make \ +musl-dev \ python3 \ +python3-dev \ py3-pip \ +py3-ruamel.yaml \ " RUN \ apk add --update --no-cache \ -${BUILD_DEPS} +${BUILD_DEPS} && \ +pip3 install -U pip COPY . /root/code/ WORKDIR /root/code/ diff --git a/molecule/default/converge.yml b/molecule/default/converge.yml new file mode 100644 index 0000000..0eb8a5b --- /dev/null +++ b/molecule/default/converge.yml @@ -0,0 +1,8 @@ +--- +- name: Converge + hosts: localhost + gather_facts: false + tasks: + - name: "Test" + debug: + msg: "Past glories are poor feeding." diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml new file mode 100644 index 0000000..74c8557 --- /dev/null +++ b/molecule/default/molecule.yml @@ -0,0 +1,11 @@ +--- +dependency: + name: galaxy +driver: + name: delegated +platforms: + - name: instance +provisioner: + name: ansible +verifier: + name: ansible diff --git a/setup.cfg b/setup.cfg index 24af7a9..a35ca9e 100644 --- a/setup.cfg +++ b/setup.cfg @@ -66,12 +66,13 @@ setup_requires = [options.extras_require] test = - enrich>=1.2.5 - mock>=3.0.5 - pytest-cov>=2.7.1 - pytest-plus - pytest-xdist>=1.29.0 - pytest>=6.1.0 + enrich>=1.2.6 + mock>=4.0.3 + molecule>=3.4.0 # ansible is needed but no direct imports are made + pytest-cov>=2.12.1 + pytest-plus>=0.2 + pytest-xdist>=2.3.0 + pytest>=6.2.5 [options.packages.find] where = src diff --git a/src/subprocess_tee/__init__.py b/src/subprocess_tee/__init__.py index d12a62d..6e47f12 100644 --- a/src/subprocess_tee/__init__.py +++ b/src/subprocess_tee/__init__.py @@ -74,8 +74,11 @@ async def _stream_subprocess(args: str, **kwargs: Any) -> CompletedProcess: def tee_func(line: bytes, sink: List[str], pipe: Optional[Any]) -> None: line_str = line.decode("utf-8").rstrip() sink.append(line_str) - if not kwargs.get("quiet", False) and hasattr(pipe, "write"): - print(line_str, file=pipe) + if not kwargs.get("quiet", False): + if pipe and hasattr(pipe, "write"): + print(line_str, file=pipe) + else: + print(line_str) loop = asyncio.get_event_loop() tasks = [] diff --git a/src/subprocess_tee/test/test_func.py b/src/subprocess_tee/test/test_func.py new file mode 100644 index 0000000..c630aa3 --- /dev/null +++ b/src/subprocess_tee/test/test_func.py @@ -0,0 +1,15 @@ +"""Functional tests for subprocess-tee library.""" +import subprocess + + +def test_molecule() -> None: + """Ensures molecule does display output of its subprocesses.""" + result = subprocess.run( + ["molecule", "test"], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + universal_newlines=True, + check=False, + ) # type: ignore + assert result.returncode == 0 + assert "Past glories are poor feeding." in result.stdout diff --git a/tox.ini b/tox.ini index 2430848..7f366b2 100644 --- a/tox.ini +++ b/tox.ini @@ -3,7 +3,8 @@ minversion = 3.9.0 envlist = lint packaging - py{36,37,38,39} + deps + py isolated_build = True @@ -26,17 +27,20 @@ passenv = TERM setenv = PIP_DISABLE_VERSION_CHECK=1 - PYTEST_REQPASS=15 + PYTEST_REQPASS=16 PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1 commands = python -m pytest +deps = + ansible-core extras = test whitelist_externals = find rm sh +changedir = {toxinidir} [testenv:lint] description = Runs all linting tasks