forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Fixed Django tests discovery #21468
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Fixed Django tests discovery #21468
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3e26cd1
mh-firouzjah-patch: Fixed Django tests discovery
mh-firouzjah 8be42eb
mh-firouzjah-patch: Updated django tests support
mh-firouzjah 62f9c53
Merge branch 'main' into mh-firouzjah-patch
eleanorjboyd 5cdf782
Merge branch 'main' into mh-firouzjah-patch
mh-firouzjah 821a0be
Merge branch 'main' into mh-firouzjah-patch
mh-firouzjah File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
""" | ||
This module sets up a Django environment to run Django tests. | ||
""" | ||
|
||
import os | ||
import re | ||
import sys | ||
|
||
|
||
def setup_django_test_env(workspace_directory="."): | ||
"""Configures the Django environment for running Django tests. | ||
|
||
If Django is not installed, workspace_directory is not in sys.path or | ||
manage.py can not be found inside the given workspace_directory, the function fails quietly. | ||
|
||
Args: | ||
workspace_directory (str): The current workspace directory that is expected to contain manage.py module | ||
|
||
Returns: | ||
None | ||
""" | ||
|
||
# To avoid false positive ModuleNotFoundError from django.setup() due to missing current workspace in sys.path | ||
sys.path.insert(0, os.getcwd()) | ||
|
||
try: | ||
import django | ||
except ImportError: | ||
return | ||
|
||
manage_py_module = os.path.join(workspace_directory, "manage.py") | ||
if not os.path.exists(manage_py_module): | ||
return | ||
|
||
dj_settings_module = None | ||
|
||
with open(manage_py_module, "r") as manage_py: | ||
pattern = r"^os\.environ\.setdefault\((\'|\")DJANGO_SETTINGS_MODULE(\'|\"), (\'|\")(?P<settings_path>[\w.]+)(\'|\")\)$" | ||
for line in manage_py.readlines(): | ||
match_result = re.match(pattern, line.strip()) | ||
if match_result is not None: | ||
dj_settings_module = match_result.groupdict().get("settings_path", None) | ||
break | ||
|
||
if dj_settings_module is None: | ||
return | ||
|
||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", dj_settings_module) | ||
|
||
try: | ||
django.setup() | ||
except ModuleNotFoundError: | ||
return | ||
|
||
return |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.