Skip to content

Commit

Permalink
Attempt to fix potential race condition in tests
Browse files Browse the repository at this point in the history
By installing 2 packages using the same venv when running tests in
parallel, we might be running in race conditions. That might be the case
for https://cirrus-ci.com/task/4794694590791680
  • Loading branch information
abravalheri committed Dec 28, 2020
1 parent eb012eb commit 9e0a3f4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
9 changes: 9 additions & 0 deletions tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import stat
import sys
import traceback
from os import environ
from pathlib import Path
from shutil import rmtree
from subprocess import STDOUT, CalledProcessError, check_output
Expand Down Expand Up @@ -53,6 +54,14 @@ def set_writable(func, path, _exc_info):
func(path)


def merge_env(other=None, **kwargs):
"""Create a dict from merging items to the current ``os.environ``"""
env = {k: v for k, v in environ.items()} # Clone the environ as a dict
env.update(other or {})
env.update(kwargs)
return env


def run(*args, **kwargs):
"""Run the external command. See ``subprocess.check_output``."""
# normalize args
Expand Down
13 changes: 7 additions & 6 deletions tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from pyscaffoldext.django.extension import Django

from .helpers import PYTHON, run
from .helpers import PYTHON, merge_env, run

FLAG = Django().flag
PUTUP = shell.get_executable("putup")
Expand Down Expand Up @@ -73,8 +73,8 @@ def remove_eventual_package(name):
run(f"{PIP} uninstall --yes {name}")


RND_NAME1 = "pkg82b3f28b-2811-4126-99a7-c88b4eb3fbb9"
RND_NAME2 = "pkg9402c0de-c719-41b6-83ad-35c477fcc517"
RND_NAME1 = "pkg82b3f28b-2811"
RND_NAME2 = "pkg9402c0de-c719"
# ^ Fixed random values.
# If we use random values for parametrization pytest-xdist gets confused
# and returns an error.
Expand All @@ -98,15 +98,16 @@ def test_manage_py_runs_nicely(tmpfolder, name, command):
print(list(glob("*")))
print(list(Path("src").glob("*/*")))
print(Path("manage.py").read_text())
run(f"{PIP} install -Ie .") # required for `python -m`
env = merge_env(PYTHONPATH=str(Path("src").resolve()))
# ^ required for `python -m` to work, without installing the package

# when we call manage.py or python -m djangotest
run(f"{command} migrate")
run(f"{command} migrate", env=env)
# then it should create a sqlite3 file in the right place
assert Path("db.sqlite3").exists()
assert not Path("src/db.sqlite3").exists()
# and everything should be fine
for task in TASKS:
run(f"{command} {task}")
run(f"{command} {task}", env=env)
finally:
remove_eventual_package(name)

0 comments on commit 9e0a3f4

Please sign in to comment.