Repository navigation
feat: add management command context manager filter #39194
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
Open
rahulkanneri-2u
wants to merge
9
commits into
openedx:master
Choose a base branch
from
rahulkanneri-2u:feat/management-command-contextmanager-filter
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+131
−3
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
487de1b
feat: add management command context manager filter
rahulkanneri-2u 7991a98
fix: align management command filter type with management subdomain
rahulkanneri-2u c5843de
feat: use public management command filter from openedx-filters
rahul-apphelix 9a5b1dd
Merge branch 'master' into feat/management-command-contextmanager-filter
rahulkanneri-2u 3a21503
fix: review comments fixed
rahulkanneri-2u fff6653
Merge branch 'master' into feat/management-command-contextmanager-filter
rahulkanneri-2u e6a8357
Merge branch 'master' into feat/management-command-contextmanager-filter
robrap 358b83d
chore: require openedx-filters>=3.16.0
rahulkanneri-2u b1aed1d
Merge branch 'master' into feat/management-command-contextmanager-filter
rahulkanneri-2u 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| """ | ||
| Tests for the management command context manager filter hook in the top-level ``manage.py``. | ||
| """ | ||
| import os | ||
| import runpy | ||
| import sys | ||
| from contextlib import contextmanager | ||
| from pathlib import Path | ||
| from unittest import TestCase | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| from openedx_filters.management.filters import ManagementCommandContextmanagerRequested | ||
|
|
||
| MANAGE_PY = str(Path(__file__).resolve().parents[3] / "manage.py") | ||
|
|
||
|
|
||
| class ManagePyFilterTest(TestCase): | ||
| """ | ||
| Verify that manage.py runs the command inside the context manager returned by the filter pipeline. | ||
| """ | ||
|
|
||
| def _run_manage_py(self, argv, filter_side_effect=None): | ||
| """ | ||
| Execute manage.py as ``__main__`` with the given argv. | ||
|
|
||
| Returns the mocked ``run_filter`` and ``execute_from_command_line``. | ||
| """ | ||
| events = [] | ||
|
|
||
| @contextmanager | ||
| def tracking_contextmanager(): | ||
| events.append("enter") | ||
| yield | ||
| events.append("exit") | ||
|
|
||
| def fake_run_filter(**kwargs): | ||
| return ( | ||
| kwargs["command_contextmanager"] if filter_side_effect is None else tracking_contextmanager(), | ||
| kwargs["command_name"], | ||
| kwargs["service_variant"], | ||
| ) | ||
|
|
||
| mock_execute = MagicMock(side_effect=lambda *_a, **_k: events.append("execute")) | ||
| filter_patch = patch.object( | ||
| ManagementCommandContextmanagerRequested, "run_filter", side_effect=fake_run_filter | ||
| ) | ||
| with patch.object(sys, "argv", ["manage.py", *argv]), \ | ||
| patch.dict(os.environ), \ | ||
| filter_patch as mock_filter, \ | ||
| patch("django.core.management.execute_from_command_line", mock_execute): | ||
| runpy.run_path(MANAGE_PY, run_name="__main__") | ||
| return mock_filter, mock_execute, events | ||
|
|
||
| def test_filter_receives_command_name_and_service_variant(self): | ||
| mock_filter, mock_execute, _ = self._run_manage_py(["lms", "migrate", "--noinput"]) | ||
|
|
||
| kwargs = mock_filter.call_args.kwargs | ||
| assert kwargs["command_name"] == "migrate" | ||
| assert kwargs["service_variant"] == "lms" | ||
| # runpy sets argv[0] to the absolute path of manage.py, so only compare the arguments after it. | ||
| mock_execute.assert_called_once() | ||
| assert mock_execute.call_args.args[0][1:] == ["migrate", "--noinput"] | ||
|
|
||
| def test_cms_service_variant(self): | ||
| mock_filter, _, _ = self._run_manage_py(["cms", "check"]) | ||
|
|
||
| assert mock_filter.call_args.kwargs["service_variant"] == "cms" | ||
|
|
||
| def test_leading_options_are_skipped_to_find_command_name(self): | ||
| mock_filter, mock_execute, _ = self._run_manage_py(["lms", "--verbosity", "2", "--traceback", "migrate"]) | ||
|
|
||
| assert mock_filter.call_args.kwargs["command_name"] == "migrate" | ||
| mock_execute.assert_called_once() | ||
| assert mock_execute.call_args.args[0][1:] == ["--verbosity", "2", "--traceback", "migrate"] | ||
|
|
||
| def test_options_only_uses_fallback_name(self): | ||
| mock_filter, _, _ = self._run_manage_py(["lms", "--traceback"]) | ||
|
|
||
| assert mock_filter.call_args.kwargs["command_name"] == "no-argument" | ||
|
|
||
| def test_no_command_uses_fallback_name(self): | ||
| mock_filter, _, _ = self._run_manage_py(["lms"]) | ||
|
|
||
| assert mock_filter.call_args.kwargs["command_name"] == "no-argument" | ||
|
|
||
| def test_command_runs_inside_returned_contextmanager(self): | ||
| _, _, events = self._run_manage_py(["lms", "check"], filter_side_effect=True) | ||
|
|
||
| assert events == ["enter", "execute", "exit"] |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
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.