Skip to content

Commit 07eba44

Browse files
Increased test coverage (#85)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent f2ab5ba commit 07eba44

File tree

16 files changed

+215
-101
lines changed

16 files changed

+215
-101
lines changed

.github/workflows/test.yml

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ jobs:
4949
run: |
5050
git fetch origin feature/migration-test-01:feature/migration-test-01
5151
git fetch origin feature/migration-test-02:feature/migration-test-02
52+
git fetch origin feature/migration-test-03:feature/migration-test-03
53+
git fetch origin feature/migration-test-04:feature/migration-test-04
5254
working-directory: migration_fixer/tests/demo
5355

5456
- name: Install dependencies
@@ -60,24 +62,11 @@ jobs:
6062
git checkout feature/migration-test-01
6163
working-directory: migration_fixer/tests/demo
6264

63-
- name: Run test_01
65+
- name: Run test
6466
run: make tox
6567
env:
6668
CODACY_PROJECT_TOKEN: ${{ secrets.CODACY_PROJECT_TOKEN }}
6769
PLATFORM: ${{ matrix.platform }}
68-
ENV: test_01
69-
70-
- name: Checkout feature/migration-test-01
71-
run: |
72-
git checkout feature/migration-test-01
73-
working-directory: migration_fixer/tests/demo
74-
75-
- name: Run test_02
76-
run: make tox
77-
env:
78-
CODACY_PROJECT_TOKEN: ${{ secrets.CODACY_PROJECT_TOKEN }}
79-
PLATFORM: ${{ matrix.platform }}
80-
ENV: test_02
8170

8271
- name: Run codacy-coverage-reporter
8372
uses: codacy/codacy-coverage-reporter-action@v1

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ clean-test: ## remove test and coverage artifacts
4545
@rm -f .coverage
4646
@rm -fr htmlcov/
4747
@rm -fr .pytest_cache
48+
@rm -fr .mypy_cache/
4849

4950
clean-docs: ## remove all doc artifacts
5051
@rm -fr site

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,7 @@ jobs:
115115
* Resolve migration conflicts on PR branches
116116
* Resolve migration conflicts on the default branch **(NOT RECOMMENDED)**
117117
* Supports default migration modules i.e (`0001_....py`)
118-
* Supports named migration modules i.e (`custom_migration.py`)
119-
* Re-nummber all migrations using the last migration on the default branch i.e `main` or `develop`
118+
* Re-number all migrations using the last migration on the default branch i.e `main` or `develop`
120119

121120
## Test Platforms
122121

docs/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ $ python manage.py makemigrations -b master --fix
5151
* Resolve migration conflicts on PR branches
5252
* Resolve migration conflicts on the default branch **(NOT RECOMMENDED)**
5353
* Supports default migration modules i.e (`0001_....py`)
54-
* Supports named migration modules i.e (`custom_migration.py`)
55-
* Re-nummber all migrations using the last migration on the default branch i.e `main` or `develop`
54+
* Re-number all migrations using the last migration on the default branch i.e `main` or `develop`
5655

5756
## Example
5857

migration_fixer/management/commands/makemigrations.py

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,7 @@
1313
from django.db.migrations.loader import MigrationLoader
1414
from git import InvalidGitRepositoryError, Repo
1515

16-
from migration_fixer.utils import (
17-
fix_named_migration,
18-
fix_numbered_migration,
19-
no_translations,
20-
)
16+
from migration_fixer.utils import fix_numbered_migration, no_translations
2117

2218

2319
class Command(BaseCommand):
@@ -69,8 +65,8 @@ def handle(self, *app_labels, **options):
6965
self.stdout.write("Verifying git repository...")
7066

7167
try:
72-
self.repo.git_dir
73-
except InvalidGitRepositoryError:
68+
self.repo.git_dir and self.repo.head.commit
69+
except (ValueError, InvalidGitRepositoryError):
7470
is_git_repo = False
7571
else:
7672
is_git_repo = True
@@ -87,15 +83,27 @@ def handle(self, *app_labels, **options):
8783
if self.verbosity >= 2:
8884
self.stdout.write("Retrieving the current branch...")
8985

90-
current_branch = self.repo.head.name
86+
current_branch = self.repo.head.ref.name
87+
88+
if self.repo.is_dirty(): # pragma: no cover
89+
raise CommandError(
90+
self.style.ERROR(
91+
"Git repository has uncommitted changes. "
92+
"Please commit any outstanding changes."
93+
)
94+
)
9195

9296
if self.verbosity >= 2:
9397
self.stdout.write(
9498
f"Fetching git remote origin changes on: {self.default_branch}"
9599
)
96100

97-
if current_branch == self.default_branch:
98-
self.repo.remotes[self.default_branch].origin.pull()
101+
if current_branch == self.default_branch: # pragma: no cover
102+
for remote in self.repo.remotes:
103+
remote.pull(
104+
self.default_branch,
105+
force=self.force_update,
106+
)
99107
else:
100108
for remote in self.repo.remotes:
101109
remote.fetch(
@@ -184,16 +192,16 @@ def handle(self, *app_labels, **options):
184192
)
185193

186194
last_remote = [
187-
fname
188-
for fname in conflict
189-
if fname not in local_filenames
195+
name for name in conflict if name not in local_filenames
190196
]
191197

192-
if not last_remote:
198+
if not last_remote: # pragma: no cover
193199
raise CommandError(
194200
self.style.ERROR(
195201
f"Unable to determine the last migration on: "
196-
f"{self.default_branch}",
202+
f"{self.default_branch}. "
203+
"Please verify the target branch using"
204+
'"-b [target branch]".',
197205
)
198206
)
199207

@@ -221,16 +229,16 @@ def handle(self, *app_labels, **options):
221229
seed=int(seed_split[0]),
222230
start_name=last_remote_filename,
223231
changed_files=changed_files,
232+
writer=(
233+
lambda message: self.stdout.write(message)
234+
if self.verbosity >= 2
235+
else lambda x: x
236+
),
224237
)
225-
else:
226-
if self.verbosity >= 2:
227-
self.stdout.write("Fixing named migration...")
228-
229-
fix_named_migration(
230-
app_label=app_label,
231-
migration_path=migration_path,
232-
start_name=last_remote_filename,
233-
changed_files=changed_files,
238+
else: # pragma: no cover
239+
raise ValueError(
240+
f"Unable to fix migration: {last_remote_filename}. \n"
241+
f"NOTE: It needs to begin with a number. eg. 0001_*",
234242
)
235243
except (ValueError, IndexError, TypeError) as e:
236244
self.stderr.write(f"Error: {e}")
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
DEFAULT_BRANCH = "main"
2+
# Single merge to main
3+
TEST_01_MIGRATION_BRANCH = "feature/migration-test-01"
4+
# Single merge to main after 01 is merged (single conflict)
5+
TEST_02_MIGRATION_BRANCH = "feature/migration-test-02"
6+
# Single merge to main after 01 is merged (multiple conflicts)
7+
TEST_03_MIGRATION_BRANCH = "feature/migration-test-03"
8+
# Single merge to main after 01 is merged (named module)
9+
TEST_04_MIGRATION_BRANCH = "feature/migration-test-04"

migration_fixer/tests/management/commands/utils.py renamed to migration_fixer/tests/management/commands/_utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
def execute_command(cmd, *args, **kwargs):
1111
out = StringIO()
1212
kwargs["stdout"] = out
13+
kwargs["stderr"] = out
1314
call_command(cmd, *args, **kwargs)
1415
output = out.getvalue()
1516

migration_fixer/tests/management/commands/conftest.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,11 @@ def git_repo(settings):
3131

3232
with GitRepo(workspace=workspace) as repo:
3333
yield repo
34+
35+
36+
@pytest.fixture
37+
def invalid_git_repo(settings):
38+
workspace = os.path.join(settings.BASE_DIR, "non_existing")
39+
40+
with GitRepo(workspace=workspace, delete=True) as repo:
41+
yield repo

migration_fixer/tests/management/commands/constants.py

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)