From ffe656e94a3211ee6a4b0e9f4606d560be65a4d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Alvergnat?= Date: Tue, 29 Jan 2019 12:27:03 +0100 Subject: [PATCH] Add docker_compose_subprocess_kwargs fixture support This allow custom options be passed to underlying subprocess.check_output invocations, like cwd. @pytest.fixture(scope='session') def docker_compose_subprocess_kwargs(): return {'cwd': '..'} --- src/pytest_docker/__init__.py | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/pytest_docker/__init__.py b/src/pytest_docker/__init__.py index 567ec9f..3cdb949 100644 --- a/src/pytest_docker/__init__.py +++ b/src/pytest_docker/__init__.py @@ -10,11 +10,11 @@ import timeit -def execute(command, success_codes=(0,)): +def execute(command, success_codes=(0,), **kwargs): """Run a shell command.""" try: output = subprocess.check_output( - command, stderr=subprocess.STDOUT, shell=True, + command, **kwargs, ) status = 0 except subprocess.CalledProcessError as error: @@ -29,6 +29,16 @@ def execute(command, success_codes=(0,)): return output +@pytest.fixture(scope='session') +def docker_compose_subprocess_default_kwargs(): + return {'stderr': subprocess.STDOUT, 'shell': True} + + +@pytest.fixture(scope='session') +def docker_compose_subprocess_kwargs(): + return {} + + @pytest.fixture(scope='session') def docker_ip(): """Determine IP address for TCP connections to Docker containers.""" @@ -106,13 +116,14 @@ def str_to_list(arg): class DockerComposeExecutor(object): _compose_files = attr.ib(convert=str_to_list) _compose_project_name = attr.ib() + _compose_subprocess_kwargs = attr.ib() def execute(self, subcommand): command = "docker-compose" for compose_file in self._compose_files: command += ' -f "{}"'.format(compose_file) command += ' -p "{}" {}'.format(self._compose_project_name, subcommand) - return execute(command) + return execute(command, **self._compose_subprocess_kwargs) @pytest.fixture(scope='session') @@ -140,12 +151,16 @@ def docker_compose_project_name(): @pytest.fixture(scope='session') def docker_services( - docker_compose_file, docker_compose_project_name + docker_compose_file, docker_compose_project_name, docker_compose_subprocess_default_kwargs, + docker_compose_subprocess_kwargs ): """Ensure all Docker-based services are up and running.""" + subprocess_kwargs = {**docker_compose_subprocess_default_kwargs} + subprocess_kwargs.update(docker_compose_subprocess_kwargs) + docker_compose = DockerComposeExecutor( - docker_compose_file, docker_compose_project_name + docker_compose_file, docker_compose_project_name, subprocess_kwargs ) # Spawn containers.