From 568477b6afc22c18335db450c28ef77e97690980 Mon Sep 17 00:00:00 2001 From: Wolfgang Walther Date: Wed, 30 Dec 2020 14:26:09 +0100 Subject: [PATCH] Fix io tests not collecting coverage data when using run() Before, postgrest was killed after each test and no coverage data was saved. Now the process is terminated gracefully via SIGTERM first to allow writing the .tix file. --- nix/tests.nix | 8 +++++--- test/io-tests/test_io.py | 24 +++++++++++++++++++----- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/nix/tests.nix b/nix/tests.nix index 93a2395b8..1db5fe102 100644 --- a/nix/tests.nix +++ b/nix/tests.nix @@ -184,8 +184,10 @@ let # temporary directory to collect data in tmpdir="$(mktemp -d)" - # we keep the tmpdir when an error occurs for debugging and only remove it on success - trap 'echo Temporary directory kept at: $tmpdir' ERR SIGINT SIGTERM + # we keep the tmpdir when an error occurs for debugging + trap 'echo Temporary directory kept at: $tmpdir' ERR + # remove the tmpdir when cancelled (postgrest-watch) + trap 'rm -rf "$tmpdir"' SIGINT SIGTERM # build once before running all the tests ${cabal-install}/bin/cabal v2-build ${devCabalOptions} --enable-tests all @@ -199,7 +201,7 @@ let ${withTmpDb postgresql} ${cabal-install}/bin/cabal v2-test ${devCabalOptions} # collect all the tix files - ${ghc}/bin/hpc sum --union --exclude=Paths_postgrest --output="$tmpdir"/tests.tix "$tmpdir"/io.tix "$tmpdir"/spec.tix + ${ghc}/bin/hpc sum --union --exclude=Paths_postgrest --output="$tmpdir"/tests.tix "$tmpdir"/io*.tix "$tmpdir"/spec.tix # prepare the overlay ${ghc}/bin/hpc overlay --output="$tmpdir"/overlay.tix test/coverage.overlay diff --git a/test/io-tests/test_io.py b/test/io-tests/test_io.py index 6c7517f4a..7b5d75e64 100644 --- a/test/io-tests/test_io.py +++ b/test/io-tests/test_io.py @@ -73,12 +73,22 @@ def defaultenv(): } +def hpctixfile(): + "Returns an individual filename for each test, if the HPCTIXFILE environment variable is set." + if "HPCTIXFILE" not in os.environ: + return "" + + tixfile = pathlib.Path(os.environ["HPCTIXFILE"]) + test = hash(os.environ["PYTEST_CURRENT_TEST"]) + return tixfile.with_suffix(f".{test}.tix") + + def dumpconfig(configpath=None, env=None, stdin=None): "Dump the config as parsed by PostgREST." env = env or {} command = [POSTGREST_BIN, "--dump-config"] - env["HPCTIXFILE"] = os.getenv("HPCTIXFILE", "") + env["HPCTIXFILE"] = hpctixfile() if configpath: command.append(configpath) @@ -88,7 +98,7 @@ def dumpconfig(configpath=None, env=None, stdin=None): ) process.stdin.write(stdin or b"") - result = process.communicate()[0] + result = process.communicate(timeout=5)[0] process.kill() process.wait() if process.returncode != 0: @@ -112,7 +122,7 @@ def run(configpath=None, stdin=None, env=None, port=None): baseurl = "http+unix://" + urllib.parse.quote_plus(str(socketfile)) command = [POSTGREST_BIN] - env["HPCTIXFILE"] = os.getenv("HPCTIXFILE", "") + env["HPCTIXFILE"] = hpctixfile() if configpath: command.append(configpath) @@ -127,8 +137,12 @@ def run(configpath=None, stdin=None, env=None, port=None): yield PostgrestProcess(process=process, session=PostgrestSession(baseurl)) finally: - process.kill() - process.wait() + process.terminate() + try: + process.wait(timeout=1) + except: + process.kill() + process.wait() def freeport():