-
-
Notifications
You must be signed in to change notification settings - Fork 21
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
Detect if this is a merge or rebase in rebase_migration command #260
Closed
dmitrysleptsov
wants to merge
4
commits into
adamchainz:main
from
dmitrysleptsov:move-find-migration-names-to-command
Closed
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7b48cda
Move find_migration_names to Command class
dmitrysleptsov 15539e6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 343f069
Detect if merge is in progress
dmitrysleptsov 8156d93
Update comment, README and CHANGELOG
dmitrysleptsov 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 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 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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
from __future__ import annotations | ||
|
||
import subprocess | ||
import sys | ||
import time | ||
from functools import partial | ||
|
@@ -438,7 +439,7 @@ def test_none_when_no_second_marker(self): | |
result = module.find_migration_names(["<<<<<<<", "0002_author_nicknames"]) | ||
assert result is None | ||
|
||
def test_works_with_two_way_merge(self): | ||
def test_works_with_two_way_merge_during_rebase(self): | ||
result = module.find_migration_names( | ||
[ | ||
"<<<<<<<", | ||
|
@@ -450,7 +451,7 @@ def test_works_with_two_way_merge(self): | |
) | ||
assert result == ("0002_author_nicknames", "0002_longer_titles") | ||
|
||
def test_works_with_three_way_merge(self): | ||
def test_works_with_three_way_merge_during_rebase(self): | ||
result = module.find_migration_names( | ||
[ | ||
"<<<<<<<", | ||
|
@@ -464,6 +465,104 @@ def test_works_with_three_way_merge(self): | |
) | ||
assert result == ("0002_author_nicknames", "0002_longer_titles") | ||
|
||
def test_works_with_two_way_merge_during_merge(self): | ||
with mock.patch.object(module, "is_merge_in_progress", return_value=True): | ||
result = module.find_migration_names( | ||
[ | ||
"<<<<<<<", | ||
"0002_longer_titles", | ||
"=======", | ||
"0002_author_nicknames", | ||
">>>>>>>", | ||
] | ||
) | ||
assert result == ("0002_author_nicknames", "0002_longer_titles") | ||
|
||
def test_works_with_three_way_merge_during_merge(self): | ||
with mock.patch.object(module, "is_merge_in_progress", return_value=True): | ||
result = module.find_migration_names( | ||
[ | ||
"<<<<<<<", | ||
"0002_longer_titles", | ||
"|||||||", | ||
"0001_initial", | ||
"=======", | ||
"0002_author_nicknames", | ||
">>>>>>>", | ||
] | ||
) | ||
assert result == ("0002_author_nicknames", "0002_longer_titles") | ||
|
||
|
||
class IsMergeInProgressTests(SimpleTestCase): | ||
git_command = ["git", "rev-parse", "--git-dir"] | ||
|
||
def setUp(self) -> None: | ||
subprocess_run_patch = mock.patch( | ||
"django_linear_migrations.management.commands.rebase_migration" | ||
".subprocess.run" | ||
) | ||
self.mock_subprocess_run = subprocess_run_patch.start() | ||
|
||
self.addCleanup(subprocess_run_patch.stop) | ||
Comment on lines
+500
to
+507
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm gonna rewrite these tests to not use mocking |
||
|
||
@pytest.fixture(autouse=True) | ||
def tmp_path_fixture(self, tmp_path): | ||
git_dir_name = ".git" + str(time.time()).replace(".", "") | ||
self.git_dir_path = tmp_path / git_dir_name | ||
self.git_dir_path.mkdir() | ||
|
||
def test_true_when_git_repository_exists_and_merge_in_progress(self): | ||
with open(self.git_dir_path.joinpath("MERGE_HEAD"), "w"): | ||
pass | ||
self.mock_subprocess_run.return_value = subprocess.CompletedProcess( | ||
args=self.git_command, | ||
returncode=0, | ||
stdout=str(self.git_dir_path), | ||
) | ||
|
||
result = module.is_merge_in_progress() | ||
|
||
assert result is True | ||
self.mock_subprocess_run.assert_called_once_with( | ||
self.git_command, | ||
capture_output=True, | ||
check=True, | ||
text=True, | ||
) | ||
|
||
def test_false_when_git_repository_exists_and_not_merge_in_progress(self): | ||
self.mock_subprocess_run.return_value = subprocess.CompletedProcess( | ||
args=self.git_command, | ||
returncode=0, | ||
stdout=str(self.git_dir_path), | ||
) | ||
|
||
result = module.is_merge_in_progress() | ||
|
||
assert result is False | ||
|
||
def test_false_when_repository_not_exists(self): | ||
# subprocess.run raises SubprocessError with 128 code when there is | ||
# no git repository | ||
self.mock_subprocess_run.side_effect = subprocess.SubprocessError( | ||
f"Command '{self.git_command}' returned non-zero exit status 128" | ||
) | ||
|
||
result = module.is_merge_in_progress() | ||
|
||
assert result is False | ||
|
||
def test_false_when_git_command_is_not_available(self): | ||
# subprocess.run raises FailNotFound error when `git` command is not found | ||
self.mock_subprocess_run.side_effect = FileNotFoundError( | ||
"No such file or directory: 'git'" | ||
) | ||
|
||
result = module.is_merge_in_progress() | ||
|
||
assert result is False | ||
|
||
|
||
class MigrationAppliedTests(TestCase): | ||
def test_table_does_not_exist(self): | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If some error should be shown to user, this function can be moved under
Command
class