Skip to content

Commit

Permalink
Fix io tests not collecting coverage data when using run()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
wolfgangwalther authored and monacoremo committed Dec 30, 2020
1 parent c7f0d42 commit 568477b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
8 changes: 5 additions & 3 deletions nix/tests.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand 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
Expand Down
24 changes: 19 additions & 5 deletions test/io-tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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:
Expand All @@ -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)
Expand All @@ -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():
Expand Down

0 comments on commit 568477b

Please sign in to comment.