From: Sarah Hoffmann Date: Mon, 4 Jan 2021 18:58:59 +0000 (+0100) Subject: bdd: move external process execution in separate func X-Git-Tag: v3.7.0~59^2~12 X-Git-Url: https://git.openstreetmap.org./nominatim.git/commitdiff_plain/72587b08fa66d70930b279a7ed96a945ed6c27e7 bdd: move external process execution in separate func --- diff --git a/test/bdd/steps/nominatim_environment.py b/test/bdd/steps/nominatim_environment.py index 4c973358..7741d667 100644 --- a/test/bdd/steps/nominatim_environment.py +++ b/test/bdd/steps/nominatim_environment.py @@ -1,13 +1,11 @@ -import logging import os from pathlib import Path -import subprocess import tempfile import psycopg2 import psycopg2.extras -LOG = logging.getLogger(__name__) +from steps.utils import run_script class NominatimEnvironment: """ Collects all functions for the execution of Nominatim functions. @@ -216,9 +214,4 @@ class NominatimEnvironment: else: cwd = self.build_dir - proc = subprocess.Popen(cmd, cwd=cwd, env=self.test_env, - stdout=subprocess.PIPE, stderr=subprocess.PIPE) - (outp, outerr) = proc.communicate() - outerr = outerr.decode('utf-8').replace('\\n', '\n') - LOG.debug("run_nominatim_script: %s\n%s\n%s", cmd, outp, outerr) - assert (proc.returncode == 0), "Script '%s' failed:\n%s\n%s\n" % (script, outp, outerr) + run_script(cmd, cwd=cwd, env=self.test_env) diff --git a/test/bdd/steps/queries.py b/test/bdd/steps/queries.py index 0ea4685b..c7fdd6db 100644 --- a/test/bdd/steps/queries.py +++ b/test/bdd/steps/queries.py @@ -10,11 +10,11 @@ import io import re import logging import xml.etree.ElementTree as ET -import subprocess from urllib.parse import urlencode from collections import OrderedDict from check_functions import Almost +from utils import run_script logger = logging.getLogger(__name__) @@ -277,14 +277,9 @@ def query_cmd(context, query, dups): if dups: cmd.extend(('--dedupe', '0')) - proc = subprocess.Popen(cmd, cwd=context.nominatim.build_dir, - stdout=subprocess.PIPE, stderr=subprocess.PIPE) - (outp, err) = proc.communicate() + outp, err = run_script(cmd, cwd=context.nominatim.build_dir) - assert proc.returncode == 0, "query.php failed with message: %s\noutput: %s" % (err, outp) - logger.debug("run_nominatim_script: %s\n%s\n" % (cmd, outp.decode('utf-8').replace('\\n', '\n'))) - - context.response = SearchResponse(outp.decode('utf-8'), 'json') + context.response = SearchResponse(outp, 'json') def send_api_query(endpoint, params, fmt, context): if fmt is not None: @@ -326,19 +321,7 @@ def send_api_query(endpoint, params, fmt, context): for k,v in params.items(): cmd.append("%s=%s" % (k, v)) - proc = subprocess.Popen(cmd, cwd=context.nominatim.website_dir.name, env=env, - stdout=subprocess.PIPE, stderr=subprocess.PIPE) - - (outp, err) = proc.communicate() - outp = outp.decode('utf-8') - err = err.decode("utf-8") - - logger.debug("Result: \n===============================\n" - + outp + "\n===============================\n") - - assert proc.returncode == 0, \ - "%s failed with message: %s" % ( - os.path.basename(env['SCRIPT_FILENAME']), err) + outp, err = run_script(cmd, cwd=context.nominatim.website_dir.name, env=env) assert len(err) == 0, "Unexpected PHP error: %s" % (err) diff --git a/test/bdd/steps/utils.py b/test/bdd/steps/utils.py new file mode 100644 index 00000000..64d020db --- /dev/null +++ b/test/bdd/steps/utils.py @@ -0,0 +1,22 @@ +""" +Various smaller helps for step execution. +""" +import logging +import subprocess + +LOG = logging.getLogger(__name__) + +def run_script(cmd, **kwargs): + """ Run the given command, check that it is successful and output + when necessary. + """ + proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, + **kwargs) + (outp, outerr) = proc.communicate() + outp = outp.decode('utf-8') + outerr = outerr.decode('utf-8').replace('\\n', '\n') + LOG.debug("Run command: %s\n%s\n%s", cmd, outp, outerr) + + assert proc.returncode == 0, "Script '{}' failed:\n{}\n{}\n".format(cmd[0], outp, outerr) + + return outp, outerr