Skip to content

Commit

Permalink
Adopt black for code formatting (#131)
Browse files Browse the repository at this point in the history
Since most of the open PRs have been merged, it seems like a good time to put in place tooling that would have been too disruptive up to now.

This adds black to the toolchain and enforces code format in CI.
  • Loading branch information
paulmelnikow authored Sep 29, 2020
1 parent 22ba4c1 commit 9818a76
Show file tree
Hide file tree
Showing 31 changed files with 413 additions and 325 deletions.
7 changes: 6 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,10 @@ after_success:
matrix:
fast_finish: true
include:
- python: '2.7'
- python: '3.8'
install: pip install flake8
script: make lint
include:
- python: '3.8'
install: pip install black==20.8b1
script: make format
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ all: install test
.PHONY: install
install:
pip install -e ".[test]"
pip install flake8

.PHONY: test
test:
Expand All @@ -23,6 +22,14 @@ test:
lint:
flake8

.PHONY: format
format:
black --check setup.py snapshottest tests examples --exclude 'snapshots\/snap_.*.py$$'

.PHONY: format-fix
format-fix:
black setup.py snapshottest tests examples --exclude 'snapshots\/snap_.*.py$$'

.PHONY: clean
clean:
rm -rf dist/ build/
Expand Down
72 changes: 36 additions & 36 deletions examples/django_project/django_project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '$5@im(@s1+p9a&ob#1osrq%*-sue%90o6q*cf0)$h@urtql^4@'
SECRET_KEY = "$5@im(@s1+p9a&ob#1osrq%*-sue%90o6q*cf0)$h@urtql^4@"

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
Expand All @@ -31,53 +31,53 @@
# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'lists'
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"lists",
]

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]

ROOT_URLCONF = 'django_project.urls'
ROOT_URLCONF = "django_project.urls"

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
],
},
},
]

WSGI_APPLICATION = 'django_project.wsgi.application'
WSGI_APPLICATION = "django_project.wsgi.application"

TEST_RUNNER = 'snapshottest.django.TestRunner'
TEST_RUNNER = "snapshottest.django.TestRunner"

# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
"default": {
"ENGINE": "django.db.backends.sqlite3",
}
}

Expand All @@ -87,26 +87,26 @@

AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
},
]


# Internationalization
# https://docs.djangoproject.com/en/1.11/topics/i18n/

LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = "en-us"

TIME_ZONE = 'UTC'
TIME_ZONE = "UTC"

USE_I18N = True

Expand All @@ -118,4 +118,4 @@
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/

STATIC_URL = '/static/'
STATIC_URL = "/static/"
7 changes: 3 additions & 4 deletions examples/django_project/django_project/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@

def api_client_get(url):
return {
'url': url,
"url": url,
}


class TestDemo(SimpleTestCase):

def test_api_me(self):
my_api_response = api_client_get('/me')
my_api_response = api_client_get("/me")
self.assertMatchSnapshot(my_api_response)


if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()
2 changes: 1 addition & 1 deletion examples/django_project/django_project/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@


urlpatterns = [
url(r'^$', views.home_page, name='home'),
url(r"^$", views.home_page, name="home"),
]
2 changes: 1 addition & 1 deletion examples/django_project/lists/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@


class ListsConfig(AppConfig):
name = 'lists'
name = "lists"
19 changes: 13 additions & 6 deletions examples/django_project/lists/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,23 @@ class Migration(migrations.Migration):

initial = True

dependencies = [
]
dependencies = []

operations = [
migrations.CreateModel(
name='List',
name="List",
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=200)),
('description', models.CharField(max_length=200)),
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("name", models.CharField(max_length=200)),
("description", models.CharField(max_length=200)),
],
),
]
3 changes: 1 addition & 2 deletions examples/django_project/lists/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@


class ListTest(TestCase):

def test_uses_home_template(self):
List.objects.create(name="test")
response = self.client.get('/')
response = self.client.get("/")
self.assertMatchSnapshot(response.content.decode())
2 changes: 1 addition & 1 deletion examples/django_project/lists/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

def home_page(request):
lists = List.objects.all()
return render(request, 'home.html', {'lists': lists})
return render(request, "home.html", {"lists": lists})
36 changes: 18 additions & 18 deletions examples/pytest/test_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@

def api_client_get(url):
return {
'url': url,
"url": url,
}


def test_me_endpoint(snapshot):
"""Testing the API for /me"""
my_api_response = api_client_get('/me')
my_api_response = api_client_get("/me")
snapshot.assert_match(my_api_response)


def test_unicode(snapshot):
"""Simple test with unicode"""
expect = u'pépère'
expect = u"pépère"
snapshot.assert_match(expect)


Expand All @@ -27,7 +27,7 @@ def __init__(self, value):
self.value = value

def __repr__(self):
return 'SomeObject({})'.format(repr(self.value))
return "SomeObject({})".format(repr(self.value))


def test_object(snapshot):
Expand All @@ -44,21 +44,21 @@ def test_file(snapshot, tmpdir):
Test a file snapshot. The file contents will be saved in a sub-folder of the snapshots folder. Useful for large
files (e.g. media files) that aren't suitable for storage as text inside the snap_***.py file.
"""
temp_file = tmpdir.join('example.txt')
temp_file.write('Hello, world!')
temp_file = tmpdir.join("example.txt")
temp_file.write("Hello, world!")
snapshot.assert_match(FileSnapshot(str(temp_file)))


def test_multiple_files(snapshot, tmpdir):
"""
Each file is stored separately with the snapshot's name inside the module's file snapshots folder.
"""
temp_file1 = tmpdir.join('example1.txt')
temp_file1.write('Hello, world 1!')
temp_file1 = tmpdir.join("example1.txt")
temp_file1.write("Hello, world 1!")
snapshot.assert_match(FileSnapshot(str(temp_file1)))

temp_file1 = tmpdir.join('example2.txt')
temp_file1.write('Hello, world 2!')
temp_file1 = tmpdir.join("example2.txt")
temp_file1.write("Hello, world 2!")
snapshot.assert_match(FileSnapshot(str(temp_file1)))


Expand All @@ -70,16 +70,16 @@ def __repr__(self):
def test_nested_objects(snapshot):
obj = ObjectWithBadRepr()

dict_ = {'key': obj}
defaultdict_ = defaultdict(list, [('key', [obj])])
dict_ = {"key": obj}
defaultdict_ = defaultdict(list, [("key", [obj])])
list_ = [obj]
tuple_ = (obj,)
set_ = set((obj,))
frozenset_ = frozenset((obj,))

snapshot.assert_match(dict_, 'dict')
snapshot.assert_match(defaultdict_, 'defaultdict')
snapshot.assert_match(list_, 'list')
snapshot.assert_match(tuple_, 'tuple')
snapshot.assert_match(set_, 'set')
snapshot.assert_match(frozenset_, 'frozenset')
snapshot.assert_match(dict_, "dict")
snapshot.assert_match(defaultdict_, "defaultdict")
snapshot.assert_match(list_, "list")
snapshot.assert_match(tuple_, "tuple")
snapshot.assert_match(set_, "set")
snapshot.assert_match(frozenset_, "frozenset")
7 changes: 3 additions & 4 deletions examples/unittest/test_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@

def api_client_get(url):
return {
'url': url,
"url": url,
}


class TestDemo(snapshottest.TestCase):

def setUp(self):
pass

def test_api_me(self):
my_api_response = api_client_get('/me')
my_api_response = api_client_get("/me")
self.assertMatchSnapshot(my_api_response)


if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()
Loading

0 comments on commit 9818a76

Please sign in to comment.