Skip to content
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

Replace print with writing to stdout in syncpermissions command #87

Merged
merged 2 commits into from
Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ repos:
- flake8-bugbear
- flake8-comprehensions
- flake8-tidy-imports
- flake8-print
- repo: https://github.com/sirosen/check-jsonschema
rev: "0.18.1"
hooks:
Expand Down
6 changes: 4 additions & 2 deletions src/bananas/management/commands/syncpermissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ def handle_noargs(self, *args: object, **options: object) -> None:
)

if created:
print(f"Found new admin view: {ct.name} [{ct.app_label}]")
self.stdout.write(
f"Found new admin view: {ct.name} [{ct.app_label}]"
)

for codename, name in model._meta.permissions:
p, created = Permission.objects.update_or_create(
codename=codename, content_type=ct, defaults={"name": name}
)
if created:
print(f"Created permission: {name}")
self.stdout.write(f"Created permission: {name}")
16 changes: 4 additions & 12 deletions tests/tests/test_commands.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List
from unittest import mock

from django.core.management import call_command
from django.test import TestCase
Expand All @@ -13,17 +13,9 @@ def test_show_urls(self):
admin_api_url_count = 50
self.assertEqual(len(urls), admin_api_url_count)

class FakeSys:
class stdout:
lines: List[str] = []
with mock.patch.object(show_urls.sys, "stdout", autospec=True) as stdout: # type: ignore[attr-defined]
show_urls.show_urls()

@classmethod
def write(cls, line: str) -> None:
cls.lines.append(line)

show_urls.sys = FakeSys # type: ignore[attr-defined]
show_urls.show_urls()

self.assertEqual(len(FakeSys.stdout.lines), admin_api_url_count)
self.assertEqual(stdout.write.call_count, admin_api_url_count)

call_command("show_urls")